• Moegirl.ICU:萌娘百科流亡社群 581077156(QQ),欢迎对萌娘百科运营感到失望的编辑者加入
  • Moegirl.ICU:账号认领正在试运行,有意者请参照账号认领流程

Module:Sandbox/Chi ZJ2/var

萌娘百科,万物皆可萌的百科全书!转载请标注来源页面的网页链接,并声明引自萌娘百科。内容不可商用。
跳转到导航 跳转到搜索
Template-info.svg 模块文档  [创建] [刷新]
local p = {}

local format = string.format
local sub = string.sub
local match = string.match
local gmatch = string.gmatch
local insert = table.insert
local concat = table.concat
local pairs = pairs
local unstripNoWiki = mw.text.unstripNoWiki

local initNoWikiCount = mw.loadData('Module:Sandbox/Chi_ZJ2/var/init')[1]
local currNoWikiCount = 0

local function trim(str)
    return match(str, '^ *(.-) *$')
end

local function explode(str, sep)
    local result = {}
    for mat in gmatch(str .. sep, '(.-)' .. sep) do
        insert(result, mat)
    end
    return result
end

local function getNoWiki(from, to)
    local markers = {}
    for i = from, to do
        insert(markers, '\127\'"`UNIQ--nowiki-' .. format('%08X', i) .. '-QINU`"\'\127')
    end

    local resultString = unstripNoWiki(concat(markers, '\127SPLIT\127'))
    local results = explode(resultString, '\127SPLIT\127')
    return results
end

local function getVar(frame, key)
    local noWikiList = getNoWiki(initNoWikiCount, currNoWikiCount - 1)
    for i = #noWikiList, 1, -1 do
        local _, currKey, currVal = unpack(explode(noWikiList[i],  '\'"`'))
        if currKey == key then
            return currVal
        end
    end
end

function p.main(frame)
    currNoWikiCount = tonumber(sub(frame.args[1], 18, -10), 16)
    local args = frame:getParent().args
    return getVar(frame, trim(args[2]))
end

function p.init(frame)
    return ''
end

return p