Modul:Tutorials
Erscheinungsbild
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 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
-- 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 )
return widgetHtml
end
return p