模块:切换显示:修订间差异

来自SS唯基
跳到导航 跳到搜索
无编辑摘要
无编辑摘要
第4行: 第4行:
   
   
local initSpan = function(className, templateName, frame)
local initSpan = function(className, templateName, frame)
local args = getArgs(frame, { wrappers = 'Template:' .. templateName })
    mw.log.warn("Module:切换显示 called")
    mw.log.warn(className)
    local args = getArgs(frame, { wrappers = 'Template:' .. templateName })
     local parent_args = frame:getParent().args;
     local parent_args = frame:getParent().args;
   
   
local span = mw.html.create('span')
    local span = mw.html.create('span')
span:attr('class', className)
    span:attr('class', className)
   
   
if className == 'textToggleDisplayButtons' then -- 自Template:切换显示调用。
    if className == 'textToggleDisplayButtons' then -- 自Template:切换显示调用。
local operators = {} -- 操作参数。
        local operators = {} -- 操作参数。
local contentlist = {} --[[内容表列。结构为:
        local contentlist = {} --[[内容表列。结构为:
{
                                                {
strcode = 字符串代号,
                                                    strcode = 字符串代号,
on = @on后缀的内容,
                                                    on = @on后缀的内容,
off = @off后缀的内容,
                                                    off = @off后缀的内容,
default = 没有@on或@off后缀的内容,
                                                    default = 没有@on或@off后缀的内容,
}
                                                }
]]
                                            ]]
for k, v in pairs(args) do
        for k, v in pairs(args) do
mw.log(k, v)
            mw.log(k, v)
if mw.ustring.sub(k, 1, 1) == '@' then
            if mw.ustring.sub(k, 1, 1) == '@' then
operators[k] = v
                operators[k] = v
else
            else
local strcode
                local strcode
local onoff = nil
                local onoff = nil
-- 获取字符串代号和开关类型。
                -- 获取字符串代号和开关类型。
if mw.ustring.sub(k, -3) == '@on' then
                if mw.ustring.sub(k, -3) == '@on' then
strcode = mw.ustring.sub(k, 1, mw.ustring.len(k) - 3)
                    strcode = mw.ustring.sub(k, 1, mw.ustring.len(k) - 3)
onoff = 'on'
                    onoff = 'on'
elseif mw.ustring.sub(k, -4) == '@off' then
                elseif mw.ustring.sub(k, -4) == '@off' then
strcode = mw.ustring.sub(k, 1, mw.ustring.len(k) - 4)
                    strcode = mw.ustring.sub(k, 1, mw.ustring.len(k) - 4)
onoff = 'off'
                    onoff = 'off'
else
                else
strcode = k
                    strcode = k
end
                end
onoff = onoff or 'default' -- 没有开关类型就是默认值。
                onoff = onoff or 'default' -- 没有开关类型就是默认值。
   
   
if not contentlist[strcode] then
                if not contentlist[strcode] then
table.insert(contentlist, {strcode = strcode}) -- 增加空的内容表。
                    table.insert(contentlist, {strcode = strcode}) -- 增加空的内容表。
contentlist[strcode] = #contentlist -- 定位新增加的索引,绑定到参数k。
                    contentlist[strcode] = #contentlist -- 定位新增加的索引,绑定到参数k。
end
                end
contentlist[contentlist[strcode]][onoff] = v -- 替换内容。
                contentlist[contentlist[strcode]][onoff] = v -- 替换内容。
end
            end
end
        end
   
   
local counter = 0 -- 计数器。
        local counter = 0 -- 计数器。
for k_operators, v_operators in pairs(operators) do
        for k_operators, v_operators in pairs(operators) do
span:attr('data-key-' .. counter, k_operators):attr('data-value-' .. counter, v_operators)
            span:attr('data-key-' .. counter, k_operators):attr('data-value-' .. counter, v_operators)
counter = counter + 1
            counter = counter + 1
end
        end
   
   
for index, content in ipairs(contentlist) do
        for index, content in ipairs(contentlist) do
span:tag('span'):attr('data-order', index - 1):attr('data-key', content.strcode):tag('span'):addClass(
            span:tag('span'):attr('data-order', index - 1):attr('data-key', content.strcode):tag('span'):addClass(
'textToggleDisplayButtonLabelText on'
                'textToggleDisplayButtonLabelText on'
):wikitext(frame:preprocess(content.on or content.default)):done():tag('span'):addClass(
            ):wikitext(frame:preprocess(content.on or content.default)):done():tag('span'):addClass(
'textToggleDisplayButtonLabelText off'
                'textToggleDisplayButtonLabelText off'
):wikitext(frame:preprocess(content.off or content.default))
            ):wikitext(frame:preprocess(content.off or content.default))
end
        end
else
    else
