Zum Inhalt springen

„Modul:Tutorials“ – Versionsunterschied

aus Wikipedia, der freien Enzyklopädie
[gesichtete Version][gesichtete Version]
Inhalt gelöscht Inhalt hinzugefügt
Table-Bug behoben
generalisiert
Zeile 3: Zeile 3:
-- Modul:Tutorials requires a customisable config.json, passed via the parameter config to each function call
-- Modul:Tutorials requires a customisable config.json, passed via the parameter config to each function call
-- The Default is Wikipedia:Tutorial/config.json
-- The Default is Wikipedia:Tutorial/config.json



-- Helper Functions
-- Helper Functions
Zeile 9: Zeile 8:
-- Local functions and vars
-- Local functions and vars


local configData, structureData, templateData
local configData, templateData, rootNode
local currentTitle, currentNode, refNode, currentPageNo, totalPageNo, lastNode, nextNode, isAllPage, isLastNode = false
local currentTitle, currentNode, refNode, lastNode, nextNode
local isAllPage
local currentPageNo = 0
local totalPageNo = 0
local isLastNode = false
local showHidden = false
local inited = false
local inited = false


Zeile 17: Zeile 21:
inited = true
inited = true


--mw.log('Lua Init')
currentTitle = mw.title.getCurrentTitle()


currentTitle = mw.title.getCurrentTitle()
if not configPage then configPage = 'Wikipedia:Tutorial/config.json' end
if not configPage then configPage = 'Wikipedia:Tutorial/config.json' end
configData = mw.text.jsonDecode( mw.title.new(configPage):getContent() )
configData = mw.text.jsonDecode( mw.title.new(configPage):getContent() )

structureData = configData.structure
rootNode = configData.structure
refNode = rootNode
templateData = configData.templates
templateData = configData.templates


local previousNode
function initNode(node, i, parentNode)
function initNode(node, i, parentNode)
node.index = i
node.index = i
Zeile 29: Zeile 37:
node.title = mw.title.new(node.page)
node.title = mw.title.new(node.page)
node.isCurrentPage = mw.title.equals( currentTitle, node.title )
node.isCurrentPage = mw.title.equals( currentTitle, node.title )
node.containsCurrentPage = node.isCurrentPage;
node.containsCurrentPage = node.isCurrentPage
--node.isAllPage = false
node.isLastNode = false


if not node.isCurrentPage and node.all then
if not node.isCurrentPage and node.all then
Zeile 38: Zeile 48:
end
end
end
end

if previousNode then
previousNode.nextNode = node
node.lastNode = previousNode
--[[ -- Funktioniert noch nicht
if previousNode.parent == node.parent or previousNode ~= node.parent then
else
previousNode.isLastNode = true
end
]]--
end
previousNode = node


if node.children then
if node.children then
Zeile 49: Zeile 71:
node.containsCurrentPage = true
node.containsCurrentPage = true
end
end
if not (childNode.hidden and childNode.containsCurrentPage == false) then
if not (childNode.hidden and childNode.containsCurrentPage == false) or showHidden then
node.childCount = node.childCount + 1
node.childCount = node.childCount + 1
newChildrenArray[node.childCount] = childNode
newChildrenArray[node.childCount] = childNode
Zeile 55: Zeile 77:
end
end
node.children = newChildrenArray
node.children = newChildrenArray
end
end


node.refNode = node;
if parentNode then
if parentNode then
node.parent = parentNode
node.parent = parentNode

if node.childCount == 0 then
node.refNode = node.parent;
end
end
end
if node.isCurrentPage then
if node.isCurrentPage then
currentNode = node
currentNode = node
end

if node.refNode == node then
node.isRefNode = true
end
end


Zeile 103: Zeile 134:
end;
end;
initNode(structureData, 0)
initNode(rootNode, 0, false)


if not currentNode then
refNode = structureData
currentPageNo = 0
currentNode = rootNode;
totalPageNo = 0
end
if currentNode then
currentPageNo = currentNode.index
if currentNode.parent and currentNode.childCount==0 then
-- Subpages
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]
else
isLastNode = true
-- Search next Tutorial
end
else
-- Mainpages
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


if currentNode.isAllPage then
refNode = currentNode.refNode
totalPageNo = refNode.childCount
isAllPage = true
currentPageNo = currentNode.index
end
isLastNode = currentNode.isLastNode
end
isAllPage = currentNode.isAllPage
nextNode = currentNode.nextNode
lastNode = currentNode.lastNode
prepareNodeHtml(structureData, 0)
prepareNodeHtml(rootNode, 0)
end
end


