Module:number list/show: Difference between revisions

Jump to navigation Jump to search
no edit summary
(Created page with "local export = {} local m_number_list = require("Module:number list") local function compare_numbers(a, b) a, b = tonumber(a), tonumber(b) return a < b end function expo...")
 
No edit summary
Line 3: Line 3:
local m_number_list = require("Module:number list")
local m_number_list = require("Module:number list")


local function compare_numbers(a, b)
local function link_forms(forms, m_data, lang)
a, b = tonumber(a), tonumber(b)
if type(forms) ~= "table" then
return a < b
forms = {forms}
end
local seen_forms, forms_by_tag, seen_tags, combined_tags_to_tag_lists = m_number_list.group_numeral_forms_by_tag(forms)
local formatted_forms = {}
for _, tag in ipairs(seen_tags) do
local formatted_tag_forms = {}
 
for _, formobj in ipairs(forms_by_tag[tag]) do
table.insert(formatted_tag_forms, m_number_list.format_formobj(formobj, m_data, lang))
end
formatted_tag_forms = table.concat(formatted_tag_forms, ", ")
if tag == "" then
if #seen_tags == 1 then
table.insert(formatted_forms, formatted_tag_forms)
else
table.insert(formatted_forms, ("(default): %s"):format(formatted_tag_forms))
end
else
local tag_list = combined_tags_to_tag_lists[tag]
table.insert(formatted_forms, ("%s: %s"):format(table.concat(tag_list, " / "), formatted_tag_forms))
end
end
 
return table.concat(formatted_forms, "<br />")
end
end


function export.print_table(language_code, module)
local function num_to_ordinal(numstr)
local module = require(module)
if numstr:find("1$") then
return numstr .. "st"
local lang = require("Module:languages").getByCode(language_code)
elseif numstr:find("2$") then
return numstr .. "nd"
elseif numstr:find("3$") then
return numstr .. "rd"
else
return numstr .. "th"
end
end
 
local function print_full_table(lang, m_data)
local full_link = require("Module:links").full_link
local full_link = require("Module:links").full_link
local tag_text = require("Module:script utilities").tag_text
local tag_text = require("Module:script utilities").tag_text
local function link(form)
return full_link({ lang = lang, term = form})
end
local function tag(form)
local function tag(form)
return tag_text(form, lang)
return tag_text(form, lang)
end
end
 
local form_types = m_number_list.get_number_types(language_code)
local form_types = m_number_list.get_number_types(m_data)
local numeral_index = 1
local numeral_index = 1
table.insert(form_types, numeral_index,
table.insert(form_types, numeral_index,
{key = "numeral", display = "Numeral"})
{key = "numeral", display = "Numeral"})
table.insert(form_types, {key = "wplink", display = "Wikipedia link"})
local wplink_index = #form_types
 
local number_type_indices = {}
local number_type_indices = {}
for number, data in pairs(module.numbers) do
for number, data in pairs(m_data.numbers) do
for i, form_type in pairs(form_types) do
for i, form_type in pairs(form_types) do
if data[form_type.key] then
if data[form_type.key] then
Line 34: Line 65:
end
end
end
end
 
local numeral_config = module.numeral_config
local numeral_config = m_data.numeral_config
if numeral_config then
if numeral_config then
number_type_indices[numeral_index] = true
number_type_indices[numeral_index] = true
end
end
 
local has_wplink_column = number_type_indices[wplink_index]
 
number_type_indices = require("Module:table").keysToList(number_type_indices)
number_type_indices = require("Module:table").keysToList(number_type_indices)
 
local Array = require("Module:array")
local Array = require("Module:array")
local output = Array()
local output = Array()
 
local function header(content)
local function header(content)
output:insert(("! %s"):format(content))
output:insert(("! %s"):format(content))
end
end
 
local function cell(content)
local function cell(content)
output:insert(("| %s"):format(content))
output:insert(("| %s"):format(content))
end
end
 
local function row(content)
local function row(content)
output:insert("|-\n")
output:insert("|-\n")
Line 59: Line 92:
end
end
end
end
 
local data_module_name = m_number_list.get_data_module_name(lang:getCode())
if data_module_name then
output:insert('<div class="floatright"><sup>(<span class="plainlinks">[' ..
tostring(mw.uri.fullUrl(data_module_name, { action = "edit" })) ..
" edit]</span>)</sup></div>")
end
 
output:insert('{| class="wikitable"')
output:insert('{| class="wikitable"')
 
-- Add headers.
-- Add headers.
header("Number")
header("Number")
Line 67: Line 107:
header(m_number_list.display_number_type(form_types[index]))
header(m_number_list.display_number_type(form_types[index]))
end
end
 
local errors = Array()
local errors = Array()
 
for number, data in require("Module:table").sortedPairs(module.numbers, compare_numbers) do
for number, data in require("Module:table").sortedPairs(m_data.numbers, m_number_list.numbers_less_than) do
local number_string
local function check_string(val)
if type(number) == "number" then
if type(val) ~= "string" then
number_string = ("%.0f"):format(number)
error(("For number %s, Expected string but saw '%s"):format(number, mw.dumpObject(val)))
else
end
number_string = number
end
end
local number_string = m_number_list.format_fixed(number)
row(number)
 
