Module:StorySim:修订间差异

来自Rotaeno中文维基
无编辑摘要
SSF讨论 | 贡献
无编辑摘要
 
(未显示同一用户的15个中间版本)
第1行: 第1行:
local p = {}
local p = {}


local CNames = require('Module:StorySim/CName').CNames
local getArgs = require('Module:Arguments').getArgs
local getArgs = require('Module:Arguments').getArgs
local Data = mw.loadData('Module:RotaenoStoryData/FullText')
local asmCNames = {}


local function getTextList(txt,add,mod)
local function writeCName(node,id)
    local t = {};
    if asmCNames[id] and CNames[id] then
    local isValid = false;
     return node:tag('span'):addClass('name changedName name_'..id)
   for i in string.gmatch(txt .. add,mod) do
           :tag('span'):wikitext(asmCNames[id]):done()
     isValid = isValid or (i ~= '')
           :tag('span'):wikitext(CNames[id]):done();
     if isValid then table.insert(t,i);end
    elseif asmCNames[id] or CNames[id] then
     return node:tag('span'):addClass('name name_'..id):wikitext(asmCNames[id] or CNames[id]);
   else return node:tag('span'):addClass('name UnknownName name_'..id):wikitext(id);
    end
    end
   return t;
end
end
local function delbr(txt)
 
    local t = txt;
local function _generic_AppendLine(self,txt)
   while true do
    self.curCNode:wikitext(txt);
     local a = string.sub(t,-4)
    return self;
     if a ~= '<br>' then break;end
     t = string.sub(t,1,-5);
   end
    return t;
end
end


local function getParam(txt,id)
local function _generic_Reset(self)
    return string.match(txt,id .. '"(.-)"')
    self.curNode = self.node:tag('div'):addClass('SimChild');
   return self;
end
end


local function _summonTextShow(tag,txt)
local _mtList = {
   local s = '';
   ['Novel'] = {-- Novel
    for _,v in ipairs(getTextList(txt,'\n',"(.-)%c")) do
     WriteLine = function(self,txt,_)
     s = s .. v .. '<br>';
       self.curCNode = self.curNode:tag('div'):wikitext(txt);
   end
       return self;
   tag:tag("div"):addClass("textShow"):wikitext(delbr(s));
     end;
     AppendLine = _generic_AppendLine,
     Reset = _generic_Reset
    },
   ['Chat'] = {-- Chat
     WriteLine = function(self,txt,name)
       if name == self.lastName then self.left = self.left;
       else self.left = not self.left;end
       self.lastName = name;
 
       local n = self.curNode:tag('tr');
       local l = n:tag('th'):addClass('L');
       self.curCNode = n:tag('td'):wikitext(txt);
       local r = n:tag('th'):addClass('R');
       if self.left then writeCName(l,name);self.curCNode:addClass('L')
       else writeCName(r,name);self.curCNode:addClass('R')
       end
       return self;
     end;
     AppendLine = _generic_AppendLine,
     Reset = function(self)
       if not self.init then
         self.init = true;
         self.left = false;
         self.curNode = self.node:tag('table'):addClass('SimChild');
       else self.curNode:tag('tr'):tag('th'):attr('colspan',3):addClass('LineBreak');
       end
     return self;
end
end
   },
   ['Dialogue'] = {-- Dialogue
     WriteLine = function(self,txt,name)
       local isThinking = false;
       if name=='' then isThinking = true;name='ilot';end
       local n = self.curNode:tag('tr');
       if name == self.lastName then
         self.left = self.left;
         self.i = self.i + 1;
         self.NameNode:attr('rowspan',self.i)
       else
         local l = n:tag('th'):addClass('L');writeCName(l,name);
         self.NameNode = l;
         self.lastName = name;
         self.i = 1;
       end
       self.curCNode = n:tag('td'):wikitext(txt);
       if isThinking then self.curCNode:addClass('thinking');end
       return self;
     end;
     AppendLine = _generic_AppendLine,
     Reset = function(self)
       if not self.init then
         self.init = true;
         self.curNode = self.node:tag('table'):addClass('SimChild');
       else self.curNode:tag('tr'):tag('th'):attr('colspan',2):addClass('LineBreak');
       end;self.i = 1;self.lastName=nil;self.NameNode = nil;
       return self;end
   },
   ['Fullscreen'] = {-- Fullscreen
     WriteLine = function(self,txt,_)
       self.curCNode = self.curNode:tag('div'):wikitext(txt);
       return self;end;
     AppendLine = _generic_AppendLine,
     Reset = _generic_Reset
   },
   ['FullscreenImportant'] = {-- FullscreenImportant
     WriteLine = function(self,txt,_)
       self.curCNode = self.curNode:wikitext(txt);
       return self;end;
     AppendLine = _generic_AppendLine,
     Reset = _generic_Reset
   },
   ['CgDialog'] = {-- CgDialog TBD
     WriteLine = function(self,txt,name)
       local N = self.curNode:tag('div'):addClass('DialogChild');
       if name ~= '' then local n = N:tag('div');writeCName(n,name);end
       self.curCNode = N:tag('div'):wikitext(txt);
       if name == '' then self.curCNode:addClass('thinking');end
       return self;
     end;
     AppendLine = _generic_AppendLine,
     Reset = _generic_Reset
   },
   ['ChapterTitle'] = {-- CT
     WriteLine = function(self,txt,_)
       local a,b = string.match(txt,'(.*)|(.*)');
       if a and b then
         self.curNode:tag('div'):addClass('subTitle'):wikitext(a);
         self.curCNode = self.curNode:tag('div'):addClass('Title'):wikitext(b);
       else self.curCNode = self.curNode:tag('div'):addClass('onlyTitle'):wikitext(txt);
       end
       return self;
     end;
     AppendLine = _generic_AppendLine,
     Reset = _generic_Reset
},
}
--- @class Printer:table
--- @field WriteLine fun(self:Printer,txt:string,name:string)
--- @field AppendLine fun(self:Printer,txt:string)
--- @field Reset fun(self:Printer)
local _ = {}