local counter = 0 -- 计数器
        local counter = 0 -- 计数器
for k, v in pairs(args) do
        for k, v in pairs(args) do
if v ~= nil then
            if v ~= nil then
span:attr('data-key-' .. counter, k):attr('data-value-' .. counter, v)
                span:attr('data-key-' .. counter, k):attr('data-value-' .. counter, v)
counter = counter + 1
                counter = counter + 1
end
            end
end
        end
end
    end
   
   
return tostring(span)
    return tostring(span)
end
end
   
   
function module.template(frame)
function module.template(frame)
local args = getArgs(frame, { wrappers = 'Template:切换显示/模板' })
    local args = getArgs(frame, { wrappers = 'Template:切换显示/模板' })
   
   
local tTitle = args['@'] or ''
    local tTitle = args['@'] or ''
if tTitle == '' then
    if tTitle == '' then
return nil
        return nil
end
    end
local tag = args['@tag']
    local tag = args['@tag']
   
   
local branches = {}
    local branches = {}
local params = {}
    local params = {}
for k, v in args do
    for k, v in args do
if not mw.ustring.match(k, '^@[^@]') then
        if not mw.ustring.match(k, '^@[^@]') then
local i = 1
            local i = 1
while true do
            while true do
local _, j, flag = mw.ustring.find(k, '@(.)', i)
                local _, j, flag = mw.ustring.find(k, '@(.)', i)
if j == nil then
                if j == nil then
i = mw.ustring.len(k)
                    i = mw.ustring.len(k)
break
                    break
elseif flag == '@' then
                elseif flag == '@' then
i = j + 1
                    i = j + 1
else
                else
i = j - 2
                    i = j - 2
break
                    break
end
                end
end
            end
   
   
local param = mw.text.trim(mw.ustring.gsub(mw.ustring.sub(k, 1, i), '@@', '@'))
            local param = mw.text.trim(mw.ustring.gsub(mw.ustring.sub(k, 1, i), '@@', '@'))
local branch = mw.ustring.sub(k, i + 2)
            local branch = mw.ustring.sub(k, i + 2)
if mw.text.trim(param) ~= '' then
            if mw.text.trim(param) ~= '' then
if params[param] == nil then
                if params[param] == nil then
params[param] = {}
                    params[param] = {}
end
                end
if branch ~= '' and branches[branch] == nil then
                if branch ~= '' and branches[branch] == nil then
branches[branch] = #branches + 1
                    branches[branch] = #branches + 1
table.insert(branches, branch)
                    table.insert(branches, branch)
end
                end
if branch == '' then
                if branch == '' then
params[param][0] = v
                    params[param][0] = v
else
                else
params[param][branch] = v
                    params[param][branch] = v
end
                end
end
            end
end
        end
end
    end
   
   
local result = {}
    local result = {}
for _, branch in ipairs(branches) do
    for _, branch in ipairs(branches) do
if type(branch) == 'string' then
        if type(branch) == 'string' then
local tArgs = {}
            local tArgs = {}
for param, bValue in pairs(params) do
            for param, bValue in pairs(params) do
tArgs[param] = bValue[branch] or bValue[0]
                tArgs[param] = bValue[branch] or bValue[0]
end
            end
   
   
table.insert(
            table.insert(
result,
                result,
frame:expandTemplate {
                frame:expandTemplate {
title = '切换显示',
                    title = '切换显示',
args = {
                    args = {
[1] = branch,
                        [1] = branch,
[2] = frame:expandTemplate {title = tTitle, args = tArgs},
                        [2] = frame:expandTemplate {title = tTitle, args = tArgs},
[3] = tag
                        [3] = tag
}
                    }
}
                }
)
            )
end
        end
end
    end
   
   
return table.concat(result)
    return table.concat(result)
end
end
   
   
function module.button(frame)
function module.button(frame)
return initSpan('textToggleDisplayButtons', '切换显示按钮', frame)
    return initSpan('textToggleDisplayButtons', '切换显示按钮', frame)
end
end
   
   
function module.style(frame)
function module.style(frame)
return initSpan('textToggleDisplayStyle', '切换显示样式', frame)
    return initSpan('textToggleDisplayStyle', '切换显示样式', frame)
end
end
   
   
function module.buttonStyle(frame)
function module.buttonStyle(frame)
return initSpan('textToggleDisplayButtonsStyle', '切换显示按钮样式', frame)
    return initSpan('textToggleDisplayButtonsStyle', '切换显示按钮样式', frame)
end
end
   
   
return module
return module

2022年5月4日 (三) 13:48的版本

此模块的文档可以在模块:切换显示/doc创建

local module = {}
 
local getArgs = require('Module:Arguments').getArgs
 
