Module:Lua guide/Helper module list

From the Tesseract Wiki, the wiki for all things Marvel Cinematic Universe
Jump to navigation Jump to search

Documentation for this module may be created at Module:Lua guide/Helper module list/doc

-- <nowiki>
local p = {}
local arr = require( 'Module:Array' )
local dpl = require( 'Module:DPLlua' )
require( 'Module:Mw.html extension' )

function p.main( frame )
    local args = frame.args
    return p._main( args )
end

function p._main( args )
    local hlevel = tonumber( args.hlevel or '' ) or 2
    local helperModules = dpl.ask{
        namespace = 'Module',
        uses = 'Template:Helper module',
        titlematch = '%/doc',
        nottitlematch = 'Sandbox/%',
        ordermethod = 'title',
        include = '{Helper module}, {Helper module}:example',
        allowcachedresults = true,
        cacheperiod = 604800 -- One week
    }

    local tbl = mw.html.create( 'table' ):addClass( 'wikitable' ):css( 'width', '100%' )
    tbl:tag( 'tr' )
        :tag( 'th' ):wikitext( 'Module' ):done()
        :tag( 'th' ):wikitext( 'Function' ):done()
        :tag( 'th' ):wikitext( 'Use' ):done()
        :tag( 'th' ):wikitext( 'Example' ):done()

    for _, module in ipairs( helperModules ) do
        local include = module.include['Helper module']

        if include.name and include.name ~= '' then
            local index = arr.newIncrementor()
            local funcData = {}
            local moduleName = string.format( "<h%d style='display:inline; font-size:1em; position:absolute; top:0; opacity:0;'><span style='display:none'>%s</span></h%d>[[%s|%s]]",
                hlevel,
                'Module:' .. include.name,
                hlevel,
                'Module:' .. include.name,
                include.name
            )

            while include[ 'fname'..index() ] do
                local fname = mw.text.split( mw.text.unstripNoWiki( include[ 'fname'..index.n ] ), ';;', true )

                for i = 1, #fname do
                    local overload = mw.text.trim( fname[i] )
                    local name = overload:match( '^(.-)[%({]' )
                    local arg = overload:gsub( name, '' )

                    if name:match( '%S' ) then
                        if i ==1 then
                            fname[i] = string.format( "<h%d style='display:inline; font-size:1em; position:absolute; top:0; opacity:0;'><span style='display:none'>%s</span></h%d><code>'''%s'''%s</code>",
                                hlevel + 1,
                                name,
                                hlevel + 1,
                                name,
                                mw.getCurrentFrame():extensionTag( 'nowiki', arg )
                            )
                        else
                            fname[i] = string.format( "<code>'''%s'''%s</code>",
                                name,
                                mw.getCurrentFrame():extensionTag( 'nowiki', arg )
                            )
                        end
                    else
                        fname[i] = string.format( "<code>%s</code>", mw.getCurrentFrame():extensionTag( 'nowiki', arg ) )
                    end
                end

                local fdata = {
                    fname = table.concat( fname, '<br>' ),
                    fuse = include[ 'fuse'..index.n ],
                }

                table.insert( funcData, fdata )
            end

            for i, func in ipairs( funcData ) do
                tbl:tag( 'tr' )
                    :IF( i == 1 )
                        :tag( 'td' )
                            :attr( 'rowspan', #funcData )
                            :css( 'position', 'relative' )
                            :wikitext( moduleName )
                        :done()
                    :END()
                    :tag( 'td' ):wikitext( func.fname ):css( 'position', 'relative' ):done()
                    :tag( 'td' ):wikitext( func.fuse ):done()
                    :IF( i == 1 )
                        :tag( 'td' )
                            :attr( 'rowspan', #funcData )
                            :wikitext( include.example or '' )
                        :done()
                    :END()
            end
        end
    end

    local res = tostring( tbl )
    return res
end

return p
-- </nowiki>