Module:Sandbox/SinonJZH/SwitchTranslate
跳到导航
跳到搜索
local p = {} local _frame = nil local _VAR_PREFIX = "switch_translate" local _TOGGLE_PREFIX = "mw-customtoggle-switch_translate_" local _COLL_PREFIX = "mw-customcollapsible-switch_translate_" local function write_index_var(index) mw.logObject(index) for i,v in ipairs(index) do mw.log("#vardefine", _VAR_PREFIX .. "index" .. i, v) _frame:callParserFunction("#vardefine", _VAR_PREFIX .. "index" .. i, v) -- index变量用于存放每个index对应的译名名称 end mw.log("#vardefine", _VAR_PREFIX .. "len", table.maxn(index)) _frame:callParserFunction("#vardefine", _VAR_PREFIX .. "len", table.maxn(index)) -- len变量用于存放index的数量 end local function get_index_data(data) local index = {} for k,v in pairs(data[1]) do if k ~= "default" then table.insert(index, k) end end write_index_var(index) return index end local function write_data_var(index,data) for i, v in ipairs(data) do local all_name = "|" .. v["default"] mw.log("#vardefine", _VAR_PREFIX .. "data" .. v["default"], i) _frame:callParserFunction("#vardefine", _VAR_PREFIX .. "data" .. v["default"], i) for ii, vv in ipairs(index) do local name = v[vv] or v["default"] all_name = all_name .. "|" .. name mw.log("#vardefine", _VAR_PREFIX .. "data" .. name, i) _frame:callParserFunction("#vardefine", _VAR_PREFIX .. "data" .. name, i) -- data变量的["name"]键值用于存放对应的index索引 end mw.log("#vardefine", _VAR_PREFIX .. "data" .. i, all_name) _frame:callParserFunction("#vardefine", _VAR_PREFIX .. "data" .. i, all_name) -- -- data变量的[index]键值用于存放index对应的译名列表,格式为:"|default|译名1|译名2|..." end end local function generate_switch(index) local output = "<span class=\"switch-translate-switch\">" output = output .. mw.ustring.format("<span class=\"mw-collapsible mw-collapsed switch-translate-btn-0\" id=\"%s\"><span class=\"%s\">%s</span></span>", _COLL_PREFIX .. 0, _TOGGLE_PREFIX .. 0, "共识译名") for i,v in ipairs(index) do output = output .. mw.ustring.format("<span class=\"mw-collapsible switch-translate-btn-%s\" id=\"%s\"><span class=\"%s\">%s</span></span>", i, _COLL_PREFIX .. i, _TOGGLE_PREFIX .. i, v) end output = output .. "</span>" return output end function p.switch(page_name) local data = mw.text.jsonDecode(mw.title.new(page_name):getContent()) mw.log("获取的data数据:") mw.logObject(data) local index = get_index_data(data) write_data_var(index,data) return generate_switch(index) end function p.show(name) local index = _frame:callParserFunction("#var", _VAR_PREFIX .. "data" .. name) local all_name = _frame:callParserFunction("#var", _VAR_PREFIX .. "data" .. index) local output = "<span class=\"switch-translate-name\">" local i = 0 for name in mw.ustring.gmatch(all_name, "|([^|]+)") do if i == 0 then output = output .. mw.ustring.format("<span class=\"mw-collapsible\" id=\"%s\">%s</span>", _COLL_PREFIX .. i, name) else output = output .. mw.ustring.format("<span class=\"mw-collapsible mw-collapsed\" id=\"%s\">%s</span>", _COLL_PREFIX .. i, name) end i = i + 1 end output = output .. "</span>" return output end function p.main(frame) _frame = frame local mode = frame.args[1] local arg = frame.args[2] if not mode or not arg then return "<span class=\"scribunto-error\">模块缺失必需参数!请前往模板文档确认用法!</span>" end if mode == "switch" then mw.incrementExpensiveFunctionCount() return p.switch(arg) elseif mode == "show" then return p.show(arg) end return "<span class=\"scribunto-error\">模块参数错误!请前往模板文档确认用法!</span>" end return p