Star Citizen Wiki Modul:ModuleRequires
Wir laden dich herzlich auf den Star Citizen Wiki Discord Server ein! Du kannst uns auch auf unserem neuen YouTube-Chanel finden!

Modul:ModuleRequires

From Star Citizen Wiki

Modulabhängigkeiten

Dieses Modul benötigt keine anderen Module.

Modulinfo

Dieses Modul extrahiert aus dem Quelltext einer Modulseite alle require(...) Aufrufe, und stellt diese als Liste dar.


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'
	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' )

        li:addClass( 'requirement hatnote' )
            :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 requirements = {}

    for requirement in mw.ustring.gmatch( text, "require%s*%(%s*'(Module?:[%w_/ ]+)'%s*%)" ) do
    	requirement = string.gsub( requirement, 'Module', 'Modul' )
        if requirements[ requirement ] == nil then
            requirements[ requirement ] = 1
        else
            requirements[ requirement ] = requirements[ requirement ] + 1
        end
    end

    local requirementsOut = makeList( requirements )

    local loads = {}

    for load in mw.ustring.gmatch( text, "mw.loadData%s*%(%s*'(Module?:[%w_/ ]+)'%s*%)" ) do
    	load = string.gsub( load, 'Module', 'Modul' )
        if loads[ load ] == nil then
            loads[ load ] = 1
        else
            loads[ load ] = loads[ load ] + 1
        end
    end

    local loadsOut = makeList( loads, 'lädt' )

    local i18n = {}

    for load in mw.ustring.gmatch( text, "format%(%s*'(I18n[%w_/ :-]+)'.*" ) do
    	load = string.format( 'Data:%s.tab|%s', load, load )
    	
        if i18n[ load ] == nil then
            i18n[ load ] = 1
        else
            i18n[ load ] = i18n[ load ] + 1
        end
    end

    local i18nOut = makeList( i18n, 'wird lokalisiert durch' )

    return requirementsOut .. loadsOut .. i18nOut
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()

    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 )
    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="hatnote">Dieses Modul benötigt keine anderen Module.</p>'
    end

    if addWikipediaInfo ~= false then
        text = text .. frame:preprocess( '<p class="hatnote">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 help us deliver our services. By using our services, you agree to our use of cookies.