Modul:NavHeader: Unterschied zwischen den Versionen

Aus Steel Beasts Wiki
imported>FoXFTW
KKeine Bearbeitungszusammenfassung
 
K (1 Version importiert)
 

Aktuelle Version vom 3. September 2023, 08:02 Uhr

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

local NavHeader = {}

local metatable = {}
local methodtable = {}

metatable.__index = methodtable

--- Parse an argument to the recognized format
--- @param arg table|string|nil
--- @return table
local function parseArg( arg )
    if arg == nil then
        return nil
    end

    if type( arg ) == 'table' then
        return {
            [ 'link' ] = tostring( arg[ 1 ] ),
            [ 'text' ] = tostring( arg[ 2 ] ),
            [ 'external' ] = arg[ 3 ] or false
        }
    end

    return {
        [ 'link' ] = tostring( arg ),
        [ 'text' ] = nil,
        [ 'external' ] = false,
    }
end

--- Make a link from data
--- @param dataTable table
--- @return string
local function makeLink( dataTable, isPrev )
    if dataTable == nil then
        return ''
    end

    if dataTable.link == nil then
        error( 'Link must be set' )
    end

    local left = '[['
    local right = ']]'

    if dataTable.external ~= false then
        left = '['
        right = ']'
    end

    local text = ''
    if dataTable.text ~= nil then
        if dataTable.external ~= false then
            text = ' ' .. text
        else
            text = '|' .. text
        end
    end

    local out = left .. mw.text.trim( dataTable.link, '%[%]' ) .. text .. right
    
    local hasArrow = mw.ustring.find( out, '«', 0, true ) or mw.ustring.find( out, '»', 0, true )
    
    if not hasArrow then
    	if isPrev == true then
    		out = '« ' .. out
    	elseif isPrev == false then
    		out = out .. ' »'
    	end
	end

	return out
end


--- Makes the html table
--- @return string
function methodtable.make( self )
    local div = mw.html.create( 'div' )
            :attr( 'class', 'nav-header' )
            :tag( 'span' )
                :wikitext( makeLink( self.prevLink, true ) )
            :done()
            :tag( 'span' )
                :wikitext( makeLink( self.mainLink ) )
            :done()
            :tag( 'span' )
                :wikitext( makeLink( self.nextLink, false ) )
            :done()

    return tostring( div )
end


--- New Instance
--- @return table NavHeader
function NavHeader.new( self, prev, main, next )
    local instance = {
        prevLink = parseArg( prev ),
        mainLink = parseArg( main ),
        nextLink = parseArg( next )
    }

    setmetatable( instance, metatable )

    return instance
end

--- Entry for module calls
---
--- @return string
function NavHeader.fromArgs( frame )
    local args = require( 'Module:Arguments' ).getArgs( frame )

    local instance = NavHeader:new(
        args[ 1 ] or args[ 'prev' ] or args[ 'Zurück' ],
        args[ 2 ] or args[ 'main' ] or args[ 'Übersicht' ],
        args[ 3 ] or args[ 'next' ] or args[ 'Weiter' ]
    )

    return instance:make()
end

return NavHeader