Module:File series source

-- script that reates a formatted image source based on input local m = {};

-- formats a generic error message local function incorrectReferenceHandler(args) local start = " Invalid source; List of template arguments: "; for k, v in pairs(args) do       start = start .. tostring(k) .. " = " .. tostring(v) .. " ";    end return start; end

-- this function provides generic formatting for mangas or episodes local function formatGenericSeries(args, descriptor) local chapter = args[2]; local page = args[3];

-- if no chapter given -> invalid input if not chapter then return incorrectReferenceHandler(args); end -- format strings used later local chapterFormat = descriptor.chapterFormat; local categoryFormat = descriptor.categoryFormat; local pageFormat = "Page %s"; -- handle special chapters in a series local specialCase = descriptor.specialChapters[string.lower(chapter)]; if specialCase then chapterFormat = specialCase.chapterFormat; categoryFormat = specialCase.categoryFormat; end -- now general chapters chapterNum = tonumber(chapter); -- check if this is a valid chapter number, if not output error if not specialCase and (not chapterNum or       (descriptor.minimum and chapterNum < descriptor.minimum) or        (descriptor.maximum and chapterNum > descriptor.maximum) or        chapterNum ~= chapterNum) then return incorrectReferenceHandler(args); end -- do the formatting return string.format(chapterFormat, chapter, chapter) .. " " ..       (page and string.format(pageFormat, page)) .. " " ..       string.format(categoryFormat, chapter); end

-- the following tables describe the different formatting depending on manga/anime/novel series -- the formatting is applied by formatGenericSeries

-- special manga chapters local specialChapters = { rize = { chapterFormat = "Tokyo Ghoul Side Story: Rize", categoryFormat = ""; },   joker = { chapterFormat = "Tokyo Ghoul:re Side Story: Joker", categoryFormat = ""; },   oneshot = { chapterFormat = "Tokyo Ghoul Oneshot", categoryFormat = ""; } }

-- describes the original manga local tgmangadescriptor = { chapterFormat = "Tokyo Ghoul Chapter %s", categoryFormat = "", minimum = 1, maximum = 143, specialChapters = specialChapters }

-- describes :re local remangadescriptor = { chapterFormat = "Tokyo Ghoul:re Chapter %s", categoryFormat = "", minimum = 1, specialChapters = specialChapters }

-- describes jack local jackmangadescriptor = { chapterFormat = "Tokyo Ghoul: Jack Chapter %s", categoryFormat = "", minimum = 1, maximum = 7, specialChapters = {} }

--describes the anime local animedescriptor = { chapterFormat = "Tokyo Ghoul Episode %s", categoryFormat = "", minimum = 1, maximum = 24, specialChapters = { jack = { chapterFormat = "Tokyo Ghoul: Jack (OVA)", categoryFormat = ""; },       pinto = { chapterFormat = "Tokyo Ghoul: Pinto (OVA)", categoryFormat = ""; }   } }

-- descriptors for volumes local tgvoldescriptor = { chapterFormat = "Tokyo Ghoul Volume %s", categoryFormat = "", minimum = 1, maximum = 14, specialChapters = {} }

local revoldescriptor = { chapterFormat = "Tokyo Ghoul:re Volume %s", categoryFormat = "", minimum = 1, specialChapters = {} }

-- descriptors for novels local hibidescriptor = { chapterFormat = "Tokyo Ghoul: Hibi Chapter %s", categoryFormat = "", minimum = 1, maximum = 6, specialChapters = {} }

local kuuhakudescriptor = { chapterFormat = "Tokyo Ghoul: Kuuhaku Chapter %s", categoryFormat = "", minimum = 1, maximum = 5, specialChapters = {} }

local sekijitsudescriptor = { chapterFormat = "Tokyo Ghoul: Sekijitsu Chapter %s", categoryFormat = "", minimum = 1, maximum = 6, specialChapters = {} }

-- register functions that will handle the different series local handlers = {}; m.sourceHandlers = handlers;

-- register original manga handlers handlers["tg-manga"] = function(args) return formatGenericSeries(args, tgmangadescriptor); end; handlers["tg"] = handlers["tg-manga"];

handlers["tg-vol"] = function(args) return formatGenericSeries(args, tgvoldescriptor); end

-- register :re handlers handlers["re"] = function(args) return formatGenericSeries(args, remangadescriptor); end; handlers[":re"] = handlers["re"];

handlers["re-vol"] = function(args) return formatGenericSeries(args, revoldescriptor); end; handlers[":re-vol"] = handlers["re-vol"];

-- register Jack handler handlers["jack"] = function(args) if not args[2] then return "Tokyo Ghoul: Jack "; else return formatGenericSeries(args, jackmangadescriptor); end end;

-- register anime handler handlers["anime"] = function(args) return formatGenericSeries(args, animedescriptor); end;

-- register book handlers handlers["hibi"] = function(args) if not args[2] then return "Tokyo Ghoul: Hibi "; else return formatGenericSeries(args, hibidescriptor); end end;

handlers["kuuhaku"] = function(args) if not args[2] then return "Tokyo Ghoul: Kuuhaku "; else return formatGenericSeries(args, kuuhakudescriptor); end end;

handlers["sekijitsu"] = function(args) if not args[2] then return "Tokyo Ghoul: Sekijitsu "; else return formatGenericSeries(args, sekijitsudescriptor); end end;

-- main function function m.formatsource(f) local args = f.args; -- get the correct handler local handler = m.sourceHandlers[args[1]]; if not handler then -- if no handler fit: try the default manga handler instead table.insert(args, 1, "tg-manga"); handler = m.sourceHandlers[args[1]]; end -- call the handler return handler(args); end

return m;