Module:Update history

From Deadlock Wiki
Revision as of 23:27, 15 September 2024 by Mgpt (talk | contribs) (Created page with "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...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

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 = pageName .. "/Update history"

    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 = 4
    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