Module:Luadocs

From The Satanic Wiki
Jump to navigation Jump to search

Documentation for this module may be created at Module:Luadocs/doc

--<nowiki>
--------------------------------------------------------------------------------
-- Shows modules links in the list of global modules
-- @usage {{#invoke:Luadocs|main|<modulename>|notests=1}}
--
-- @module Luadocs
-- @alias p
-- @release stable
-- @require [[Global Lua Modules/Arguments           |Module:Arguments]]
-- @require [[Global Lua Modules/Config              |Module:Config]]
-- @require [[Global Lua Modules/Existsmod           |Module:Existsmod]]
-- @require [[Global Lua Modules/Links               |Module:Links]]
-- @require [[Global Lua Modules/No globals          |Module:No globals]]
-- @require [[Global Lua Modules/No interwiki access |Module:No interwiki access]]
-- @require [[Global Lua Modules/User error          |Module:User error]]
-- @require [[Global Lua Modules/Yesno               |Module:Yesno]]
-- @require [[Module:Luadocs/config]]
-- @require [[Module:Luadocs/i18n]]
-- @see [[Global Lua Modules]]
-- @see [[Template:Module links]]
--------------------------------------------------------------------------------

local p = {}

local getArgs = require("Module::Arguments").getArgs
local isMod = require("Module::Existsmod").module_exists
local link = require("Module::Links").link
local userError = require("Module::User error")
local yesno     = require("Module::Yesno")

local i18n  = require("Module::I18n").loadMessages("Luadocs")
local cfg   = require("Module::Config").loadConfig("Luadocs")

require("Module::No interwiki access")
require("Module::No globals")

--------------------------------------------------------------------------------
-- @param {string} modName
--        The name of the module without the "Module:" prefix.
-- @param[opt] {string} external
--             The source this module was ported from, if any.
-- @param[opt] {boolean} noTests
--             Whether to not link to the "/testcases" subpage.
-- @return {string|nil}
--------------------------------------------------------------------------------
function p.create_links(modName, external, noTests)
    if not modName then
        return
    end

    modName = mw.language.new('en'):ucfirst(modName)
    local headerText = modName
    local modLinks = {
        "[[Global Lua Modules/" .. modName .. "|" .. i18n:msg("link-docs") .. "]]",
        "[[Module:" .. modName .. "|" .. i18n:msg("link-source") .. "]]",
    }

    if external then
        headerText = link(external, modName)

        local icons = cfg:getValue("icons")
        local lc = external:lower();

        for substring, icon in pairs(icons) do
            if lc:sub(1, substring:len()) == substring then
                headerText = "[[File:" .. icon .. "|16x16px|link=|"
                    .. i18n:msg("external-alt-" .. substring) .. "]] "
                    .. headerText
                break
            end
        end
    end

    local headerDiv = mw.html.create("div")
        :css("font-size", "18px")
        :css("line-height", "26px")
        :wikitext(headerText)

    if not noTests then
        local testLinkText = i18n:msg("link-tests")
        local testPage = "Module:" .. modName .. "/testcases"

        local testLink
        if isMod(testPage, true) then
            testLink = "[[" .. testPage .. "|" .. testLinkText .. "]]"
        else
            testLink = tostring(
                mw.html.create("span")
                    :addClass("redlink")
                    :wikitext(
                        "[", tostring(
                            mw.uri.fullUrl(testPage, {
                                action = "edit",
                                preload = "Template:Moduletestcases"
                            })
                        ), " ", testLinkText, "]"
                    )
            )
        end

        table.insert(modLinks, testLink)
    end

    return tostring(headerDiv) .. table.concat(modLinks, " · ")
end

--------------------------------------------------------------------------------
-- @param {Frame} frame
-- @return {string|nil}
--------------------------------------------------------------------------------
function p.main(frame)
    local args = getArgs(frame)

    if args[1] then
        return p.create_links(args[1], args["external"], yesno(args["notests"]))
    end
end

return p