Module:SimpleFormula

From Caves of Qud Wiki
Jump to navigation Jump to search

See Template:Simple formula for documentation.


local p = {}

local process_args = require'Module:ProcessArgs'

function p.simpleformula(frame)
    local args = process_args.merge(true)

    local formula = frame:preprocess(args['formula'])

    local size = args['size']
    if size ~= nil then
        size = size
    else
        size = '130'
    end

    local color = args['color']
    if color ~= nil then
        color = color
    else
        color = '#009403'
    end


    -- use times symbol
    formula = formula:gsub('*', '×')

    -- unbold words (2 letters or longer)
    formula = formula:gsub('(%a%a+)', '<span style="font-weight:normal;">%1</span>')

    -- convert powers (^X) into a superscript
    formula = formula:gsub('%^(%d)', '<sup>%1</sup>')

    -- format the entire formula with size, color, and bolding
    formula = '<div style="display:inline-block;font-size:' .. size .. '%;color:' .. color .. ';font-weight:bold">' .. formula .. '</div>'
    

    return formula
end

return p