row(m_number_list.format_number_for_display(number_string))
 
local numeral
local numeral
if numeral_config then
if numeral_config then
numeral = m_number_list.generate_decimal_numeral(numeral_config, number)
numeral = m_number_list.generate_non_arabic_numeral(numeral_config, number_string)
elseif data.numeral then
elseif data.numeral then
numeral = data.numeral
numeral = data.numeral
end
end
if numeral then
if numeral then
check_string(numeral)
numeral = tag(numeral)
numeral = tag(numeral)
cell(numeral or "")
cell(numeral or "")
end
end
 
for _, i in ipairs(number_type_indices) do
for _, i in ipairs(number_type_indices) do
if i ~= numeral_index then
if i ~= numeral_index and i ~= wplink_index then
local form = data[form_types[i].key]
local form = data[form_types[i].key]
cell(type(form) == "table" and Array(form):map(link):concat(", ")
cell(form and link_forms(form, data, lang) or "")
or form and link(form)
or "")
end
end
end
end
 
if data.wplink then
check_string(data.wplink)
cell(("[[w:%s:%s|%s]]"):format(lang:getCode(), data.wplink, data.wplink))
elseif has_wplink_column then
cell("")
end
 
-- Check for numerical indices, which are syntax errors.
-- Check for numerical indices, which are syntax errors.
for i, word in ipairs(data) do
for i, word in ipairs(data) do
errors:insert({ number = number, word = word })
if type(word) == "string" then
errors:insert({ number = number_string, word = word })
end
end
end
end
end
 
output:insert('|}')
output:insert('|}')
 
if #errors > 0 then
if #errors > 0 then
output:insert(
output:insert(
Line 116: Line 164:
:map(
:map(
function(data)
function(data)
return data.number .. ": " .. link(data.word)
return data.number .. ": " .. data.word
end)
end)
:concat ", "
:concat ", "
Line 122: Line 170:
.. lang:getCode() .. "]]</span>")
.. lang:getCode() .. "]]</span>")
end
end
 
return output:concat("\n")
return output:concat("\n")
end
end


function export.number_table(frame)
local params = {
[1] = {required = true, default = "und"},
[2] = {required = true, default = "cardinal"},
}
local args = require("Module:parameters").process(frame:getParent().args, params)
local langcode = args[1]
local lang = require("Module:languages").getByCode(langcode, 1)
local data_module_name = m_number_list.get_data_module_name(langcode, "must exist")
local m_data = require(data_module_name)
if args[2] == "full" then
return print_full_table(lang, m_data)
end
local parts = {}
local function ins(text)
table.insert(parts, text)
end
-- Find minimum and maximum attested number and row.
local min_num
local max_num = 0
local min_row
local max_row = 0
for i = 0, 9 do
for j = 0, 9 do
local num = i * 10 + j
local num_data = m_number_list.lookup_data(m_data, tostring(num))
local forms = num_data and num_data[args[2]]
if forms and #forms > 0 then
min_num = min_num or num
max_num = num
min_row = min_row or i
max_row = i
end
end
end
min_num = min_num or 0
min_row = min_row or 0
ins([=[<div class="NavFrame" style="">
<div class="NavHead">]=])
ins(lang:getCanonicalName())
ins(" ")
local min_ord = num_to_ordinal(tostring(min_num))
local max_ord = num_to_ordinal(tostring(max_num))
if args[2] == "ordinal" then
ins(("ordinal numbers from %s to %s"):format(min_ord, max_ord))
elseif args[2] == "ordinal_abbr" then
ins(("ordinal abbreviations from %s to %s"):format(min_ord, max_ord))
else
ins(args[2])
ins((" numbers from %s to %s"):format(min_num, max_num))
end
ins([=[</div>
<div class="NavContent" style="">
{| class="wikitable" style="width:100%;height:100%;font-size:8pt"
!
!—0
!—1
!—2
!—3
!—4
!—5
!—6
!—7
!—8
!—9
]=])
for i = min_row, max_row do
ins("|-\n")
ins("! style='white-space: nowrap;' | " .. i .. "—\n")
for j = 0, 9 do
ins(j == 0 and "| " or "|| ")
local num_data = m_number_list.lookup_data(m_data, tostring(i * 10 + j))
local forms = num_data and num_data[args[2]]
if forms then
ins(link_forms(forms, m_data, lang))
else
ins("—")
end
end
ins("\n")
end
ins("|}\n</div></div>")
return table.concat(parts)
end
-- Called from [[Module:documentation/functions/number list]].
function export.table(frame)
function export.table(frame)
local language_code
local language_code
Line 131: Line 270:
language_code = frame.args[1]
language_code = frame.args[1]
end
end
 
local module
local module
if not language_code then
if not language_code then
Line 144: Line 283:
end
end
end
end
 
return export.print_table(language_code, module)
local lang = require("Module:languages").getByCode(language_code, true)
local m_data = require(module)
return print_full_table(lang, m_data)
end
end


return export
return export

Navigation menu