local function summonTextShow(tag,txt)
---@param HTML any
    for _, v in ipairs(getTextList(txt,"[/]","(.-)%[/%]")) do _summonTextShow(tag,v) end
---@param Type string
---@return Printer
local function createPrinter(HTML,Type)
    return setmetatable(
     {node = HTML:tag('tr'):tag('td'):addClass('storySim type_'..tostring(Type))},
     {__index = _mtList[Type]}):Reset();
end
end


local function summonTalkShow(tag,txt,CName)
--[[
    CName = CName or {}
qMTPList = {
    local tableHTML = tag:tag("table"):addClass("talkShow")
    Novel = 0,
    local l = getTextList(txt,'\n',"(.-)%c");
    Chat = 1, -- 对话(d)
    local node;
   Dialogue = 2, -- 对话(vn)
    local _type;
    Fullscreen = 3, -- 全屏文字(例子:c1开头)
    local isEmpty = true;
   FullscreenImportant = 4, -- 左下角文字(例子:c2结尾)
    local i = 0;
   CgDialog = 5 -- 带cg的对话(例子:ss2)
    for a,b in string.gmatch(string.sub(txt,2) .. '\n[','(.-)%](.-)%c%[') do
}
      i = i + 1;
 
-- local a = 'name=" 伊洛" class="123"''
dC_qTable = {
      -- local b = '知道了知道了。'
   ModifyTextPrinter = {0,5},
      isEmpty = (b == '' or a == '/');
   OverridePrintCommand = {1,5,6},
      if isEmpty then b = '<div class="addLine"></div>';end
    OverrideAppendLineBreakCommand = {2},
     local n = getParam(a,"name");
    Wait = {3},
      if n then
    HidePrinter = {4},
       _type = getParam(a,"type") or 'talk'
    ResetText = {5},
       node = tableHTML:tag('tr')
    OverrideDisplayNameCommand = {6,5,6}
           :tag('th'):wikitext(CName[n] or n):done()
}
           :tag('td')
]]
      else
 
       _type = getParam(a,"type") or _type
