Zum Inhalt springen

Modul:Tutorials

aus Wikipedia, der freien Enzyklopädie
Dies ist eine alte Version dieser Seite, zuletzt bearbeitet am 4. Februar 2018 um 19:20 Uhr durch Martin Kraft (Diskussion | Beiträge). Sie kann sich erheblich von der aktuellen Version unterscheiden.

Die Dokumentation für dieses Modul kann unter Modul:Tutorials/Doku erstellt werden

-- ## Modul:Tutorials is a Lua Libary to manage Wikipedia-Tutorials ##
-- Please use this module via Vorlage:Tutorials only!
-- Modul:Tutorials requires a customisable config.json, passed via the parameter config to each function call
-- The Default is Wikipedia:Tutorial/config.json


-- Helper Functions

-- Local functions and vars

local configData, structureData, templateData
local currentTitle, currentNode, refNode, currentPageNo, totalPageNo
local inited = false

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

    currentTitle = mw.title.getCurrentTitle()       

    if not configPage then configPage = 'Wikipedia:Tutorial/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 style="margin-left:2.2ex;">' .. 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)

    refNode = structureData
    currentPageNo = 0
    totalPageNo = 0
    if currentNode then
        currentPageNo = currentNode.index
        if currentNode.parent and currentNode.childCount==0 then
            refNode = currentNode.parent
            totalPageNo = currentNode.parent.childCount
        else
            refNode = currentNode
            if currentNode.childCount>0 then
                currentPageNo = 0
                totalPageNo = currentNode.childCount
            else
                if currentNode.parent then
                    totalPageNo = currentNode.parent.childCount
                end
            end
        end
    end
end

-- Public Interface

local p = {} 

function p.navIndex(frame)
    init( frame.args[config] );
    return refNode.childHtml
end

function p.navWidget(frame)
    init( frame.args[config] );

    local pagina = currentPageNo .. '/' .. totalPageNo
    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 totalPageNo > 0 then
        if currentPageNo > 1 then
            linkedNode = currentNode.parent.children[currentPageNo-1]
            btnLast = templateData.btnLast:gsub("§§page§§", linkedNode.page ):gsub("§§name§§", linkedNode.name )
        else 
            if currentPageNo == 1 then
                linkedNode = currentNode.parent
                btnLast = templateData.btnLast:gsub("§§page§§", linkedNode.page ):gsub("§§name§§", linkedNode.name )
            end
        end

        if currentPageNo < totalPageNo then
            linkedNode = currentNode.parent.children[currentPageNo+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

return p