Module:ScenarioArchiveToWiki

local p = {} local List = require 'Module:ListUtil' local cargo = mw.ext.cargo local scenarioData = mw.loadData 'Module:ScenarioArchiveToWiki/data' local lang_JPJA = mw.loadData("Module:FEH lang/JPJA/data")

--[======[	convert JSON use: =p.convertJSON({args={[==[ EnglishTextHere ]==], [==[ JapaneseTextHere ]==]}}) ]======]

local DUO_HERO = cargo.query('DuoHero,Languages', "DuoHero._pageName=Hero, English, Japanese", {	join = "DuoHero.WikiSecondPerson = Languages._pageName" })

local imgToHero = function (hero) return scenarioData.imgToHero[hero] or hero end

local npidToNameplateJP = function (pid) --return mw.ustring.gsub(mw.ustring.gsub(scenarioData.npidToNameplateJP[pid] or pid, '愛の祭', ), '[年明水着幼少女男夢伝承総選挙夏祭総選挙味方神階比翼冬若比翼%d]', ) return lang_JPJA['MPID_' .. pid] or scenarioData.npidToNameplateJP[pid] or pid end

local npidToNameplateEN = function (pid) local index = List.find_if(DUO_HERO, function (t) return t["Japanese"] == npidToNameplateJP(pid) end) if index then return DUO_HERO[index]["English"] else return scenarioData.npidToNameplateEN[pid] or pid end end