Zeile 163: Zeile 167:


function p.navIndex(frame)
function p.navIndex(frame)
mw.log('Lua navIndex ', frame)
init( frame.args[config] );
init( frame.args[config] );
return refNode.childHtml
return refNode.childHtml
end
end


function p.navWidget(frame)
function p.navWidget(frame)
mw.log('Lua navWidget ', frame)
init( frame.args[config] );
init( frame.args[config] );


local pagina, btnHtml;
local node = currentNode
local pagina, btnHtml
if isAllPage then

if node.isAllPage == true then
pagina = templateData.paginaAll:gsub("§§page§§", refNode.page);
pagina = templateData.paginaAll:gsub("§§page§§", refNode.page);
else
else
if currentPageNo == 0 then
pagina = templateData.pagina

if node.isRefNode == true then
pagina = templateData.paginaStart
pagina = templateData.paginaStart
else
else
pagina = templateData.pagina
pagina = templateData.pagina
end
end
pagina = pagina:gsub("§§totalPageNo§§", totalPageNo ):gsub("§§currentPageNo§§", currentPageNo )
pagina = pagina:gsub("§§totalPageNo§§", node.refNode.childCount ):gsub("§§currentPageNo§§", node.index )
btnHtml = ''
btnHtml = ''
if lastNode then
mw.log('node.lastNode:',node,'.',node.lastNode,'|',previousNode)
btnHtml = templateData.btnLast:gsub("§§page§§", lastNode.page ):gsub("§§name§§", lastNode.name )
if node.lastNode then
btnHtml = templateData.btnLast:gsub("§§page§§", node.lastNode.page ):gsub("§§name§§", node.lastNode.name )
end
end
pagina = pagina:gsub("§§btnLast§§", btnHtml )
pagina = pagina:gsub("§§btnLast§§", btnHtml )
btnHtml = ''
btnHtml = ''
if nextNode then
mw.log('node.nextNode:',node,'.',node.nextNode)
btnHtml = templateData.btnNext:gsub("§§page§§", nextNode.page ):gsub("§§name§§", nextNode.name )
if not node.isLastNode and node.nextNode then
btnHtml = templateData.btnNext:gsub("§§page§§", node.nextNode.page ):gsub("§§name§§", node.nextNode.name )
end
end
pagina = pagina:gsub("§§btnNext§§", btnHtml )
pagina = pagina:gsub("§§btnNext§§", btnHtml )
btnHtml = ''
btnHtml = ''
if refNode.all then
if node.refNode.all then
btnHtml = templateData.btnAll:gsub("§§page§§", refNode.all )
btnHtml = templateData.btnAll:gsub("§§page§§", node.refNode.all )
end
end

pagina = pagina:gsub("§§btnAll§§", btnHtml )
pagina = pagina:gsub("§§btnAll§§", btnHtml )
end
end


local widgetHtml = templateData.widget:gsub("§§rootPage§§",structureData.page):gsub("§§threadNavi§§", refNode.innerHtml .. refNode.childHtml ):gsub("§§pagina§§", pagina )
local widgetHtml = templateData.widget:gsub("§§rootPage§§",rootNode.page):gsub("§§threadNavi§§", node.refNode.innerHtml .. node.refNode.childHtml ):gsub("§§pagina§§", pagina )


return widgetHtml
return widgetHtml
Zeile 205: Zeile 218:


function p.all(frame)
function p.all(frame)
mw.log('Lua all ', frame)
init( frame.args[config] );
init( frame.args[config] );
-- body
-- body
Zeile 223: Zeile 237:


function p.isAll(frame)
function p.isAll(frame)
mw.log('Lua isAll ', frame)
init( frame.args[config] );
init( frame.args[config] );
mw.log('Lua isAll -> ', isAllPage)
return isAllPage
return isAllPage
end
end