local initSpan = function(className, templateName, frame)
    mw.log.warn("Module:切换显示 called")
    mw.log.warn(className)
    local args = getArgs(frame, { wrappers = 'Template:' .. templateName })
    local parent_args = frame:getParent().args;
 
    local span = mw.html.create('span')
    span:attr('class', className)
 
    if className == 'textToggleDisplayButtons' then -- 自Template:切换显示调用。
        local operators = {} -- 操作参数。
        local contentlist = {} --[[内容表列。结构为:
                                                {
                                                    strcode = 字符串代号,
                                                    on = @on后缀的内容,
                                                    off = @off后缀的内容,
                                                    default = 没有@on或@off后缀的内容,
                                                }
                                            ]]
        for k, v in pairs(args) do
            mw.log(k, v)
            if mw.ustring.sub(k, 1, 1) == '@' then
                operators[k] = v
            else
                local strcode
                local onoff = nil
                -- 获取字符串代号和开关类型。
                if mw.ustring.sub(k, -3) == '@on' then
                    strcode = mw.ustring.sub(k, 1, mw.ustring.len(k) - 3)
                    onoff = 'on'
                elseif mw.ustring.sub(k, -4) == '@off' then
                    strcode = mw.ustring.sub(k, 1, mw.ustring.len(k) - 4)
                    onoff = 'off'
                else
                    strcode = k
                end
                onoff = onoff or 'default' -- 没有开关类型就是默认值。
 
                if not contentlist[strcode] then
                    table.insert(contentlist, {strcode = strcode}) -- 增加空的内容表。
                    contentlist[strcode] = #contentlist -- 定位新增加的索引,绑定到参数k。
                end
                contentlist[contentlist[strcode]][onoff] = v -- 替换内容。
            end
        end
 
        local counter = 0 -- 计数器。
        for k_operators, v_operators in pairs(operators) do
            span:attr('data-key-' .. counter, k_operators):attr('data-value-' .. counter, v_operators)
            counter = counter + 1
        end
 
        for index, content in ipairs(contentlist) do
            span:tag('span'):attr('data-order', index - 1):attr('data-key', content.strcode):tag('span'):addClass(
                'textToggleDisplayButtonLabelText on'
            ):wikitext(frame:preprocess(content.on or content.default)):done():tag('span'):addClass(
                'textToggleDisplayButtonLabelText off'
            ):wikitext(frame:preprocess(content.off or content.default))
        end
    else
        local counter = 0 -- 计数器
        for k, v in pairs(args) do
            if v ~= nil then
                span:attr('data-key-' .. counter, k):attr('data-value-' .. counter, v)
                counter = counter + 1
            end
        end
    end
 
    return tostring(span)
end
 
function module.template(frame)
    local args = getArgs(frame, { wrappers = 'Template:切换显示/模板' })
 
    local tTitle = args['@'] or ''
    if tTitle == '' then
        return nil
    end
    local tag = args['@tag']
 
    local branches = {}
    local params = {}
    for k, v in args do
        if not mw.ustring.match(k, '^@[^@]') then
            local i = 1
            while true do
                local _, j, flag = mw.ustring.find(k, '@(.)', i)
                if j == nil then
                    i = mw.ustring.len(k)
                    break
                elseif flag == '@' then
                    i = j + 1
                else
                    i = j - 2
                    break
                end
            end
 
            local param = mw.text.trim(mw.ustring.gsub(mw.ustring.sub(k, 1, i), '@@', '@'))
            local branch = mw.ustring.sub(k, i + 2)
            if mw.text.trim(param) ~= '' then
                if params[param] == nil then
                    params[param] = {}
                end
                if branch ~= '' and branches[branch] == nil then
                    branches[branch] = #branches + 1
                    table.insert(branches, branch)
                end
                if branch == '' then
                    params[param][0] = v
                else
                    params[param][branch] = v
                end
            end
        end
    end
 
    local result = {}
    for _, branch in ipairs(branches) do
        if type(branch) == 'string' then
            local tArgs = {}
            for param, bValue in pairs(params) do
                tArgs[param] = bValue[branch] or bValue[0]
            end
 
            table.insert(
                result,
                frame:expandTemplate {
                    title = '切换显示',
                    args = {
                        [1] = branch,
                        [2] = frame:expandTemplate {title = tTitle, args = tArgs},
                        [3] = tag
                    }
                }
            )
        end
    end
 
    return table.concat(result)
end
 
function module.button(frame)
    return initSpan('textToggleDisplayButtons', '切换显示按钮', frame)
end
 
function module.style(frame)
    return initSpan('textToggleDisplayStyle', '切换显示样式', frame)
end
 
function module.buttonStyle(frame)
    return initSpan('textToggleDisplayButtonsStyle', '切换显示按钮样式', frame)
end
 
return module