f = degree = "°" -- used by add_unit_namesminus = "−" -- used by makeRow and makeTablethinSpace = mw.ustring.char(0x2009) -- used by makeCell
-- Error message handlingmessage = ""
local function add_message(new_message) if show then if check_for_string(message) then message = message .. " " .. new_message else message = "Notices: " .. new_message end endend
-- Input and output parameterslocal function get_format (frame) local input_parameter = frame.args.input local output_parameter = frame.args.output if input_parameter
"F" then output_unit = "C" else error ("Please provide an input unit in the input parameter: F for Fahrenheit or C for Celsius", 0) end if length
nil then add_message("No output format has been provided in the
output parameter.") else cell_format = local n = 1 for unit in output_parameter:gmatch("[CF]") do cell_format[n] = unit n = n + 1 if n > 2 then break end end local function set_format(key, formatVariable, formatValue1, formatValue2) if string.find(output_parameter, key) then cell_format[formatVariable] = formatValue1 else cell_format[formatVariable] = formatValue2 end end if cell_format[1] then cell_format.first = cell_format[1] else error("C or F not found in output parameter") end if cell_format[2]cell_format[1] then error("There should not be two of the same unit name in the output parameter.") else cell_format["convert_units"] = "yes" end end set_format("unit", "unit_names", "yes", "no") set_format("no ?color", "color", "no", "yes") set_format("sort", "sortable", "yes", "no") set_format("full ?size", "small_font", "no", "yes") set_format("no ?brackets", "brackets", "no", "yes") set_format("round", "decimals", "0", "") if string.find(output_parameter, "line break") then cell_format["line_break"] = "yes" elseif string.find(output_parameter, "one line") then cell_format["line_break"] = "no" else cell_format["line_break"] = "auto" end if string.find(output_parameter, "one line") and string.find(output_parameter, "line break") then error("Place either \"one line\" or \"line break\" in the output parameter, not both") end end if frame.args.palette
"show" then show = true else show = false end return length, input_unit, output_unitend
-- Number and string-handling functionslocal function check_for_number(value) return type(tonumber(value))
function check_for_string(string) string = tostring(string) return string ~= "" and string ~= nilend
local function round(value, decimals) value = tonumber(value) if type(value)
nil then value = "nil" add_message("Format was asked to operate on " .. value .. ", which cannot be converted to a number.", 2) return "" endend
local function convert(value, decimals, unit) -- Unit is the unit being converted from. It defaults to input_unit. if not unit then unit = input_unit end if check_for_number(value) then local value = tonumber(value) if unit
"F" then add_message(value .. " " .. degree .. unit .. " was converted.") return round((value - 32) * 5/9, decimals) else error("Input unit not recognized", 2) end else return "" -- Setting result to empty string if value is not a number avoids concatenation errors. endend
-- Input parsingfunction make_array(parameter, array, frame) local array = local n = 1 for number in parameter:gmatch("%-?%d+%.?%d?") do local number = number if n
nil then precision = "0" else precision = #decimals end end table.insert(array, n, number) n = n + 1 if n > length then break end end if not array[length] then add_message("There are not " .. length .. " values in the " .. parameter .. " parameter.") end return array, precisionend
function make_arrays(frame) get_format(frame) local parameter_a = frame.args.a local parameter_b = frame.args.b local parameter_c = frame.args.c if parameter_a then a = make_array(parameter_a, a, frame) else error("Please provide a set of numbers in parameter a") end if parameter_b then b = make_array(parameter_b, b, frame) else add_message("There is no content in parameter
b.") end if parameter_c then c = make_array(parameter_c, c, frame) else add_message("There is no content in parameter c.") end return a, b, cend-- Color generation
palettes =
local function temperature_color(palette, value, out_rgb) --Return style for a table cell based on the given value which should be a temperature in °C. local background_color, text_color value = tonumber(value) if value
local background_rgb = out_rgb or for i, v in ipairs(palette) do local a, b, c, d = unpack(v) if value <= a then background_rgb[i] = 0 elseif value < b then background_rgb[i] = (value - a) * 255 / (b - a) elseif value <= c then background_rgb[i] = 255 elseif value < d then background_rgb[i] = 255 - ((value - c) * 255 / (d - c)) else background_rgb[i] = 0 end end background_color = string.format('%02X%02X%02X', background_rgb[1], background_rgb[2], background_rgb[3]) end if text_color
local function color_CSS(background_color, text_color) if background_color and text_color then return 'background: #' .. background_color .. '; color: #' .. text_color .. ';' elseif background_color then return 'background: #' .. background_color .. ';' else return endend
function temperature_CSS(value, unit, palette) local palette = palettes[palette] or palettes.cool local value = tonumber(value) if value
'F' then return color_CSS(temperature_color(palette, convert(value, decimals, 'F'))) else unit_error(unit or "nil") end endend
--
local function add_unit_names(value, unit) if not unit then unit = input_unit end if output_format.unit_names
local function if_yes(parameter, realization1, realization2) if realization1 then if realization2 then if parameter
"yes" then parameter = realization1 else parameter = "" end end else parameter = "" add_message("
if_yes needs at least one realization") end return parameterendfunction makeCell(output_format, a, b, c) local cell, cell_content = "", "" local color_CSS, other_CSS, title_attribute, sortkey, attribute_separator, converted_units_separator = "", "", "", "", "", "", "" local style_attribute, high_low_separator, brackets, values, converted_units =,,,, if check_for_number(output_format.decimals) then decimals = output_format.decimals --Precision is the number of decimals in the first number of the last array. This may be a problem for data from Weatherbase, which seems to inappropriately remove .0 from numbers that have it. else decimals = precision end if check_for_number(b) and check_for_number(a) then values, high_low_separator =, elseif check_for_number(a) then values = elseif check_for_number(c) then values = end if output_format.first
"yes" then converted_units = end values = elseif output_format.first
"F" then if output_format.convert_units
nil then output_format.first = "nil" end add_message("
" .. output_format.first .. ", the value for first in output_format is not recognized.") end -- if output_format.convert_units"auto" then if check_for_string(values[2]) or decimals ~= "0" or output_format.show_units
"yes" then converted_units_separator = "
" elseif output_format.line_break
input_unit then attribute_value = c else attribute_value = convert(c, decimals) end sortkey = if_yes(output_format.sortable, " data-sort-value=\"" .. attribute_value .. "\"") title_attribute = " title=\"Average temperature: " .. attribute_value .. " " .. degree .. output_format.first .. "\"" end elseif check_for_number(b) then color_css = "" elseif check_for_number(a) then color_CSS = if_yes(output_format.color, temperature_CSS(a, input_unit, palette)) else add_message("Neither a nor b nor c are strings.") end other_CSS = if_yes(output_format.small_font, "font-size: 85%;") if check_for_string(color_CSS) or check_for_string(other_CSS) then style_attribute = end if check_for_string(other_CSS) or check_for_string(color_CSS) or check_for_string(title_attribute) or check_for_string(sortkey) then attribute_separator = " | " end cell = "\n| " .. style_attribute[1] .. color_CSS .. other_CSS .. style_attribute[2] .. title_attribute .. sortkey .. attribute_separator .. cell_content return cellend
function f.makeRow(frame) make_arrays(frame) local output = "" if frame.args[1] then output = "\n|-" output = output .. "\n! " .. frame.args[1] if frame.args[2] then output = output .. " !! " .. frame.args[2] end end if cell_format then output_format = cell_format end if a and b and c then for i = 1, length do if not output_format then output_format = output_formats.high_low_average_F end output = output .. makeCell(output_format, a[i], b[i], c[i]) end elseif a and b then for i = 1, length do if not output_format then output_format = output_formats.high_low_F end output = output .. makeCell(output_format, a[i], b[i]) end elseif a then for i = 1, length do if not output_format then output_format = output_formats.average_F end output = output .. makeCell(output_format, a[i]) end end output = mw.ustring.gsub(output, "([%p%s])-(%d)", "%1" .. minus .. "%2") return outputend
function f.makeTable(frame) make_arrays(frame) local output = "
return f