FANDOM


-- <pre> Creates documentation for using or loading lua modules
-- The messages used for the install documentation are stored in /i18n
-- and these follow the format below:
-- format: prefix_messagename_messagetype_languagecode
-- There are three types of messages ("header", "instruction", "code")
-- example: docs_install_header_en = "Header for install description"
-- example: docs_install_instruction_en = "The instructions to be followed"
-- example: docs_install_code_en = "code for installation"
local docs = {}
local getArgs = require("Dev:Arguments").getArgs
local i18n = require("Dev:Lua install/i18n")
local html = mw.html
local contentLanguage = mw.getContentLanguage():getCode()
 
--%Entry point
--@frame (table)  A frame object
--:(string) Documentation to install a lua module
function docs.main(frame)
    local args = getArgs(frame)
    local source = args.source
    local uselang = args.uselang
 
    return docs.createDocs( source, uselang)
end
 
--%Creates docs
--@source (string)  custom code to use module
--@uselang (string) localization for the module text stored in /i18n subpage
function docs.createDocs(source, uselang)
    contentLanguage = uselang or contentLanguage 
    local moduleName = tostring(mw.title.getCurrentTitle().subpageText or "")
    local nodeInstructions = mw.html.create("div")
    local messages, messageType, messageLang, nodeContent,
         headerNode, code, heading, lang, instruction,uncollapsedNode
    local messages = {
        "main",
        "explanation",
        "install",
        "loadingmodules",
        "creatingtemplate",
        "articleuse"
    }
    local isCollapsed = false
    for key, messageName in ipairs(messages) do
        if messageName == "main" then
            headerNode = createHeader(getMessage(messageName, "header" , contentLanguage))
        else 
            instruction =  getMessage(messageName, "instruction", contentLanguage)
            code = getMessage(messageName, "code", contentLanguage)
            heading = getMessage(messageName, "header", contentLanguage)
            nodeContent = createInstructions(instruction, code, heading, lang)
 
            if not isCollapsed then
                uncollapsedNode = nodeContent
                isCollapsed = true
            else
                nodeInstructions:node(nodeContent)
            end
        end
    end
 
    if source then
        local nodeOtherDocs = createInstructions("Other instructions", source, "Other information")
 
        nodeInstructions:node(nodeOtherDocs)
    end
    local codedocs = require("Module:Codedoc")
    local moduleDocs = codedocs.createDocs(nil, true)
 
    local fullInstructions = createCollapse(tostring(headerNode) .. tostring(uncollapsedNode), nodeInstructions)
 
    return tostring(fullInstructions) .. moduleDocs
end
--%Obtains a localized message
--@msg (string) The name of the message
--@msgType (string) The type of the message (types: instruction, code, header) 
--@lang (string) The language of the message (l10n)
--:(string) A message in the documentation
function getMessage(msg,msgType, lang)
    return i18n["docs_"..msg .. "_" .. msgType .. "_" .. (lang or "en")]
end
--%Creates a simple header (makes it easier to understand code and style differently)
--@text (string) The title or content of a message header
--:(string) A header for a message
function createHeader(text)
    local styles = {
        margin = "1em auto", ["border-left"]= "10px solid #228b22", 
        ["border-right"]= "10px solid #228b22", ["border-radius"] ="10px", 
        ["background-color"] = "#228b22",
        ["color"] = "white",
        ["text-align"] = "center"
    }
    local header = html.create("h2")
        :addClass("lua_install")
 
    for style,attribute in pairs(styles) do
        header:css(style,attribute)
    end
 
    return tostring(header:wikitext(text))
end
--%Creates instructions along with the code and heading
--@text (string) The instructions for installing the lua module
--:(table) A mw.html node containing the instructions
function createInstructions(instruction, code, heading, lang)
    local createTag = mw.html.create
    local nodeContainer = createTag("div")
    local nodeHeader =  createTag("h3")
    local nodeCode = createTag("div")
    local nodeInstruction = createTag("div")
    local styles = {margin= "1em auto", ["border-left"]= "10px solid #228b22", 
        ["border-right"]= "10px solid #228b22", ["border-radius"] ="10px", 
        ["background-color"] = "rgb(242, 242, 242)"
    }
 
    for style,attribute in pairs(styles) do
        nodeContainer:css(style,attribute)
    end
 
    if instruction and code and heading then
        instruction = replaceVariables(instruction)
        code = replaceVariables(code)
        heading = replaceVariables(heading)
 
        nodeHeader:css("text-align","center")
        nodeHeader:wikitext(heading)
        nodeInstruction:wikitext(instruction)
        nodeContainer:node(nodeHeader)
        nodeContainer:node(nodeInstruction)
 
        if code ~= "" then
            nodeCode:wikitext(preprocess(
                [[<source lang="]]..(lang or "lua")..[[">]].. code .."</source>"))
            nodeContainer:node(nodeCode)
        end
    else
        mw.log("Missing messages (instruction, code, heading):\ninstruction:".. (instruction or " nil")
            .. "\ncode: ".. (code or " nil") .. "\nheading:" .. (heading or " nil"))
    end
 
    return nodeContainer
end
--%Replaces certain variables in a message ($1 = pagename)
--@text (string) The text containing the variables to be replaces
--:(string) Replaced text
function replaceVariables(text)
    local pageName = tostring(mw.title.getCurrentTitle().subpageText or "")
 
    text = text:gsub("$1", pageName)
 
    return text
end
--%Creates a collapsible section with a hidden area and a displayed area
--@displayed (string) The text that must be always displayed
--@collapsed (string) The text that is hidden or collapsed initially
--:(table) An mw.html node containing the collapsible section
function createCollapse(displayed, collapsed)
    local collapsedDiv = mw.html.create("div")
    collapsedDiv:addClass("mw-collapsible mw-collapsed")
        :wikitext(tostring(displayed))
    collapsedDiv:tag("div")
        :addClass("mw-collapsible-content")
        :wikitext(tostring(collapsed))
 
    return collapsedDiv
end
--%Returns preprocessed text, and is useful for debugging and testing in lua console
--@text (string) The text that will be preprocessed
--:(string) Preprocessed text
function preprocess(text)
    if mw.getCurrentFrame() then
        return mw.getCurrentFrame():preprocess(text)
    end
 
    return text
end
 
return docs

Ad blocker interference detected!


Wikia is a free-to-use site that makes money from advertising. We have a modified experience for viewers using ad blockers

Wikia is not accessible if you’ve made further modifications. Remove the custom ad blocker rule(s) and the page will load as expected.