Zum Inhalt springen

„Modul:Tutorials“ – Versionsunterschied

aus Wikipedia, der freien Enzyklopädie
[gesichtete Version][gesichtete Version]
Inhalt gelöscht Inhalt hinzugefügt
Wikipedia:Tutorial/Seiten/alles
Änderung 173683064 von Martin Kraft rückgängig gemacht;
Markierung: Rückgängigmachung
Zeile 10: Zeile 10:


local configData, structureData, templateData
local configData, structureData, templateData
local currentTitle, currentNode, refNode, currentPageNo, totalPageNo, lastNode, nextNode, isAllPage = false;
local currentTitle, currentNode, refNode, currentPageNo, totalPageNo, lastNode, nextNode
local inited = false
local inited = false


Zeile 38: Zeile 38:
end
end


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

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

return node;
end
function prepareNodeHtml(node, i, parentNode)
if node.isCurrentPage then
if node.isCurrentPage then
currentNode = node
node.innerHtml = templateData.threadNaviItemActive
node.innerHtml = templateData.threadNaviItemActive
else
else
Zeile 59: Zeile 45:
end
end


node.innerHtml = node.innerHtml:gsub("§§page§§", node.page)
local urlPrefix = isAllPage and '#' or ''
node.innerHtml = node.innerHtml:gsub("§§page§§", urlPrefix .. node.page)
node.innerHtml = node.innerHtml:gsub("§§name§§", node.name)
node.innerHtml = node.innerHtml:gsub("§§name§§", node.name)


Zeile 67: Zeile 52:
if node.children then
if node.children then
for childIndex, childNode in pairs( node.children ) do
for childIndex, childNode in pairs( node.children ) do
prepareNodeHtml(childNode, childIndex, node)
initNode(childNode, childIndex, node)
end
end
end
end
Zeile 79: Zeile 64:


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


return node;
return node;
end;
end
initNode(structureData, 0)
initNode(structureData, 0)
Zeile 114: Zeile 101:
end
end
end
end
end

if currentNode.isAllPage then
isAllPage = true
end
end
end
end
prepareNodeHtml(structureData, 0)
end
end


Zeile 193: Zeile 174:
function p.isAll(frame)
function p.isAll(frame)
init( frame.args[config] );
init( frame.args[config] );
return isAllPage
return currentNode and currentNode.isAllPage
end
end



Version vom 5. Februar 2018, 12:35 Uhr

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, lastNode, nextNode
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 not node.isCurrentPage and node.all then
            node.allTitle = mw.title.new(node.all)
            if mw.title.equals( currentTitle,  node.allTitle ) then 
                node.isCurrentPage = true
                node.isAllPage = true
            end
        end

        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
            lastNode = (currentPageNo==1) and currentNode.parent or currentNode.parent.children[currentPageNo-1]
            if currentPageNo < totalPageNo then
                nextNode = currentNode.parent.children[currentPageNo+1]
            end
        else
            refNode = currentNode
            if currentNode.childCount>0 then
                currentPageNo = 0
                totalPageNo = currentNode.childCount
                nextNode = currentNode.children[1]
            else
                if currentNode.parent then
                    totalPageNo = currentNode.parent.childCount
                    lastNode = (currentPageNo==1) and currentNode.parent or currentNode.parent.children[currentPageNo-1]
                    if currentPageNo < totalPageNo then
                        nextNode = currentNode.parent.children[currentPageNo+1]
                    end
                end
            end
        end
    end
end

function getPageContent( node, frame )
    if not node then 
        return ''
    end     
    local source, html
    source = node.title:getContent()
    html = frame:preprocess( tostring( mw.html.create( "div" ):wikitext( source ) ) )

    return '<h2>' .. node.name .. '</h2>' .. html
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 btnLast = ''
    if lastNode then
        btnLast = templateData.btnLast:gsub("§§page§§", lastNode.page ):gsub("§§name§§", lastNode.name )
    end 
    widgetHtml = widgetHtml:gsub("§§btnLast§§", btnLast )

    local btnNext = ''
    if nextNode then
        btnNext = templateData.btnNext:gsub("§§page§§", nextNode.page ):gsub("§§name§§", nextNode.name )
    end
    widgetHtml = widgetHtml:gsub("§§btnNext§§", btnNext )   

    local btnAll = ''
    if refNode.all then
        btnAll = templateData.btnAll:gsub("§§page§§", refNode.all )
    end
    widgetHtml = widgetHtml:gsub("§§btnAll§§", btnAll )  

    return widgetHtml
end

function p.all(frame)
    init( frame.args[config] );
    -- body
    local html
    if currentNode then

        html = '<div class="tutorial-article" style="margin-bottom:6em;">' .. getPageContent(refNode, frame) .. '</div>'
         
        for childIndex, childNode in pairs( refNode.children ) do
            html = html .. '<div class="tutorial-article" style="margin-bottom:6em;">' .. getPageContent(childNode, frame) .. '</div>'
        end

        return html
    else
        return "not found"
    end
end

function p.isAll(frame)
    init( frame.args[config] );
    return currentNode and currentNode.isAllPage
end

return p