跳转到内容

模組:List extract

维基百科,自由的百科全书
文档图示 模块文档[创建]
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][^\n]-[^=\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]-==([^=].*)")
        if y1 and y2 then
            y[1]=mw.text.trim(gsub(gsub(gsub(gsub(gsub(mw.text.trim(y1),"(.*)",""),"%[%[.-|(.-)%]%]","%1"),"%[",""),"%]",""),"<ref>.-</ref>",""))
            y[2]=mw.text.trim(gsub(gsub(gsub(gsub(gsub(mw.text.trim(y2),"<ref.-</ref>",""),"{{box|([^{}]+)}}","%1"),"{{f?o?n?t?color|[^{}]+|([^{}]+)}}","%1"),"{{[^{}]+}}",""),"*[^\n%[%]]*\n",""))
            table.insert(lists,y)
        end
    end
    return lists
end

function p.do_extract_section(title, section, section2)
    sections=p.extract_sections(title)
    for k, v in ipairs(sections) do
        if string.match(v[1],section) then
            return v[2]
        end
        if section2 and string.match(v[1],section2) then
            return v[2]
        end
    end
end

function p.extract_section(args)
    args=args and (args.args or args) or {}
    return p.do_extract_section(args.extract_title, args.section, args.section2)
end

function p.extract_section_no_table(args)
    args=args and (args.args or args) or {}
    cur_section=p.do_extract_section(args.extract_title, args.section, args.section2)
    if string.match(cur_section,"{|") and string.match(cur_section,"wikitable") then
    	results={}
    	mw.log(cur_section)
        for i in string.gmatch(cur_section,"|%-[^\r\n]*[ \r\n\t]*|[ \r\n\t]*(%[%[[^%]]*%]%])") do
            table.insert(results,i)
        end
        return table.concat(results,"、")
    end
    return cur_section
end

function p.make_navbox(args)
    args=args and (args.args or args) or {}
    skip_group=(args.skip_group and mw.text.split(args.skip_group,",")) or {"參見"}
    skip_group_len=#skip_group
    for i=1, skip_group_len do
        skip_group[skip_group[i]]=1
        skip_group[i]=nil
    end
    skip_pattern=args.skip_pattern
    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]]) and (not (skip_pattern and string.match(v[1],skip_pattern))) 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