local function reco(str)
   while true do
     local s,e,c,t = string.find(str,'<color=(.-)>(.-)</color>');
      if s then str = string.sub(str,1,s-1) .. "<span class='Colored color_" .. c .. "' style='color:" .. c .. ";'>" .. t .. "</span>" .. string.sub(str,e+1)
      else break;
      end
   end
   while true do
      local s,e,c,t = string.find(str,'<size=(.-)>(.-)</size>');
      if s then str = string.sub(str,1,s-1) .. string.format("<span class='Sized size_%s' style='font-size:%.3fem;'>",c,(tonumber(c)/36)) .. t .. "</span>" .. string.sub(str,e+1)
      else break;
      end
      end
 
   end
     if _type == 'talk' then node:wikitext(b):done()
   local s,_,c,t = string.find(str,'<indent=(.-)%%>(.*)');
      elseif _type == 'think' then
      if c then str = string.sub(str,1,s-1) .. string.format("<span class='Indented indent_%s' style='padding-left:%.3fem;'>",c,(tonumber(c)/5)) .. t .. "</span>"
       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
      end
     if not isEmpty then (node or tableHTML:tag('tr')
   return str;
       :tag('th'):wikitext('[Warning]'):done()
       :tag('td')):wikitext('<br>') end
end
end
end


第87行: 第197行:
p.main = makeInvokeFunc('_main')
p.main = makeInvokeFunc('_main')
function p._main(args)-- 生成HTML的主函数 部分代码来源:[[Module:Songtable]]
function p._main(args)-- 生成HTML的主函数 部分代码来源:[[Module:Songtable]]
   local id = args['name'] or args['story'] or args['id'] or args[2];
   local d = Data[id];
   if not d then error('找不到故事文本!!!');end
    --- 构建HTML ---
    --- 构建HTML ---
   local title = args[1] or args['title'] or "标题";
    local box = mw.html.create("table"):addClass("rotable mw-collapsible mw-collapsed storyTable");
    local box = mw.html.create("table"):addClass("rotable mw-collapsible mw-collapsed storyTable");
    box:tag('tr'):tag('th'):wikitext(args['title'] or "标题");
    box:tag('tr'):tag('th'):tag('h4'):attr('id',id):wikitext(title);
   if args['autoUpload']then
     local rawTxt = mw.getCurrentFrame():expandTemplate{ title = 'Stories/' .. args['fName'] };
     local CNameTxt = mw.getCurrentFrame():expandTemplate{ title = 'Stories/CName' };
     local CName = {}; ---@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);
   local cPID = 'Dialogue';
        elseif b then summonTalkShow(tag,b);
   if d[1][1] == 0 then cPID = d[1][2];end
        else tag:tag("span"):addClass("storyError"):wikitext(string.format("警告:无法识别源代码!(arg=%d)",index)):done();
   local currentPrinter;
   local lastName = '';
   local canAppend = false;
   -- local _i = 0;
   for _, t in ipairs(d) do
     -- _i = _i + 1;
     if t[1] == 0 and t[2] ~= "" then
        cPID = t[2];
       currentPrinter=nil;
       canAppend=false;
     elseif t[1] == 1 then
       if not currentPrinter then currentPrinter = createPrinter(box,cPID);end
        if canAppend and t[3] == lastName then currentPrinter:AppendLine(reco(t[2] or ''));
       else currentPrinter:WriteLine(reco(t[2] or ''),t[3]);lastName = t[3];
       end;canAppend=nil;
     elseif t[1] == 2 then
       if canAppend == nil then canAppend = true;end
       if canAppend then currentPrinter:AppendLine('<br>');end
     elseif t[1] == 3 then
       if canAppend == nil then canAppend = true;end
     elseif t[1] == 4 then
       canAppend = false;
     elseif t[1] == 5 then
       canAppend = false;
       currentPrinter:Reset();
     elseif t[1] == 6 then
       if t[2] == '' then asmCNames[t[3]] = nil
       else asmCNames[t[3]] = t[2]
       end
      end
      end
    
     -- box:tag("span"):wikitext(type(index) .. arg):done()
    end
    end
    return tostring(box)
    return tostring(box)
end
end
return p
return p

2024年5月30日 (四) 11:40的最新版本

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

local p = {}

local CNames = require('Module:StorySim/CName').CNames
local getArgs = require('Module:Arguments').getArgs
local Data = mw.loadData('Module:RotaenoStoryData/FullText')
local asmCNames = {}

