「Module:StorySim」:修訂間差異

增加 2,376 位元組 、​ 2023年12月21日 (星期四)
無編輯摘要
無編輯摘要
SSF留言 | 貢獻
無編輯摘要
第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('rowspan',3):addClass('LineBreak');
       end
     return self;
end
end
   },
   ['Dialogue'] = {-- Dialogue
     WriteLine = function(self,txt,name)
       local n = self.curNode:tag('tr');


local function summonTextShow(tag,txt)
       if name == self.lastName then
    for _, v in ipairs(getTextList(txt,"[/]","(.-)%[/%]")) do _summonTextShow(tag,v) end
         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);
       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('rowspan',2):addClass('LineBreak');
       end;self.i = 1;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:tag('div'):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');
       local n = N:tag('div');writeCName(n,name);
       self.curCNode = N:tag('div'):wikitext(txt);
       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
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
     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 --
-- Start --
第87行: 第161行:
p.main = makeInvokeFunc('_main')
p.main = makeInvokeFunc('_main')
function p._main(args)-- 生成HTML的主函数 部分代码来源:[[Module:Songtable]]
function p._main(args)-- 生成HTML的主函数 部分代码来源:[[Module:Songtable]]
   local d = Data[args['name'] or args['story'] or args['id'] or args[2]];
   if not d then error('找不到故事文本!!!');end
    --- 构建HTML ---
    --- 构建HTML ---
    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'):wikitext(args[1] or args['title'] or "标题");
    if args['autoUpload']then
 
     local rawTxt = mw.getCurrentFrame():expandTemplate{ title = 'Stories/' .. args['fName'] };
   local cPID = 'Dialogue';
     local CNameTxt = mw.getCurrentFrame():expandTemplate{ title = 'Stories/CName' };
    if d[1][1] == 0 then cPID = d[1][2];end
      local CName = setmetatable({},{
   local currentPrinter = createPrinter(box,cPID);
        __index = function(t,k)
   cPID = nil;
         local _,e,_ = string.find(k,"%.");
 
         if e then return rawget(t,string.sub(k,1,e-1));end
   local canAppend = false;
         return nil;
   local _i = 0;
   for _, t in ipairs(d) do
     _i = _i + 1;
      if t[1] == 0 and _i ~= 1 then currentPrinter=createPrinter(box,t[2]); canAppend=false;
     elseif t[1] == 1 then
       if canAppend then currentPrinter:AppendLine(t[2]);
        else currentPrinter:WriteLine(t[2],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[3] == '' then asmCNames[t[2]] = nil
       else asmCNames[t[3]] = t[2]
        end
        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
      end
    
     -- box:tag("span"):wikitext(type(index) .. arg):done()
    end
    end
    return tostring(box)
    return tostring(box)
end
end
return p
return p
WikiBuilder
329

次編輯