模块:少女歌剧

来自萌娘文库
サンムル讨论 | 贡献2020年8月14日 (五) 16:26的版本 (合并对话)
跳转至: 导航搜索

此模块的文档可以在模块:少女歌剧/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 actor = "???"
    local pair = true
    local actor_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_actor = mw.text.trim(mw.ustring.sub(line, 2))
            if actor_image ~= nil and new_actor ~= actor then
                if counter > 1 then
                    actor_image:attr("rowspan", counter)
                end
                actor_image = nil
                counter = 0
            end
            actor = new_actor
        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 actor_image ~= nil then
                if counter > 1 then
                    actor_image:attr("rowspan", counter)
                end
                actor_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")
                actor_image = actor_image or node:tag("td")
                    :wikitext("[[File:")
                    :wikitext(module.images.icon(actor))
                    :wikitext("|40px]]")
            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")
                :wikitext(rawtext)
            if not pair then
                node = node:done()
                counter = counter + 1
            end
            
            pair = not pair
        end
    end
    if actor_image ~= nil then
        if counter > 1 then
            actor_image:attr("rowspan", counter)
        end
        actor_image = nil
        counter = 0
    end
    
    return tostring(node)
end

return module