Module:StorySim

来自Rotaeno中文维基
SSF讨论 | 贡献2023年12月18日 (一) 11:38的版本

可在Module:StorySim/doc创建此模块的帮助文档

local p = {}

local getArgs = require('Module:Arguments').getArgs

local function getTextList(txt,add,mod)
    local t = {};
    local isValid = false;
    for i in string.gmatch(txt .. add,mod) do
        isValid = isValid or (i ~= '')
        if isValid then table.insert(t,i);end
    end
    return t;
end
local function delbr(txt)
    local t = txt;
    while true do
        local a = string.sub(t,-4)
        if a ~= '<br>' then break;end
        t = string.sub(t,1,-5);
    end
    return t;
end

local function getParam(txt,id)
    return string.match(txt,id .. '"(.-)"')
end

local function _summonTextShow(tag,txt)
    local s = '';
    for _,v in ipairs(getTextList(txt,'\n',"(.-)%c")) do
        s = s .. v .. '<br>';
    end
    tag:tag("div"):addClass("textShow"):wikitext(delbr(s));
end

local function summonTextShow(tag,txt)
    for _, v in ipairs(getTextList(txt,"[/]","(.-)%[/%]")) do _summonTextShow(tag,v) end
end

local function summonTalkShow(tag,txt,CName)
    CName = CName or {}
    local tableHTML = tag:tag("table"):addClass("talkShow")
    local l = getTextList(txt,'\n',"(.-)%c");
    local node;
    local _type;
    local isEmpty = true;
    local i = 0;
    for a,b in string.gmatch(string.sub(txt,2) .. '\n[','(.-)%](.-)%c%[') do
        i = i + 1;
		-- local a = 'name="伊洛" class="123"''
        -- local b = '知道了知道了。'
        isEmpty = (b == '' or a == '/');
        if isEmpty then b = '<div class="addLine"></div>';end
        local n = getParam(a,"name");
        if n then
            _type = getParam(a,"type") or 'talk'
            node = tableHTML:tag('tr')
                    :tag('th'):wikitext(CName[n] or n):done()
                    :tag('td')
        else
            _type = getParam(a,"type") or _type
        end

        if _type == 'talk' then node:wikitext(b):done()
        elseif _type == 'think' then
            if isEmpty then node:wikitext(b):done()
            else node:tag('span'):addClass('thought'):wikitext(b):done()
            end
        else (node or tableHTML:tag('tr')
            :tag('th'):wikitext('[Warning]'):done()
            :tag('td')):tag("span"):addClass("storyError"):wikitext(string.format("警告:无法识别类型名!(line=%d)",i)):done();
        end
        if not isEmpty then (node or tableHTML:tag('tr')
            :tag('th'):wikitext('[Warning]'):done()
            :tag('td')):wikitext('<br>') end
	end
end

-- Start --
local function makeInvokeFunc(funcName)
    return function(frame)
        local args = getArgs(frame)
        return p[funcName](args)
    end
end

p.main = makeInvokeFunc('_main')
function p._main(args)-- 生成HTML的主函数 部分代码来源:[[Module:Songtable]]
    --- 构建HTML ---
    local box = mw.html.create("table"):addClass("rotable mw-collapsible mw-collapsed storyTable");
    box:tag('tr'):tag('th'):wikitext(args['title'] or "标题");
    if args['autoUpload']then
        local rawTxt = mw.getCurrentFrame():expandTemplate{ title = 'Stories/' .. args['fName'] };
        local CNameTxt = mw.getCurrentFrame():expandTemplate{ title = 'Stories/CName' };
        local CName = setmetatable({},{
            __index = function(t,k)
                local _,e,_ = string.find(k,"%.");
                if e then return rawget(t,string.sub(k,1,e-1));end
                return nil;
            end
        }); ---@type table
        for n,t in string.gmatch(CNameTxt .. '\n','([%a%d%-]*): (.-)\n')do
            CName[n] = t;
        end
        local tag = box:tag("tr"):tag("td"):addClass("storySim");
        tag:tag('span'):addClass("storyWarning"):wikitext('该文本为自动生成,如有错误请及时指出。'):done()
        summonTalkShow(tag,rawTxt,CName);
        return tostring(box);
    end
    for index, arg in ipairs(args) do
        local tag = box:tag("tr"):tag("td"):addClass("storySim");
            local a = string.match(arg,"textShow>(.+)");
            local b = string.match(arg,"talkShow>(.+)");

            if a then summonTextShow(tag,a);
            elseif b then summonTalkShow(tag,b);
            else tag:tag("span"):addClass("storyError"):wikitext(string.format("警告:无法识别源代码!(arg=%d)",index)):done();
        end
        
        -- box:tag("span"):wikitext(type(index) .. arg):done()
    end
    return tostring(box)
end
return p