dev

This module was made as a sandbox for the Math-colors module. This documentation is kept to prevent redlinks.


-- <nowiki>
--------------------------------------------------------------------------------
-- DO NOT USE!
--
-- This is mostly hypothetical, since [[Module:Color]] doesn't exist yet(?).
--------------------------------------------------------------------------------
local p = {}
local color = require("Dev:Color")
local getArgs = require("Dev:Arguments").getArgs
local userError = require("Dev:User error")

--% Describe the following function here.
--@ args (table)
--@ args.expression (string)
--@ [args.bgColor] (string) defaults to "white"
--@ [args.textColor] (string) defaults to "black"
--: (str)
function p.render(args)
    local expression = args.expression

    if not expression then
        error("missing expression", 0)
    end

    local _, bgColor = xpcall(function ()
        local tmp = color(args.bgColor or "white")

        return tmp.red .. "," .. tmp.green .. "," .. tmp.blue
    end, function ()
        error("invalid background color", 0)
    end)

    local _, textColor = xpcall(function ()
        local tmp = color(args.textColor or "black")

        return tmp.red .. "," .. tmp.green .. "," .. tmp.blue
    end, function ()
        error("invalid text color", 0)
    end)

    local function parseMath(expression)
        local frame = mw.getCurrentFrame()

        return frame:preprocess("<math>" .. expression .. "</math>")
    end

    local colored = mw.html.create("span")
        :addClass("hidden")
        :css("display", "inline")
        :wikitext(parseMath(
            "\\pagecolor[RGB]{" .. bgColor .. "}" ..
            "{\\color[RGB]{" .. textColor .. "}" .. expression .. "}"
        ))

    local plain = mw.html.create("span")
        :css("display", "none")
        :wikitext(parseMath(expression))

    return tostring(colored) .. tostring(plain)
end

--% Describe the following function here.
--@ frame (table) The
--- [[Lua reference manual/Scribunto libraries#Frame object|frame object]]
--: (str)
function p.main(frame)
    local args = getArgs(frame)
    local success, response = pcall(p.render, args)

    return success and response or userError(response)
end

return p
-- </nowiki>