-- This module generates the wikitext required at Module:Convert/data
-- by reading and processing the wikitext of the master list of units
-- (see conversion_data for the page title).
---
-- Script method:
-- * Read lines, ignoring everything before "== Conversions ==".
-- * Process the following lines:
-- * Find next level-3 heading like "=== Length ===".
-- * Parse each following line starting with "|"
-- (but ignore lines starting with "|-" or "|}".
-- * Split such lines into fields (delimiter "||") and trim
-- leading/trailing whitespace from each field.
-- Remove any "colspan" at front of second field (symbol).
-- * Remove thousand separators (commas) from the scale field.
-- If the scale is a number, do not change it.
-- Otherwise, it should be an expression like "5/9", in
-- which case it is replaced by the value of the expression.
-- * Remove wiki formatting '...' from the link field.
-- * Remove redundant fields from the unit to reduce size of data table.
-- * Create alternative forms of a unit such as an alias or a combination.
-- * Stop processing when encounter end of text or a line starting
-- with a level-2 heading ("==" but not "===").
-- * Repeat above for each heading listed at prepare_data.
-- * Output Lua source for the units table.
---
-- -- Output has the following form.
-- local all_units = {
-- ["unitcode"] = { -- standard format
-- name1 = "singular name", -- omitted if redundant
-- name1_us = "singular name sp=us", -- omitted if redundant
-- name2 = "plural name", -- omitted if redundant
-- name2_us = "plural name sp=us", -- omitted if redundant
-- symbol = "symbol",
-- sym_us = "symbol sp=us", -- omitted if redundant
-- usename = 1, -- omitted if empty
-- utype = "unit type", -- from level-3 heading
-- scale = 1, -- a value, if necessary from evaluating an expression
-- subdivs = { ["ft"] = { 5280, default = "km" }, ["yd"] = { 1760 } } -- composite input; omitted if empty
-- link = "title of article for wikilink", -- omitted if empty or redundant
-- ... -- other values
-- },
-- ["unitcode"] = { -- alternative format to generate an alias
-- target = "unit code",
-- ... -- optional values to override those of target
-- },
-- ["unitcode"] = { -- alternative format to generate a "per" unit like $/acre or BTU/h
-- per = {u1, u2}, -- numbered table of unitcodes (u1 may be a currency symbol)
-- ... -- optional values
-- },
-- ["unitcode"] = { -- alternative format to generate an error message
-- shouldbe = "message that some other unit code should be used",
-- },
-- ["unitcode"] = { -- alternative format for combination outputs (like 'm ft')
-- combination = {u1, u2, ...}, -- numbered table of unitcodes
-- utype = "unit type", -- as for standard format
-- },
-- ["unitcode"] = { -- alternative format for output multiples (like 'ftin')
-- combination = {u1, u2, ...}, -- numbered table of unitcodes
-- multiple = {f1, f2, ...}, -- numbered table of integer factors
-- utype = "unit type", -- as for standard format
-- },
-- ...
-- }  
local ulower = mw.ustring.lower
local usub = mw.ustring.sub
local text_code  
local specials = {
-- This table is used to add extra fields when defining some units which
-- require exceptions to normal processing.
-- Each key is in the local language, while each value is fixed text.
-- However, this script should NOT be edited.
-- Instead, the translation_table in Module:Convert/text can be edited,
-- and this script will replace sections of the following with localized
-- definitions from Module:Convert/text, if given.
-- Ask for assistance at :en:Module talk:Convert.
-- LATER: It would be better if this was defined in the conversion data.
utype = {
-- ["unit type in local language"] = "name_used_in_this_script"
["fuel efficiency"] = "type_fuel_efficiency",
["length"] = "type_length",
["temperature"] = "type_temperature",
["volume"] = "type_volume",
},
ucode = {
exception = {
-- ["unit code in local language"] = "name_used_in_module_convert"
["ft"] = "integer_more_precision",
["in"] = "subunit_more_precision",
["lb"] = "integer_more_precision",
},
istemperature = {
-- Common temperature scales (not keVT or MK).
-- ["unit code in local language"] = true
["C"] = true,
["F"] = true,
["K"] = true,
["R"] = true,
},
usesymbol = {
-- Use unit symbol not name if abbr not specified.
-- ["unit code in local language"] = 1
["C"] = 1,
["F"] = 1,
["K"] = 1,
["R"] = 1,
["C-change"] = 1,
["F-change"] = 1,
["K-change"] = 1,
},
alttype = {
-- Unit has an alternate type that is a valid conversion.
-- ["unit code in local language"] = "alternate type in local language"
["Nm"] = "energy",
["ftlb"] = "torque",
["ftlb-f"] = "torque",
["ftlbf"] = "torque",
["inlb"] = "torque",
["inlb-f"] = "torque",
["inlbf"] = "torque",
["inoz-f"] = "torque",
["inozf"] = "torque",
},
},
}  
-- Module text for the local language (localization).
-- A default table of text for enwiki is provided here.
-- If needed for another wiki, wanted sections from the table can be
-- copied into translation_table in Module:Convert/text.
-- For example, copying and modifying only the titles section may give:
---
-- local translation_table = {
-- ... -- other items
-- mtext = {
-- titles = {
-- -- name_used_in_this_script = 'Title of page'
-- conversion_data = 'Modul:Convert/documentation/conversion data/dok',
-- },
-- },
-- }
local mtext = {
section_names = {
-- name_used_in_this_script = 'Section title used in conversion data'
overrides = 'Overrides',
conversions = 'Conversions',
outmultiples = 'Output multiples',
combinations = 'Combinations',
inmultiples = 'Input multiples',
defaults = 'Defaults',
links = 'Links',
perunits = 'Automatic per units',
varnames = 'Variable names',
},
titles = {
-- name_used_in_this_script = 'Title of page'
conversion_data = 'Module:Convert/documentation/conversion data',
},
messages = {
-- name_used_in_this_script = 'Error message ($1 = first parameter, $2 = second)'
m_als_bad = 'Alias has invalid text in field "$1".',
m_als_dup = 'Alias "$1" already defined.',
m_als_link = 'Alias "$1" must include a wikilink ("...") in the symlink text.',
m_als_mul = 'Alias "$1" has multiplier "$2" which is not a number.',
m_als_same = 'Should omit "$1" for alias "$2" because it is the same as its target.',
m_als_type = 'Target of alias "$1" has wrong type.',
m_als_undef = 'Primary unit must be defined before alias "=$1"',
m_cmb_miss = 'Missing unit code for a combination.',
m_cmb_none = 'No units specified for combination "$1"',
m_cmb_one = 'Only one unit specified for combination "$1"',
m_cmb_type = 'Unit "$1" in combination "$2" has wrong type.',
m_cmb_undef = 'Unit "$1" in combination "$2" not defined.',
m_cmp_def = 'Composite "$1" must specify a default unit code.',
m_cmp_int = 'Composite "$1" has components where scale ratios are not integers.',
m_cmp_inval = 'Composite "$1" has a component with an invalid scale, "$2".',
m_cmp_many = 'Composite "$1" has too many fields.',
m_cmp_miss = 'Missing unit code for a composite.',
m_cmp_order = 'Composite "$1" has components in wrong order or with invalid scales.',
m_cmp_scale = 'Alternate unit "$1" in composite "$2" has wrong scale.',
m_cmp_two = 'Composite "$1" must specify exactly two unit codes.',
m_cmp_type = 'Unit "$1" in composite "$2" has wrong type.',
m_cmp_undef = 'Unit "$1" in composite "$2" not defined.',
m_def_cond = 'Invalid condition in default "$1" for unit "$2".',
m_def_fmt = 'Default output "$1" for unit "$2" should have 2 or 3 "!".',
m_def_rpt = 'Default output "$1" for unit "$2" is repeated.',
m_def_same = 'Default output for unit "$1" is the same unit.',
m_def_type = 'Default output "$1" for unit "$2" has wrong type.',
m_def_undef = 'Default output "$1" for unit "$2" is not defined.',
m_dfs_code = 'Defaults section: no unit code specified.',
m_dfs_dup = 'Defaults section: unit "$1" has already been specified.',
m_dfs_none = 'Defaults section: unit "$1" has no default specified.',
m_dfs_sym = 'Defaults section: unit "$1" must have a symbol.',