local function writeCName(node,id)
    if asmCNames[id] and CNames[id] then
        return node:tag('span'):addClass('name changedName name_'..id)
                    :tag('span'):wikitext(asmCNames[id]):done()
                    :tag('span'):wikitext(CNames[id]):done();
    elseif asmCNames[id] or CNames[id] then 
        return node:tag('span'):addClass('name name_'..id):wikitext(asmCNames[id] or CNames[id]);
    else return node:tag('span'):addClass('name UnknownName name_'..id):wikitext(id);
    end
end

local function _generic_AppendLine(self,txt)
    self.curCNode:wikitext(txt);
    return self;
end

local function _generic_Reset(self)
    self.curNode = self.node:tag('div'):addClass('SimChild');
    return self;
end

local _mtList = {
    ['Novel'] = {-- Novel
        WriteLine = function(self,txt,_)
            self.curCNode = self.curNode:tag('div'):wikitext(txt);
            return self;
        end;
        AppendLine = _generic_AppendLine,
        Reset = _generic_Reset
    },
    ['Chat'] = {-- Chat
        WriteLine = function(self,txt,name)
            if name == self.lastName then self.left = self.left;
            else self.left = not self.left;end
            self.lastName = name;

            local n = self.curNode:tag('tr');
            local l = n:tag('th'):addClass('L');
            self.curCNode = n:tag('td'):wikitext(txt);
            local r = n:tag('th'):addClass('R');
            if self.left then writeCName(l,name);self.curCNode:addClass('L')
            else writeCName(r,name);self.curCNode:addClass('R')
            end
            return self;
        end;
        AppendLine = _generic_AppendLine,
        Reset = function(self)
            if not self.init then
                self.init = true;
                self.left = false;
                self.curNode = self.node:tag('table'):addClass('SimChild');
            else self.curNode:tag('tr'):tag('th'):attr('colspan',3):addClass('LineBreak');
            end
        return self;
end
    },
    ['Dialogue'] = {-- Dialogue
        WriteLine = function(self,txt,name)
            local isThinking = false;
            if name=='' then isThinking = true;name='ilot';end
            local n = self.curNode:tag('tr');

            if name == self.lastName then
                self.left = self.left;
                self.i = self.i + 1;
                self.NameNode:attr('rowspan',self.i)
            else
                local l = n:tag('th'):addClass('L');writeCName(l,name);
                self.NameNode = l;
                self.lastName = name;
                self.i = 1;
            end

            self.curCNode = n:tag('td'):wikitext(txt);
            if isThinking then self.curCNode:addClass('thinking');end
            return self;
        end;
        AppendLine = _generic_AppendLine,
        Reset = function(self)
            if not self.init then
                self.init = true;
                self.curNode = self.node:tag('table'):addClass('SimChild');
            else self.curNode:tag('tr'):tag('th'):attr('colspan',2):addClass('LineBreak');
            end;self.i = 1;self.lastName=nil;self.NameNode = nil;
            return self;end
    },
    ['Fullscreen'] = {-- Fullscreen
        WriteLine = function(self,txt,_)
            self.curCNode = self.curNode:tag('div'):wikitext(txt);
            return self;end;
        AppendLine = _generic_AppendLine,
        Reset = _generic_Reset
    },
    ['FullscreenImportant'] = {-- FullscreenImportant
        WriteLine = function(self,txt,_)
            self.curCNode = self.curNode:wikitext(txt);
            return self;end;
        AppendLine = _generic_AppendLine,
        Reset = _generic_Reset
    },
    ['CgDialog'] = {-- CgDialog TBD
        WriteLine = function(self,txt,name)
            local N = self.curNode:tag('div'):addClass('DialogChild');
            if name ~= '' then local n = N:tag('div');writeCName(n,name);end
            self.curCNode = N:tag('div'):wikitext(txt);
            if name == '' then self.curCNode:addClass('thinking');end
            return self;
        end;
        AppendLine = _generic_AppendLine,
        Reset = _generic_Reset
    },
    ['ChapterTitle'] = {-- CT
        WriteLine = function(self,txt,_)
            local a,b = string.match(txt,'(.*)|(.*)');
            if a and b then 
                self.curNode:tag('div'):addClass('subTitle'):wikitext(a);
                self.curCNode = self.curNode:tag('div'):addClass('Title'):wikitext(b);
            else self.curCNode = self.curNode:tag('div'):addClass('onlyTitle'):wikitext(txt);
            end
            return self;
        end;
        AppendLine = _generic_AppendLine,
        Reset = _generic_Reset
},
}

