跳转到内容

模組:List extract

维基百科,自由的百科全书

这是本页的一个历史版本,由GZWDer留言 | 贡献2023年3月11日 (六) 06:50编辑。这可能和当前版本存在着巨大的差异。

local p = {}
local gsub = mw.ustring.gsub

function p.extract_sections(title)
    local title = mw.title.new(title)
    local content = title:getContent()
    local lists={}
    local sections={}
    local sid=0
    repeat 
        sid=string.find( content, "==[^\n]-==", sid+1 )
        if sid~=nil then
            table.insert(sections,sid)
        end
    until sid==nil
    for i = 1, #sections-1 do
        x=string.sub(content,sections[i],sections[i+1]-1)
        y={}
        y[1]=gsub(gsub(gsub(gsub(gsub(string.match(x,"==([^\n]-)=="),"(.*)",""),"%[%[.-|(.-)%]%]","%1"),"%[",""),"%]",""),"<ref>.-</ref>","")
        y[2]=gsub(mw.text.trim(string.match(x,"==[^\n]-==(.*)")),"<ref>.-</ref>","")
        table.insert(lists,y)
    end
    return lists
end

function p.make_navbox(args)
    args=(args and (args.args or args)) or {}
    sections=p.extract_sections(args.extract_title)
    for k, v in ipairs(section) do
        args["group"..k]=v[1]
        args["list"..k]=v[2]
    end
    return frame:expandTemplate{ title = 'Navbox', args = args }
end

return p