Fandom Developers Wiki
mNo edit summary
mNo edit summary
Line 7: Line 7:
 
-- Generates dpl parser function from a key-value table
 
-- Generates dpl parser function from a key-value table
 
function p.tag(tag, tbl)
 
function p.tag(tag, tbl)
local buffer = {'{{#' .. tag .. ':\n'}
+
local buffer = {'{{#' .. tag .. '\n'}
   
 
for key, val in pairs(tbl) do
 
for key, val in pairs(tbl) do
Line 33: Line 33:
 
function p.parse(tbl)
 
function p.parse(tbl)
 
local frame = mw.getCurrentFrame()
 
local frame = mw.getCurrentFrame()
return frame:preprocess(p.tag('dpl', tbl))
+
return frame:preprocess(p.tag('dpl:', tbl))
 
end
 
end
   

Revision as of 23:49, 11 August 2020

butts lol


local p = {}
local getArgs = require('Dev:Arguments').getArgs

-- ExtDynamicPageList::$maxResultCount
p.COUNT = 500

-- Generates dpl parser function from a key-value table
function p.tag(tag, tbl)
    local buffer = {'{{#' .. tag .. '\n'}

    for key, val in pairs(tbl) do
        if type(val) == 'table' then
            for _, value in ipairs(val) do
                table.insert(buffer, '| ' .. key .. ' = ' .. value .. '\n')
           end
        else
            table.insert(buffer, '| ' .. key .. ' = ' .. val .. '\n')
        end
    end
    
    table.insert(buffer, '}}')

    return table.concat(buffer, '')
end

-- Gets a forum parser function and returns its wikitext
function p.forum(tbl)
    local frame = mw.getCurrentFrame()
    return frame:preprocess(p.tag('tag: forum', tbl))
end

-- Gets the DPL parser function and returns its wikitext after parsing
function p.parse(tbl)
    local frame = mw.getCurrentFrame()
    return frame:preprocess(p.tag('dpl:', tbl))
end

-- Returns all DPL results concatenated
-- The count parameter doesn't take effect
function p.recursive(tbl)
    tbl['noresultsheader'] = tbl['noresultsheader'] or '<no-results>'
    tbl['count'] = p.COUNT
    local offset = tbl['offset'] or 0
    local buffer = {}
    local parsed = p.parse(tbl)
    if parsed == tbl['noresultsheader'] then
        if tbl['noresultsheader'] == '<no-results>' then
            return ''
        else
            return tbl['noresultsheader']
        end
    end
    while parsed ~= tbl['noresultsheader'] do
        table.insert(buffer, parsed)
        offset = offset + p.COUNT
        tbl['offset'] = offset
        parsed = p.parse(tbl)
    end
    return table.concat(buffer)
end

-- Returns an array (really just a table) of all pages that match a query
-- The format parameter doesn't take effect
function p.list(tbl)
    tbl['format'] = ',,%PAGE%#,'
    local content = p.recursive(tbl)
    local results = {}
    for page in content:gmatch("[^#]+") do
        table.insert(results, page)
    end
    return results
end

-- For usage in #invoke
function p.main(frame)
    return p.parse(getArgs(frame))
end

-- For usage in #invoke
function p.all(frame)
    return p.recursive(getArgs(frame))
end

return p