跳转到内容

模組:List extract

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

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

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+1)
        end
    until sid==nil
    for i = 1, #sections-1 do
        x=string.sub(content,sections[i],sections[i+1]-1)
        y={}
        y1=string.match(x,"==([^=][^\n]-[^=])==")
        y2=string.match(x,"==[^\n]-==([^=].*)")
        mw.logObject(y1)
        mw.logObject(y2)
        if y1 and y2 then
            y[1]=mw.text.trim(gsub(gsub(gsub(gsub(gsub(mw.text.trim(y1),"(.*)",""),"%[%[.-|(.-)%]%]","%1"),"%[",""),"%]",""),"<ref>.-</ref>",""))
            y[2]=gsub(gsub(gsub(mw.text.trim(y2),"<ref>.-</ref>",""),"{{[^{}]+}}",""),"*[^\n%[%]]\n","")
            table.insert(lists,y)
        end
    end
    return lists
end

function p.make_navbox(args)
    skip_group={["參見"]=1}
    args=args and (args.args or args) or {}
    sections=p.extract_sections(args.extract_title)
    new_args={}
    for k, v in pairs(args) do
        new_args[k]=v
    end
    for k, v in ipairs(sections) do
    	if not skip_group[v[1]] then
            new_args["group"..k]=v[1]
            new_args["list"..k]=v[2]
        end
    end
    return mw.getCurrentFrame():expandTemplate{ title = 'Navbox', args = new_args }
end

return p