Module:CreditsTable

-- @author Dantman local p = {}

local function parse(text) local blocks = mw.text.gsplit(mw.text.trim(text), '\n\n+')

local credits = {} for block in blocks do		local lines = mw.text.split(block, '\n') local nameline = table.remove(lines, 1)

local credit = {} local link, name = mw.ustring.match(nameline, '(.+)%[(.+)%]') if not link then link = nameline name = nameline end

credit.link = mw.text.trim(link) credit.name = mw.text.trim(name) credit.actors = { EN = {}, JA = {} }

for ii, line in ipairs(lines) do			lang, person = mw.ustring.match(mw.text.trim(line), '^(%a+): *(.+)$') lang = mw.ustring.upper(lang) if lang ~= 'JA' and lang ~= 'EN' then mw.log(mw.ustring.format("The language %s is not recognized.", lang)) else table.insert(credit.actors[lang], {					name = person,					link = "Wikipedia:"..person				}) end end

table.insert(credits, credit) end

return credits end

local function link(target, text) wt = mw.ustring.format('%s', target, text) wt = tostring(mw.html.create('b'):wikitext(wt)) return wt end

local function render(credits) local t = mw.html.create('table'):attr('class', "wikitable fill-horiz cell-align-center") local haseng = false for i, credit in ipairs(credits) do		if #credit.actors.EN > 0 then haseng = true break end end

local tr = t:tag('tr') tr:tag('th'):wikitext("Role") tr:tag('th'):attr('colspan', 3):wikitext("Seiyū") if haseng then tr:tag('th'):wikitext("English Voice Actor") end

local tr = t:tag('tr') tr:tag('th'):wikitext("") tr:tag('th'):wikitext("English") tr:tag('th'):wikitext("Japanese") tr:tag('th'):wikitext("Rōmaji") if haseng then tr:tag('th') end

local function actorformat(actors) local results = { english = {}, japanese = {}, romaji = {} }

for i, actor in ipairs(actors) do			table.insert(results.english, link(actor.link, actor.name, true)) end

results.english = table.concat(results.english, ' ') results.japanese = table.concat(results.japanese, ' ') results.romaji = table.concat(results.romaji, ' ') return results end

for i, credit in ipairs(credits) do		local jactor = actorformat(credit.actors.JA) tr = t:tag('tr') tr:tag('td'):wikitext(link(credit.link, credit.name)) tr:tag('td'):wikitext(jactor.english) tr:tag('td'):wikitext(jactor.japanese) tr:tag('td'):wikitext(jactor.romaji) if haseng then tr:tag('td'):wikitext(actorformat(credit.actors.EN).english) end end

return tostring(t) end

function p._main(text) local credits = parse(text) return render(credits) end

function p.main(frame) local text = frame.args[1] or frame:getParent.args[1] return p._main(text) end

return p

--