Die Dokumentation für dieses Modul kann unter Modul:Benutzer:Martin Kraft/Doku erstellt werden

local p = {} 

-- Local functions and vars

local configData, structureData, templateData
local currentTitle, currentNode
local inited = false

function init(configPage)
    if inited then return end
    inited = true

    currentTitle = mw.title.getCurrentTitle()       

    if not configPage then configPage = 'Benutzer:Martin Kraft/LuaTest/config.json' end
    configData = mw.text.jsonDecode( mw.title.new(configPage):getContent() )
    structureData = configData.structure
    templateData = configData.templates

    function initNode(node, i, parentNode)
        node.index = i
        node.childCount = 0
        node.title = mw.title.new(node.page)
        node.isCurrentPage = mw.title.equals( currentTitle, node.title )
        if node.isCurrentPage then
            currentNode = node
            node.innerHtml = templateData.threadNaviItemActive
        else
            node.innerHtml = templateData.threadNaviItem
        end

        node.innerHtml = node.innerHtml:gsub("§§page§§", node.page)
        node.innerHtml = node.innerHtml:gsub("§§name§§", node.name)

        node.childHtml = ''
        
        if node.children then 
            for childIndex, childNode in pairs( node.children ) do
                initNode(childNode, childIndex, node)
            end
        end

        if node.childHtml == '' then
            node.outerHtml = '<li>' .. node.innerHtml .. '</li>'
        else
            node.childHtml = '<ol>' .. node.childHtml .. '</ol>'
            node.outerHtml = '<li>' .. node.innerHtml .. node.childHtml .. '</li>'
        end

        if parentNode then
            node.parent = parentNode
            parentNode.childCount = i
            parentNode.childHtml = parentNode.childHtml .. node.outerHtml
        end        

        return node;
    end
        
    initNode(structureData, 0)
end

-- Interface

function p.pagina(frame)
    init();

    local page, totalPages, refNode = structureData
    if currentNode then
        page = currentNode.index
        if currentNode.parent then
            refNode = currentNode.parent
            totalPages = currentNode.parent.childCount
        else
            refNode = currentNode
        end
    end

    local pagina = page .. '/' .. totalPages
    if templateData.paginaDelimiter then pagina = pagina:gsub("/", templateData.paginaDelimiter) end
    local widgetHtml = templateData.widget:gsub("§§threadNavi§§", refNode.innerHtml .. refNode.childHtml )
    widgetHtml = widgetHtml:gsub("§§pagina§§", pagina )

    local btnNext = ''
    local btnLast = ''
    local linkedNode

    if totalPages then
        if page > 1 then
            linkedNode = currentNode.parent.children[page-1]
            btnLast = templateData.btnLast:gsub("§§page§§", linkedNode.page ):gsub("§§name§§", linkedNode.name )
        else
            linkedNode = currentNode.parent
            btnLast = templateData.btnLast:gsub("§§page§§", linkedNode.page ):gsub("§§name§§", linkedNode.name )
        end

        if page < totalPages then
            linkedNode = currentNode.parent.children[page+1]
            btnNext = templateData.btnNext:gsub("§§page§§", linkedNode.page ):gsub("§§name§§", linkedNode.name )
        end
    end

    widgetHtml = widgetHtml:gsub("§§btnNext§§", btnNext )    
    widgetHtml = widgetHtml:gsub("§§btnLast§§", btnLast )

    return widgetHtml
end



function p.test(frame)
    return mw.title.getCurrentTitle():partialUrl()
end

return p