--- @class Printer:table
--- @field WriteLine fun(self:Printer,txt:string,name:string)
--- @field AppendLine fun(self:Printer,txt:string)
--- @field Reset fun(self:Printer)
local _ = {}

---@param HTML any
---@param Type string
---@return Printer
local function createPrinter(HTML,Type)
    return setmetatable(
        {node = HTML:tag('tr'):tag('td'):addClass('storySim type_'..tostring(Type))},
        {__index = _mtList[Type]}):Reset();
end

--[[
qMTPList = {
    Novel = 0,
    Chat = 1, -- 对话(d)
    Dialogue = 2, -- 对话(vn)
    Fullscreen = 3, -- 全屏文字(例子:c1开头)
    FullscreenImportant = 4, -- 左下角文字(例子:c2结尾)
    CgDialog = 5 -- 带cg的对话(例子:ss2)
}

dC_qTable = {
    ModifyTextPrinter = {0,5},
    OverridePrintCommand = {1,5,6},
    OverrideAppendLineBreakCommand = {2},
    Wait = {3},
    HidePrinter = {4},
    ResetText = {5},
    OverrideDisplayNameCommand = {6,5,6}
}
]]

local function reco(str)
    while true do
        local s,e,c,t = string.find(str,'<color=(.-)>(.-)</color>');
        if s then str = string.sub(str,1,s-1) .. "<span class='Colored color_" .. c .. "' style='color:" .. c .. ";'>" .. t .. "</span>" .. string.sub(str,e+1)
        else break;
        end
    end
    while true do
        local s,e,c,t = string.find(str,'<size=(.-)>(.-)</size>');
        if s then str = string.sub(str,1,s-1) .. string.format("<span class='Sized size_%s' style='font-size:%.3fem;'>",c,(tonumber(c)/36)) .. t .. "</span>" .. string.sub(str,e+1)
        else break;
        end
    end
    local s,_,c,t = string.find(str,'<indent=(.-)%%>(.*)');
        if c then str = string.sub(str,1,s-1) .. string.format("<span class='Indented indent_%s' style='padding-left:%.3fem;'>",c,(tonumber(c)/5)) .. t .. "</span>"
        end
    return str;
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]]
    local id = args['name'] or args['story'] or args['id'] or args[2];
    local d = Data[id];
    if not d then error('找不到故事文本!!!');end
    --- 构建HTML ---
    local title = args[1] or args['title'] or "标题";
    local box = mw.html.create("table"):addClass("rotable mw-collapsible mw-collapsed storyTable");
    box:tag('tr'):tag('th'):tag('h4'):attr('id',id):wikitext(title);

    local cPID = 'Dialogue';
    if d[1][1] == 0 then cPID = d[1][2];end
    local currentPrinter;
    local lastName = '';
    local canAppend = false;
    -- local _i = 0;
    for _, t in ipairs(d) do
        -- _i = _i + 1;
        if t[1] == 0 and t[2] ~= "" then
            cPID = t[2];
            currentPrinter=nil;
            canAppend=false;
        elseif t[1] == 1 then
            if not currentPrinter then currentPrinter = createPrinter(box,cPID);end
            if canAppend and t[3] == lastName then currentPrinter:AppendLine(reco(t[2] or ''));
            else currentPrinter:WriteLine(reco(t[2] or ''),t[3]);lastName = t[3];
            end;canAppend=nil;
        elseif t[1] == 2 then
            if canAppend == nil then canAppend = true;end
            if canAppend then currentPrinter:AppendLine('<br>');end
        elseif t[1] == 3 then
            if canAppend == nil then canAppend = true;end
        elseif t[1] == 4 then
            canAppend = false;
        elseif t[1] == 5 then
            canAppend = false;
            currentPrinter:Reset();
        elseif t[1] == 6 then
            if t[2] == '' then asmCNames[t[3]] = nil
            else asmCNames[t[3]] = t[2]
            end
        end
    end
    return tostring(box)
end
return p