Module:Update history

Revision as of 14:48, 7 October 2024 by Mixorio (talk | contribs)

Documentation for this module may be created at Module:Update history/doc

local p = {}
local mw = require('mw')

local function findBlockEnd(content, start_pos)
    local open_braces = 0
    local current_pos = start_pos

    while current_pos <= #content do
        local char = content:sub(current_pos, current_pos)
        if char == "{" then
            open_braces = open_braces + 1
        elseif char == "}" then
            open_braces = open_braces - 1
            if open_braces == 0 then
                return current_pos
            end
        end
        current_pos = current_pos + 1
    end
    return nil
end

function p.getRecentUpdates(frame)
    local pageName = mw.title.getCurrentTitle().text
    local pageTitle = ''
    if frame.args['pagename'] == nil or frame.args['pagename'] == '' then pageTitle = pageName .. "/Update history" else pageTitle = frame.args['pagename'] .. "/Update history" end

    local historyPage = mw.title.new(pageTitle)
    local pageContent = historyPage and historyPage:getContent() or ""

    local block_start = pageContent:find('{{Update history table/row')
    if not block_start then
        return ""
    end

    local blocks = {}
    local block_end
    local max_blocks = 3
    local block_count = 0

    while block_start and block_count < max_blocks do
        block_end = findBlockEnd(pageContent, block_start)
        if not block_end then
            break
        end

        local block_content = pageContent:sub(block_start, block_end)
        table.insert(blocks, block_content)
        block_count = block_count + 1

        if block_count < max_blocks then
            block_start = pageContent:find('{{Update history table/row', block_end + 1)
        else
            block_start = nil
        end
    end

    local processedBlocks = {}
    for _, block in ipairs(blocks) do
        local parsedBlock = frame:preprocess(block)
        table.insert(processedBlocks, parsedBlock)
    end

    local processedContent = table.concat(processedBlocks, '\n')
    return processedContent
end

return p