Module:Convert

From TEPwiki, Urth's Encyclopedia
Revision as of 13:29, 2 September 2012 by w>WOSlinker
Jump to navigation Jump to search

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

--require "mw.text"
--require "mw.page"

local p = {}

-- This is the top-level function called by {{convert}}.
function p.main(frame, config, args)
    local pframe = frame:getParent();
    local args = pframe.args; -- the arguments passed TO the {{convert}} template, in the wikitext that instantiates the template
    local config = frame.args; -- the arguments passed BY the {{convert}} template, in the wikitext of the template itself
    local output;
    
    in_val1 = tonumber(args[1]);
    in_val2 = tonumber(args[3]);
    disp = args["disp"];


    if in_val1 == nil then
        error ("Module:Convert value not supplied");
        return ""
    end

    if in_val2 == nil then
        -- Single value supplied
        in_unit  = args[2];
        out_unit = args[3];
        if in_unit .. "|" .. out_unit == "°C|°F" then
            out_val1 = in_val1 * 9 / 5 + 32;
        elseif in_unit .. "|" .. out_unit == "°F|°C" then
            out_val1 = (in_val1 - 32) * 5 / 9;
        elseif in_unit .. "|" .. out_unit == "kg|lb" then
            out_val1 = in_val1 * 2.20462262;
        end
        
        if disp == "or" then
            output = in_val1 .. " " .. in_unit .. " or " .. out_val1 .. " " .. out_unit;
        elseif disp == "sqbr" then
            output = in_val1 .. " " .. in_unit .. " <nowiki>[</nowiki>" .. out_val1 .. " " .. out_unit .. "<nowiki>]</nowiki>";
        else
            output = in_val1 .. " " .. in_unit .. " (" .. out_val1 .. " " .. out_unit .. ")";
        end
    else
        -- Two values supplied
        range    = args[2];
        in_unit  = args[4];
        out_unit = args[5];
    end
    
    --error ("Module:Convert is not implemented");
    return output    
end

return p