跳转到内容

模組:Recent Records in New Page

本页使用了标题或全文手工转换
维基百科,自由的百科全书

--- Fetch recent new page records for the last n days.
---
--- All first-level unordered list items are considered (i.e., lines 
--- that begin with ``*`` and are not immediately followed by another
--- ``*``). Bullet items other than new page records should use 
--- ``{{Bulleted list}}`` or so for handling.

require("strict")

local getArgs = require("Module:Arguments").getArgs

--- Filter the first n lines of first-level unordered list items.
--- @param text string @Wikitext to filter.
--- @param n number @Maximum number of lines to keep.
--- @return string @Filtered and parsed Wikitext.
local function filter_n(text, n)
    local lines = {}
    for line in mw.text.gsplit(text, "\n", true) do
        if n <= 0 then
            break
        end
        if mw.ustring.find(line, "^%*%f[^*]") then
            table.insert(lines, line)
            n = n - 1
        end
    end
    local raw_code = table.concat(lines, "\n")
    return mw.getCurrentFrame():preprocess(raw_code)
end
local p = {}

local function makeInvokeFunc(funcName)
    return function(frame)
        local args = getArgs(frame)
        return p[funcName](args)
    end
end

p.main = makeInvokeFunc("_main")
function p._main(args)
    local text = mw.title.new(args.page):getContent()
    local n = tonumber(args[1])
    return filter_n(text, n)
end

return p