Modul:ModuleRequires

Aus Star Citizen Wiki

Die Dokumentation für dieses Modul kann unter Modul:ModuleRequires/doc erstellt werden

local ModuleRequires = {}

local metatable = {}
local methodtable = {}

metatable.__index = methodtable


--- Creates a list from passed requirements
---
--- @param requirements table
--- @return string
local function makeList( requirements, typeText )
    if type( requirements ) ~= 'table' then
        return ''
    end

    typeText = typeText or 'benötigt'
    local icon = 'OOjs_UI_icon_wikiText.svg'

    if typeText == 'wird lokalisiert durch' then
        icon = 'OOjs_UI_icon_globe.svg'
    elseif typeText == 'lädt' then
        icon = 'OOjs_UI_icon_advanced.svg'
    elseif typeText == 'setzt folgende Kategorie' then
        icon = 'OOjs UI icon bookmarkOutline.svg'
    elseif typeText == 'nutzt das SMW Attribut' then
        icon = 'OOjs_UI_icon_link-ltr.svg'
    end


    local ul = mw.html.create( 'ul' )

    ul:addClass( 'requirements-list' )

    local added = 0

    for module, count in pairs( requirements ) do
        local li = mw.html.create( 'li' )

        if type( count ) == 'number' and count > 1 then
            module = string.format( '[[%s]] (%dx)', module, count )
        else
            module = string.format( '[[%s]]', module )
        end

        li:addClass( 'requirement mw-message-box' )
          :wikitext( string.format( '[[Datei:%s|20px|link=|class=invert-if-dark]] Dieses Modul %s %s', icon, typeText, module ) )
          :allDone()

        ul:node( li )

        added = added + 1
    end

    if added > 0 then
        return tostring( ul:allDone() )
    end

    return ''
end


--- Parses the page content for available 'require(...)' calls
---
--- @param text string - The page text
--- @return string
local function parse( text )
    if text == nil then
        return ''
    end

    local function extractData( pattern, matchFn )
        local matches = {}

        for match in mw.ustring.gmatch( text, pattern ) do
            if matchFn ~= nil then
                match = matchFn( match )
            end

            match = string.gsub( match, '_', '' )

            if matches[ match ] == nil then
                matches[ match ] = 1
            else
                matches[ match ] = matches[ match ] + 1
            end
        end

        return matches
    end

    local requirements = extractData(
        "require%s*%(%s*'(Module?:[%w_/ -]+)'%s*%)",
        function( match ) return string.gsub( match, 'Module', 'Modul' ) end
    )

    local loads = extractData(
        "mw.loadData%s*%(%s*'(Module?:[%w_/ -]+)'%s*%)",
        function( match ) return string.gsub( match, 'Module', 'Modul' ) end
    )

    local i18n = extractData(
        "'(I18n[%w_/ :-]+)'",
        function( match ) return string.format( 'Data:%s.tab|%s', match, match ) end
    )

    local categories = extractData(
        "%[%[K?C?ategory?i?e?:([%w_/ -]+)%]%]",
        function( match ) return string.format( ':Kategorie:%s|%s', match, match ) end
    )

    local smwCondProperties = extractData(
        "-?([%w_/ -]+)::",
        function( match ) return string.format( 'Attribut:%s|%s', match, match ) end
    )

    local smwAskProperties = extractData(
        "'%?([%w_/ -]+)",
        function( match )
        	match = mw.text.trim( match, '-' )
        	return string.format( 'Attribut:%s|%s', match, match )
    	end
    )

    for prop, count in pairs( smwAskProperties ) do
        if smwCondProperties[ prop ] ~= nil then
            smwCondProperties[ prop ] = smwCondProperties[ prop ] + count
        else
            smwCondProperties[ prop ] = count
        end
    end

    local requirementsOut = makeList( requirements )
    local loadsOut = makeList( loads, 'lädt' )
    local i18nOut = makeList( i18n, 'wird lokalisiert durch' )
    local categoriesOut = makeList( categories, 'setzt folgende Kategorie' )
    local smwOut = makeList( smwCondProperties, 'nutzt das SMW Attribut' )

	local content = ''
	for header, group in pairs( {
		[ 'Module' ] = requirementsOut .. loadsOut,
		[ 'I18n' ] = i18nOut,
		[ 'Kateogrien' ] = categoriesOut,
		[ 'Semantic Media Wiki' ] = smwOut } ) do

		if string.len( group ) > 0 then
			content = string.format( "%s|-|%s=\n%s", content, header, group )
		end
	end

    if content ~= '' then
    	content = mw.getCurrentFrame():extensionTag( 'tabber', mw.text.trim( content ) )
    end

	return content
end


--- Checks if the page is a docu page, creates the main module title and loads the content
--- @return string
function methodtable.getRequirements( self )
    local title = mw.title.getCurrentTitle()
    --local title = mw.title.makeTitle(0, 'Fahrzeug') DEBUG

    if title == nil then
        return ''
    end

    local root = mw.title.makeTitle( 'Module' , title.baseText )
    if root ~= nil then
        return parse( root:getContent() )
    end

    return ''
end


--- Template entry
function ModuleRequires.main( frame )
    local addWikipediaInfo = frame:getParent().args[ 'Modul aus Wikipedia' ] or false

    local instance = ModuleRequires:new()

    local text = instance:getRequirements()

    if string.len( text ) == 0 then
        text = '<p class="mw-message-box">Dieses Modul benötigt keine anderen Module.</p>'
    end

    if addWikipediaInfo ~= false then
        text = text .. frame:preprocess( '<p class="mw-message-box">Dieses Modul stammt von [[wikipedia:Module:{{ROOTPAGENAME}}|Wikipedia]]</p>' )
    end

    return text
end


--- New Instance
function ModuleRequires.new( self )
    local instance = {}

    setmetatable( instance, metatable )

    return instance
end


return ModuleRequires
Cookies helfen uns bei der Bereitstellung dieses Wikis. Durch die Nutzung des Star Citizen Wiki erklärst du dich damit einverstanden, dass wir Cookies speichern.