Version vom 10. Juni 2018, 01:18 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, templateData, rootNode
local currentTitle, currentNode, refNode, lastNode, nextNode 
local isAllPage
local currentPageNo = 0
local totalPageNo = 0
local isLastNode = false
local showHidden = false
local inited = false

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

    --mw.log('Lua Init')

    currentTitle = mw.title.getCurrentTitle()       
    if not configPage then configPage = 'Wikipedia:Tutorial/config.json' end
    configData = mw.text.jsonDecode( mw.title.new(configPage):getContent() )

    rootNode = configData.structure
    refNode = rootNode
    templateData = configData.templates

    local previousNode
    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 )
        node.containsCurrentPage = node.isCurrentPage
        --node.isAllPage = false
        node.isLastNode = false

        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 previousNode then
            previousNode.nextNode = node
            node.lastNode = previousNode
            --[[ -- Funktioniert noch nicht
            if previousNode.parent == node.parent or previousNode ~= node.parent  then
            else
                previousNode.isLastNode = true
            end
            ]]--
        end
        previousNode = node

        if node.children then 
            local newChildrenArray = {}

            for childIndex, childNode in pairs( node.children ) do

                initNode(childNode, childIndex, node)

                if childNode.containsCurrentPage then
                    node.containsCurrentPage = true
                end
                if not (childNode.hidden and childNode.containsCurrentPage == false) or showHidden then
                    node.childCount = node.childCount + 1
                    newChildrenArray[node.childCount] = childNode
                end
            end
            node.children = newChildrenArray
        end        

        node.refNode = node;
        if parentNode then
            node.parent = parentNode

            if node.childCount == 0 then
                node.refNode = node.parent;
            end
        end   
        
        if node.isCurrentPage then
            currentNode = node
        end

        if node.refNode == node then
            node.isRefNode = true
        end

        return node;
    end
    
    function prepareNodeHtml(node, i, parentNode)
        if node.isCurrentPage then
            node.innerHtml = templateData.threadNaviItemActive
        else
            node.innerHtml = templateData.threadNaviItem
        end

        if isAllPage then
            node.innerHtml = node.innerHtml:gsub("§§page§§", '#' .. node.name)
        else
            node.innerHtml = node.innerHtml:gsub("§§page§§", node.page)
        end
        node.innerHtml = node.innerHtml:gsub("§§name§§", node.name)

        node.childHtml = ''        
        if node.children then 
            for childIndex, childNode in pairs( node.children ) do
                prepareNodeHtml(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
            parentNode.childHtml = parentNode.childHtml .. node.outerHtml
        end        

        return node;
    end;
        
    initNode(rootNode, 0, false)

    if not currentNode then
        currentNode = rootNode;
    end

    refNode = currentNode.refNode
    totalPageNo = refNode.childCount
    currentPageNo = currentNode.index
    isLastNode = currentNode.isLastNode
    isAllPage = currentNode.isAllPage
    nextNode = currentNode.nextNode
    lastNode = currentNode.lastNode
    
    prepareNodeHtml(rootNode, 0)
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)
    mw.log('Lua navIndex ', frame)
    init( frame.args[config] );
    return refNode.childHtml
end

function p.navWidget(frame)    
    mw.log('Lua navWidget ', frame)
    init( frame.args[config] );

    local node = currentNode
    local pagina, btnHtml 

    if node.isAllPage == true then
        pagina = templateData.paginaAll:gsub("§§page§§", refNode.page);
    else
        pagina = templateData.pagina

        if node.isRefNode == true then
            pagina = templateData.paginaStart
        else
            pagina = templateData.pagina
        end  
        pagina = pagina:gsub("§§totalPageNo§§", node.refNode.childCount ):gsub("§§currentPageNo§§", node.index ) 
        btnHtml = ''
        mw.log('node.lastNode:',node,'.',node.lastNode,'|',previousNode)
        if node.lastNode then
            btnHtml = templateData.btnLast:gsub("§§page§§", node.lastNode.page ):gsub("§§name§§", node.lastNode.name )
        end 
        pagina = pagina:gsub("§§btnLast§§", btnHtml )
    
        btnHtml = ''
        mw.log('node.nextNode:',node,'.',node.nextNode)
        if not node.isLastNode and node.nextNode then
            btnHtml = templateData.btnNext:gsub("§§page§§", node.nextNode.page ):gsub("§§name§§", node.nextNode.name )
        end
        pagina = pagina:gsub("§§btnNext§§", btnHtml )   
    
        btnHtml = ''
        if node.refNode.all then
            btnHtml = templateData.btnAll:gsub("§§page§§", node.refNode.all )
        end

        pagina = pagina:gsub("§§btnAll§§", btnHtml )
    end

    local widgetHtml = templateData.widget:gsub("§§rootPage§§",rootNode.page):gsub("§§threadNavi§§", node.refNode.innerHtml .. node.refNode.childHtml ):gsub("§§pagina§§", pagina )

    return widgetHtml
end

function p.all(frame)
    mw.log('Lua 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)
    mw.log('Lua isAll ', frame)
    init( frame.args[config] );
    mw.log('Lua isAll -> ', isAllPage)
    return isAllPage
end

return p