Fandom Developers Wiki
Register
Advertisement

This module was made as a sandbox for the user DarthKitty. This documentation is kept to prevent redlinks.


-- <nowiki>
local p = {}
local inspect = require("Dev:Inspect")
local getArgs = require("Dev:Arguments").getArgs

function p.localDataModule()
    local success, data = pcall(mw.loadData, "Module:Some data")

    if not success then
        data = {name = "<default>"}
    end

    return "Hello, " .. data.name .. "!"
end

function p.printArgs(frame)
    local args = getArgs(frame)
    local container = mw.html.create("table")
        :css("margin-bottom", "1em")
        :css("width", "100%")
        :tag("caption")
            :wikitext("Parameters given (order is non-deterministic)")
            :done()
        :tag("tr")
            :tag("th")
                :attr("scope", "col")
                :wikitext("Key")
                :done()
            :tag("th")
                :attr("scope", "col")
                :css("width", "100%")
                :wikitext("Value")
                :done()
            :done()

    for key, value in pairs(args) do
        container:tag("tr")
            :css("vertical-align", "top")
            :tag("td")
                :tag("pre")
                    :wikitext(inspect(key))
                    :done()
                :done()
            :tag("td")
                :tag("pre")
                    :wikitext(value)
                    :done()
                :done()
            :done()
    end

    return container
end

function p.getTitle(frame)
    local title = frame:getParent():getTitle()

    return "<pre>" .. inspect(title) .. "</pre>"
end

--[[
--------------------------------------------------------------------------------
local checkType = require("libraryUtil").checkType

local function concatenate(...)
    for i = 1, select('#', ...) do
        checkType("concatenate", i, select(i, ...), "string")
    end

    return table.concat{...}
end

concatenate("Hello", ", ", "World", "!") --> "Hello, World!"
--------------------------------------------------------------------------------
--]]

function p.doSomething()
    -- Code goes here...
end

-- Call a module that is protected by [[Module:Non-global/sandbox]]
function p.call_local_module()
    return require('Dev:Sandbox/DarthKitty/Local module')
end

--------------------------------------------------------------------------------
-- Converts a module's name (which can contain spaces, punctuation, etc.) into
-- a working Lua identifier.
--
-- Created in preparation for writing [[Template:Module Install]].
--
-- @TODO `reserved` should be moved to its own submodule, and loaded with
--       `mw.loadData()`.
--------------------------------------------------------------------------------
function p.title2variable()
    local title = mw.title.getCurrentTitle()
    local rootText = title.rootText

    -- Only run on module documentation pages
    if not title.isSubpage
    or rootText ~= title.baseText -- not subpage of subpage
    or rootText ~= 'Global Lua Modules' then
        error()
    end

    local words = mw.text.split(title.subpageText, '[%s-]')
    local lang = mw.language.getContentLanguage()
    local ustring = mw.ustring
    local uFind = ustring.find
    local reserved = {
        ['and'] = true,
        ['break'] = true,
        ['do'] = true,
        ['else'] = true,
        ['elseif'] = true,
        ['end'] = true,
        ['false'] = true,
        ['for'] = true,
        ['function'] = true,
        ['if'] = true,
        ['in'] = true,
        ['local'] = true,
        ['nil'] = true,
        ['not'] = true,
        ['or'] = true,
        ['repeat'] = true,
        ['return'] = true,
        ['then'] = true,
        ['true'] = true,
        ['until'] = true,
        ['while'] = true,

        -- shadowing these might break stuff
        ['_G'] = true,
        ['assert'] = true,
        ['_VERSION'] = true,
        ['error'] = true,
        ['getfenv'] = true,
        ['getmetatable'] = true,
        ['ipairs'] = true,
        ['next'] = true,
        ['pairs'] = true,
        ['pcall'] = true,
        ['rawequal'] = true,
        ['rawget'] = true,
        ['rawset'] = true,
        ['select'] = true,
        ['setmetatable'] = true,
        ['tonumber'] = true,
        ['tostring'] = true,
        ['type'] = true,
        ['unpack'] = true,
        ['xpcall'] = true,
        ['debug'] = true,
        ['math'] = true,
        ['os'] = true,
        ['require'] = true,
        ['package'] = true,
        ['string'] = true,
        ['table'] = true,
        ['mw'] = true
    }

    for i, word in ipairs(words) do
        -- remove everything besides letters, numbers, and spaces
        word = ustring.gsub(word, '[^%w%s]', '')

        -- do not make a word lowercase if it is camelCase
        if not uFind(word, '%l') or not uFind(word, '%u') then
            word = lang:lc(word)
        end

        -- lowercase the first letter of the first word
        -- uppercase the first letter of every remaining word
        word = i > 1 and lang:ucfirst(word) or lang:lcfirst(word)

        if reserved[word] then
            word = '_' .. word
        end

        words[i] = word
    end

    return table.concat(words)
end

return p
Advertisement