模块:少女歌剧

来自萌娘文库
サンムル讨论 | 贡献2020年8月15日 (六) 11:30的版本 (修复tooltip显示文本错误)
(差异) ←上一版本 | 最后版本 (差异) | 下一版本→ (差异)
跳转至: 导航搜索

此模块的文档可以在模块:少女歌剧/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 table.concat(new_actors) ~= table.concat(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
            local actor_names = {}
            for _, actor in ipairs(actors) do
            	table.insert(actor_names, module.images.officialName(actor) or "???")
            end
            node:tag("td"):tag("span")
                :attr("title", table.concat(actor_names, "&"))
                :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