“模块:少女歌剧”的版本间的差异
小 (debug) |
小 (支持多人台词) |
||
第18行: | 第18行: | ||
:done() | :done() | ||
:done() | :done() | ||
− | local | + | local actors = { "???" } |
local pair = true | local pair = true | ||
− | local | + | local actors_image = nil |
local counter = 0 | local counter = 0 | ||
for index, line in ipairs(args) do | for index, line in ipairs(args) do | ||
第32行: | 第32行: | ||
counter = counter + 1 | counter = counter + 1 | ||
end | end | ||
− | local | + | |
− | if | + | local new_actors = { mw.text.trim(mw.ustring.sub(line, 2)) } |
+ | while true do | ||
+ | local str = new_actors[#new_actors] | ||
+ | local pos = mw.ustring.find(str, "&[^&]") | ||
+ | if pos then | ||
+ | new_actors[#new_actors] = mw.ustring.gsub(mw.text.trim(mw.ustring.sub(str, 1, pos - 1)), "&&", "&") | ||
+ | new_actors[#new_actors + 1] = mw.text.trim(mw.ustring.sub(str, pos + 1)) | ||
+ | else | ||
+ | new_actors[#new_actors] = mw.ustring.gsub(str, "&&", "&") | ||
+ | break | ||
+ | end | ||
+ | end | ||
+ | |||
+ | if actors_image ~= nil and new_actors ~= actors then | ||
if counter > 1 then | if counter > 1 then | ||
− | + | actors_image:attr("rowspan", counter) | |
end | end | ||
− | + | actors_image = nil | |
counter = 0 | counter = 0 | ||
end | end | ||
− | + | actors = new_actors | |
elseif mw.ustring.find(mw.text.trim(line), "^-[^-]") then | elseif mw.ustring.find(mw.text.trim(line), "^-[^-]") then | ||
if not pair then | if not pair then | ||
第48行: | 第61行: | ||
counter = counter + 1 | counter = counter + 1 | ||
end | end | ||
− | if | + | if actors_image ~= nil then |
if counter > 1 then | if counter > 1 then | ||
− | + | actors_image:attr("rowspan", counter) | |
end | end | ||
− | + | actors_image = nil | |
counter = 0 | counter = 0 | ||
end | end | ||
第64行: | 第77行: | ||
module.images = module.images or require("Module:少女歌剧/images") | module.images = module.images or require("Module:少女歌剧/images") | ||
node = node:tag("tr") | node = node:tag("tr") | ||
− | + | if not actors_image then | |
− | :wikitext("[[File:") | + | actors_image = node:tag("td") |
− | + | local flag = true | |
− | + | for _, actor in ipairs(actors) do | |
− | + | if flag then | |
− | + | flag = false | |
+ | else | ||
+ | actors_image:newline() -- 多人时换行。 | ||
+ | end | ||
+ | actors_image | ||
+ | :wikitext("[[File:") | ||
+ | :wikitext(module.images.icon(actor)) | ||
+ | :wikitext("|40px|link=|") | ||
+ | :wikitext(module.images.officialName(actor) or "???") | ||
+ | :wikitext("]]") | ||
+ | end | ||
+ | end | ||
end | end | ||
local rawtext = mw.ustring.gsub(line, "[-@][-@]", function(match) | local rawtext = mw.ustring.gsub(line, "[-@][-@]", function(match) | ||
第78行: | 第102行: | ||
end | end | ||
node:tag("td"):tag("span") | node:tag("td"):tag("span") | ||
− | :attr("title", module.images.officialName( | + | :attr("title", module.images.officialName(actors) or "???") |
:wikitext(rawtext) | :wikitext(rawtext) | ||
if not pair then | if not pair then | ||
第89行: | 第113行: | ||
end | end | ||
node = node:allDone() | node = node:allDone() | ||
− | if | + | if actors_image ~= nil then |
if counter > 1 then | if counter > 1 then | ||
− | + | actors_image:attr("rowspan", counter) | |
end | end | ||
− | + | actors_image = nil | |
counter = 0 | counter = 0 | ||
end | end |
2020年8月15日 (六) 11:13的版本
此模块的文档可以在模块:少女歌剧/doc创建
local module = {} function module.scenario(frame) local args = frame.args local node = mw.html.create("table") :addClass("wikitable") :tag("tr") :tag("th") :done() :tag("th") :css("width", "50%") :wikitext("原文") :done() :tag("th") :css("width", "50%") :wikitext("译文") :done() :done() local actors = { "???" } local pair = true local actors_image = nil local counter = 0 for index, line in ipairs(args) do line = mw.text.trim(line) if mw.ustring.find(mw.text.trim(line), "^@[^@]") then if not pair then node:tag("td") node = node.done() pair = true counter = counter + 1 end local new_actors = { mw.text.trim(mw.ustring.sub(line, 2)) } while true do local str = new_actors[#new_actors] local pos = mw.ustring.find(str, "&[^&]") if pos then new_actors[#new_actors] = mw.ustring.gsub(mw.text.trim(mw.ustring.sub(str, 1, pos - 1)), "&&", "&") new_actors[#new_actors + 1] = mw.text.trim(mw.ustring.sub(str, pos + 1)) else new_actors[#new_actors] = mw.ustring.gsub(str, "&&", "&") break end end if actors_image ~= nil and new_actors ~= actors then if counter > 1 then actors_image:attr("rowspan", counter) end actors_image = nil counter = 0 end actors = new_actors elseif mw.ustring.find(mw.text.trim(line), "^-[^-]") then if not pair then node:tag("td") node = node.done() pair = true counter = counter + 1 end if actors_image ~= nil then if counter > 1 then actors_image:attr("rowspan", counter) end actors_image = nil counter = 0 end local chapter = mw.text.trim(mw.ustring.sub(line, 2)) node:tag("tr"):tag("th") :attr("colspan", 3) :wikitext(chapter) pair = true else if pair then module.images = module.images or require("Module:少女歌剧/images") node = node:tag("tr") if not actors_image then actors_image = node:tag("td") local flag = true for _, actor in ipairs(actors) do if flag then flag = false else actors_image:newline() -- 多人时换行。 end actors_image :wikitext("[[File:") :wikitext(module.images.icon(actor)) :wikitext("|40px|link=|") :wikitext(module.images.officialName(actor) or "???") :wikitext("]]") end end end local rawtext = mw.ustring.gsub(line, "[-@][-@]", function(match) return mw.ustring.sub(match, 1, 2) end) if pair then rawtext = frame:expandTemplate{ title = "lj", args = { rawtext } } end node:tag("td"):tag("span") :attr("title", module.images.officialName(actors) or "???") :wikitext(rawtext) if not pair then node = node:done() counter = counter + 1 end pair = not pair end end node = node:allDone() if actors_image ~= nil then if counter > 1 then actors_image:attr("rowspan", counter) end actors_image = nil counter = 0 end return tostring(node) end return module