Module:Documentation

Module documentation[view][edit][history][purge]
This documentation is transcluded from Module:Documentation/doc. Changes can be proposed in the talk page.
An icon from the Wikimedia Codex library.
Module:Documentation loads styles from Module:Documentation/styles.css.
Function list
L 15 — mbox
L 23 — hatnote
L 31 — p.doc

Module:Documentation implements Template:Documentation for templates and modules.


-- Adapted from: https://starcitizen.tools/Module:Documentation

local noticebox = require('Module:Noticebox')._main

local p = {}

local icons = {
    notice = "MW_codex_icon_notice.svg",
    wikipedia = 'MW_codex_icon_logo-Wikipedia.svg',
    config = 'MW_codex_icon_settings.svg',
    testcases = 'MW_codex_icon_labFlask.svg',
    styles = 'MW_codex_icon_palette.svg'
}

local function mbox(title, text, options)
    return noticebox( {
            content = string.format("%s<br/>''%s''", title, text),
            image = options['icon'],
            image_width = "20px"
        })
end

local function hatnote(text, options)
    return noticebox( {
            content = text,
            image = options['icon'],
            image_width = "20px"
        })
end

function p.doc( frame )
    local title = mw.title.getCurrentTitle()
    local args = frame:getParent().args
    local page = args[1] or string.gsub( title.fullText, '/doc$', '' )
    local ret, cats, ret1, ret2, ret3
    local pageType = title.namespace == 828 and 'Module' or 'Template'

    -- subpage header
    if title.subpageText == 'doc' then
        ret = mbox(
                string.format("This is a documentation subpage for %s", page),
                string.format("It contains usage information, categories, and other content that is not part of the [[%s|original %s page]].", page, pageType),
                { icon = icons.notice }
        )

        if title.namespace == 10 then -- Template namespace
            cats = '[[Category:Templates documentation' .. '|' .. title.baseText .. ']]'
            ret2 = ''
        elseif title.namespace == 828 then -- Module namespace
            cats = '[[Category:Modules documentation'  .. '|' .. title.baseText .. ']]'
            ret2 = ''
            ret2 = ret2 .. require('Module:Module toc').main()
        else
            cats = ''
            ret2 = ''
        end

        return tostring( ret ) .. ret2 .. cats
    end

    -- template header
    -- don't use mw.html as we aren't closing the main div tag
    ret1 = '<div class="documentation">'

    ret2 = mw.html.create( nil )
             :tag( 'div' )
             :addClass( 'documentation__header' )
             :tag( 'span' )
             :addClass( 'documentation__title' )
             :wikitext( string.format("%s documentation", pageType) )
             :done()
             :tag( 'span' )
             :addClass( 'documentation__links plainlinks' )
             :wikitext(
            '[[' .. tostring( mw.uri.fullUrl( page .. '/doc', {action='view'} ) ) .. ' view]]' ..
                    '[[' .. tostring( mw.uri.fullUrl( page .. '/doc', {action='edit'} ) ) .. ' edit]]' ..
                    '[[' .. tostring( mw.uri.fullUrl( page .. '/doc', {action='history'} ) ) .. ' history]]' ..
                    '[<span class="jsPurgeLink">[' .. tostring( mw.uri.fullUrl( title.fullText, { action = 'purge' } ) ) .. ' purge]</span>]'
    )
             :done()
             :done()
             :tag( 'div' )
             :addClass( 'documentation__subheader' )
             :tag( 'span' )
             :addClass( 'documentation__documentation' )
             :wikitext( string.format( 'This documentation is transcluded from [[%s/doc]]. Changes can be proposed in the talk page.', page ) )
             :done()
             :wikitext( frame:extensionTag{ name = 'templatestyles', args = { src = 'Module:Documentation/styles.css'} } )
             :done()

    ret3 = {}

    if args.fromWikipedia then
        table.insert( ret3,
                mbox(
                        string.format(
                                "'''%s''' is imported from [https://en.wikipedia.org/wiki/%s %s] on Wikipedia.",
                                title.fullText,
                                mw.uri.encode( page, 'WIKI' ),
                                page
                        ),
                        string.format(
                                "This %s is imported from the English Wikipedia. Although the visual appearance might be different, the functionality is identical. Please refer to the Wikipedia page for detailed documentation.",
                                pageType
                        ),
                        { icon = icons.wikipedia }
                )
        )
        --- Set category
        table.insert( ret3, '[[Category:' .. string.format( "%ss imported from Wikipedia", pageType ) .. ']]' )
    end

    if title.namespace == 828 then
        -- Has config
        if mw.title.new( title.fullText .. '/config.json', 'Module' ).exists then
            table.insert( ret3,
                    mbox(
                            string.format(
                                    "'''%s''' loads configuration from [[%s/config.json]].",
                                    title.fullText,
                                    title.fullText
                            ),
                            'This module can be configured from the config.json subpage.' ,
                            { icon = icons.config }
                    )
            )
        end

        -- Testcase page
        if title.subpageText == 'testcases' then
            table.insert( ret3,
                    hatnote(
                            string.format( 'This is the test cases page for the module [[Module:%s]].', title.baseText ),
                            { icon = icons.testcases }
                    )
            )
        end

        table.insert( ret3, "[[Category:Modules]]" )
    end

    -- Has templatestyles
    if mw.title.new( title.fullText .. '/styles.css' ).exists then
        table.insert( ret3,
                hatnote(
                        string.format( "'''%s''' loads styles from [[%s/styles.css]].", title.fullText, title.fullText ),
                        { icon = icons.styles }
                )
        )
    end

    -- Module stats bar
    if title.namespace == 828 then
        table.insert( ret3, '<div class="documentation__modulestats">' )

        -- Function list
        table.insert( ret3, require( 'Module:Module toc' ).main() )

        -- Unit tests
        local testcaseTitle = title.baseText .. '/testcases'
        if mw.title.new( testcaseTitle, 'Module' ).exists then
            -- There is probably a better way :P
            table.insert( ret3, frame:preprocess( '{{#invoke:' .. testcaseTitle .. '|run}}' ) )
        end

        table.insert( ret3, '</div>' )
    end

    return ret1 .. tostring( ret2 ) .. '<div class="documentation__content">' .. table.concat( ret3 )
end

return p