Module:Update history: Difference between revisions

From Deadlock Wiki
Jump to navigation Jump to search
Mgpt (talk | contribs)
mNo edit summary
Mgpt (talk | contribs)
mNo edit summary
Line 22: Line 22:


function p.getRecentUpdates(frame)
function p.getRecentUpdates(frame)
     local pageName = mw.title.getCurrentTitle().baseText
     local title = mw.title.getCurrentTitle()
     local subpage = mw.title.getCurrentTitle().subpageText
    local pageName = title.baseText
     local subpage = title.subpageText
 
     local pageTitle
     local pageTitle
     if subpage == '' then
     if subpage == '' then

Revision as of 15:23, 7 October 2024

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 title = mw.title.getCurrentTitle()
    local pageName = title.baseText
    local subpage = title.subpageText

    local pageTitle
    if subpage == '' then
        pageTitle = pageName .. '/Update history'
    else
        pageTitle = pageName .. '/Update history/' .. subpage
    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