function p.convert(frame) -- Console use: p.convert({ args = { [==[EnglishTextHere]==], [==[JapaneseTextHere]==]} }) local jsonTable1 = {} for key,val in mw.ustring.gmatch(frame.args[1] or "", "%s*(.-): ([^\n]*)" ) do		jsonTable1[#jsonTable1 + 1] = {["key"] = key, ["value"] = val} end local jsonTable2 = {} for key,val in mw.ustring.gmatch(frame.args[2] or "", "%s*(.-): ([^\n]*)" ) do		jsonTable2[#jsonTable2 + 1] = {["key"] = key, ["value"] = val} end frame.args[1] = mw.text.jsonEncode(jsonTable1) frame.args[2] = mw.text.jsonEncode(jsonTable2) return p.convertJSON(frame) end

local parseScenario = function (s, lang) local frame = mw.getCurrentFrame local wikitext = {} local parsed = "" local sections = {""} local mwgsub = mw.ustring.gsub local mwtrim = mw.text.trim local mwsplit = mw.text.split local mwsub = mw.ustring.sub s = mwgsub( s, "$k", "" ) --s = mwgsub( s, "$n", "" ) -- quickfix until meaning of $n is determined s = mwgsub( s, "$p", "\t" ) s = mwgsub( s, "$Nu", "" ) s = mwgsub( s, "$Nf", "" ) for section in mw.ustring.gmatch(s, "$[^$]*") do		local parts = mwsplit(section, "|") for _,v in ipairs(parts) do			if v ~= "" then sections[#sections + 1] = mwgsub( mwgsub( mwtrim(v), "%s*\t%s*", " " ), "\n" , " " ) end end end local name local heroMatchesName local img local expression for _, section in ipairs(sections) do		if mwsub(section, 1, 1) ~= "$" then local dialogue = section if dialogue ~= "" then local STT = "" else STT = STT .. "}}"				end wikitext[#wikitext + 1] = STT end elseif mwsub(section, 2, 3) == "Wm" then local speakerInfo = mwsplit(mwsub(section, 4), ",") local npid = mw.ustring.match( speakerInfo[1], "M[EP]ID_(.*)" ) local nameJPJA = npidToNameplateJP(npid) --frame:expandTemplate{ title = "npidToNameplateJP", args = { npid } } img = imgToHero(speakerInfo[2]) --frame:expandTemplate{ title = 'imgToHero', args = { speakerInfo[2] } } heroMatchesName = nameJPJA == frame:expandTemplate{ title = "GetHeroJPName", args = { img } } if lang == "ja" then name = nameJPJA else name = npidToNameplateEN(npid) --frame:expandTemplate{ title = "npidToNameplateEN", args = { npid } } end expression = speakerInfo[3] elseif mwsub(section, 2, 2) == "n" then local npid = mw.ustring.match( mwsub(section, 3), "M[EP]ID_(.*)" ) local nameJPJA = npidToNameplateJP(npid) --frame:expandTemplate{ title = "npidToNameplateJP", args = { npid } } heroMatchesName = nameJPJA == frame:expandTemplate{ title = "GetHeroJPName", args = { img } } if lang == "ja" then name = nameJPJA else name = npidToNameplateEN(npid) --frame:expandTemplate{ title = "npidToNameplateEN", args = { npid } } end elseif mwsub(section, 2, 2) == "E" then expression = mwsub(section, 3) elseif mwsub(section, 2, 4) == "Sbp" then wikitext[#wikitext + 1] = "" elseif mwsub(section, 2, 4) == "Ssp" then wikitext[#wikitext + 1] = "" elseif mwsub(section, 2, 3) == "Fo" then local params = mwsplit(mwsub(section, 4), ",") wikitext[#wikitext + 1] = "" end end return wikitext end

local parseStructure = function (obj, lang) local opening = {} local mapBegin = {} local mapEnd = {} local ending = {}

for _, t in ipairs(obj) do		if (t["key"] == "MID_SCENARIO_OPENING_BGM") then opening[#opening + 1] = "" elseif (t["key"] == "MID_SCENARIO_OPENING_IMAGE") then opening[#opening + 1] = "" elseif (t["key"] == "MID_SCENARIO_OPENING") then List.concat_self(opening, parseScenario(t["value"], lang)) elseif (t["key"] == "MID_SCENARIO_MAP_BEGIN_BGM") then mapBegin[#mapBegin + 1] = "" elseif (t["key"] == "MID_SCENARIO_MAP_BEGIN_IMAGE") then mapBegin[#mapBegin + 1] = "" elseif (t["key"] == "MID_SCENARIO_MAP_BEGIN") then List.concat_self(mapBegin, parseScenario(t["value"], lang)) elseif (t["key"] == "MID_SCENARIO_MAP_END_BGM") then mapEnd[#mapEnd + 1] = "" elseif (t["key"] == "MID_SCENARIO_MAP_END_IMAGE") then mapEnd[#mapEnd + 1] = "" elseif (t["key"] == "MID_SCENARIO_MAP_END") then List.concat_self(mapEnd, parseScenario(t["value"], lang)) elseif (t["key"] == "MID_SCENARIO_ENDING_BGM") then ending[#ending + 1] = "" elseif (t["key"] == "MID_SCENARIO_ENDING_IMAGE") then ending[#ending + 1] = "" elseif (t["key"] == "MID_SCENARIO_ENDING") then List.concat_self(ending, parseScenario(t["value"], lang)) end end

local wikitext = {} local sectionCount = 0

if #opening > 0 then sectionCount = sectionCount + 1 wikitext[#wikitext + 1] = "===Opening===" wikitext[#wikitext + 1] = "" List.concat_self(wikitext, opening) wikitext[#wikitext + 1] = "" end if #mapBegin > 0 then sectionCount = sectionCount + 1 wikitext[#wikitext + 1] = "===Beginning of the battle===" wikitext[#wikitext + 1] = "" List.concat_self(wikitext, mapBegin) wikitext[#wikitext + 1] = "" end if #mapEnd > 0 then sectionCount = sectionCount + 1 wikitext[#wikitext + 1] = "====Stage Clear====" wikitext[#wikitext + 1] = "" List.concat_self(wikitext, mapEnd) wikitext[#wikitext + 1] = "" end if #ending > 0 then sectionCount = sectionCount + 1 wikitext[#wikitext + 1] = "===Ending===" wikitext[#wikitext + 1] = "" List.concat_self(wikitext, ending) wikitext[#wikitext + 1] = "" end

return wikitext, sectionCount end

function p.convertJSON(frame) local wikiTextUSEN, sectionCountUSEN = parseStructure(mw.text.jsonDecode(frame.args[1] or "[]"), "en") local wikiTextJPJA, sectionCountJPJA = parseStructure(mw.text.jsonDecode(frame.args[2] or "[]"), "ja")

local outputWikitext = {'==Story=='} if sectionCountUSEN == 1 and sectionCountJPJA == 1 and wikiTextUSEN[1] == wikiTextJPJA[1] then outputWikitext[#outputWikitext + 1] = table.remove(wikiTextUSEN, 1) table.remove(wikiTextJPJA, 1) end outputWikitext[#outputWikitext + 1] = '' List.concat_self(outputWikitext, wikiTextUSEN) outputWikitext[#outputWikitext + 1] = '' List.concat_self(outputWikitext, wikiTextJPJA) outputWikitext[#outputWikitext + 1] = '' outputWikitext = table.concat(outputWikitext, '\n') mw.log(mw.text.trim(outputWikitext)) return outputWikitext end

return p