Модул:number list

Извор: Викиречник
Иди на навигацију Иди на претрагу

This module generates navigation boxes for number words, such as three, third, thrice, and triple. It is used by {{number box}}, {{cardinalbox}}, {{ordinalbox}}, and {{adverbialbox}}.


local export = {}

local m_links = require("Модул:links")

local form_types = {
	{key = "cardinal", display = "[[кардинални број|Кардинал]]"},
	{key = "ordinal", display = "[[редни број|Редни]]"},
	{key = "adverbial", display = "[[adverbial number|Adverbial]]"},
	{key = "multiplier", display = "[[multiplier|Multiplier]]"},
	{key = "distributive", display = "[[distributive number|Distributive]]"},
	{key = "collective", display = "[[collective number|Collective]]"},
	{key = "fractional", display = "[[fractional|Fractional]]"},
}


local function multiple_num_links(terms, lang)
	local links = {}
	for i, term in ipairs(terms) do
		links[i] = m_links.language_link{lang = lang, term = term, alt = "[" .. string.char(96 + i) .. "]", tr = "-"}
	end
	return "<sup>" .. table.concat(links, ", ") .. "</sup>"
end


local function add_commas(number_string)
	local with_commas = ""
	local i = 0
	local len = #number_string
	repeat
		i = i - 3
		with_commas = number_string:sub(i, i + 2) .. with_commas
		if i > -len then
			with_commas = "," .. with_commas
		end
	until i < -len
	return with_commas
end


function export.show_box(frame)
	local full_link = m_links.full_link
	
	local params = {
		[1] = {required = true},
		[2] = {required = true, type = "number"},
	}
	
	local args = require("Модул:parameters").process(frame:getParent().args, params)
	
	local lang = args[1] or "und"
	local cur_num = args[2] or 2
	lang = require("Модул:languages").getByCode(lang) or error("The language code \"" .. lang .. "\" is not valid.")
	
	-- Get the data from the data module
	local module_name = "Модул:number list/data/" .. lang:getCode()
	local m_data = require(module_name)
	local cur_data = m_data.numbers[cur_num]
	
	if not cur_data then
		error('Број "' .. cur_num .. '" није нађен у табели "бројева" у [[' .. module_name .. ']].')
	end
	
	-- Go over each number and make links
	local forms = {}
	local cur_type
	local full_pagename = (mw.title.getCurrentTitle().nsText=="Реконструкција" and "*" or "") .. mw.title.getCurrentTitle().subpageText
	
	for _, form_type in ipairs(form_types) do
		local numeral = cur_data[form_type.key]
		if numeral then
			local form = {}
			if type(numeral) == "string" then
				table.insert(form, full_link({lang = lang, term = numeral, tr = cur_data[form_type.key .. "_tr"]}))
			
				-- If this number is the current page, then store the key for later use
				if lang:makeEntryName(numeral) == full_pagename then
					cur_type = form_type.key
				end
			elseif type(numeral) == "table" then
				local numerals = numeral
				for _, numeral in ipairs(numerals) do
					table.insert(form, full_link({lang = lang, term = numeral }))
					
					-- If this number is the current page, then store the key for later use
					if lang:makeEntryName(numeral) == full_pagename then
						cur_type = form_type.key
					end
				end
			end
			
			table.insert(forms, " &nbsp;&nbsp;&nbsp; ''" .. form_type.display .. "'': " .. table.concat(form, ", "))
		end
	end
	
	if not cur_type and mw.title.getCurrentTitle().nsText ~= "Шаблон" then
		error("Текући назив стране  није нашла ниједан број у листи  [[" .. module_name .. "]] за " .. cur_num .. ". Проверите модул податак или у спеловању стране.")
	end
	
	-- Current number in header
	-- Prevent large numbers, such as 100 trillion ([[རབ་བཀྲམ་ཆེན་པོ]]) from being
	-- displayed in scientific notation (1+e14).
	local cur_display = ("%i"):format(cur_num)
	---[[
	if cur_num >= 1000 then
		cur_display = add_commas(cur_display)
	end
	--]]
	
	if cur_data["numeral"] then
		cur_display = full_link({lang = lang, alt = tostring(cur_data["numeral"]), tr = "-"}) .. "<br/><span style=\"font-size: smaller;\">" .. cur_display .. "</span>"
	end
	
	-- Link to previous number
	local prev_data = m_data.numbers[cur_num - 1]
	local prev_display = ""
	
	--	Current format:
	--		if multiple terms:
	--			<sup>[a], [b], ...</sup> ← <numeral>
	--		else
	--			← <numeral>
	if prev_data and prev_data[cur_type] then
		if type(prev_data[cur_type]) == "table" then
			prev_display = multiple_num_links(prev_data[cur_type], lang) .. "&nbsp;←&nbsp;&nbsp;" .. (cur_num - 1)
		else
			prev_display = m_links.language_link {lang = lang, term = prev_data[cur_type], alt = "&nbsp;←&nbsp;&nbsp;" .. (cur_num - 1), tr = "-"}
		end
	end
	
	-- Link to next number
	local next_data = m_data.numbers[cur_num + 1]
	local next_display = ""
	
	--	Current format:
	--		if multiple terms:
	--			<numeral> → <sup>[a], [b], ...</sup>
	--		else
	--			<numeral> →
	if next_data and next_data[cur_type] then
		if type(next_data[cur_type]) == "table" then
			next_display = (cur_num + 1) .. "&nbsp;&nbsp;→&nbsp;" .. multiple_num_links(next_data[cur_type], lang)
		else
			next_display = m_links.language_link {lang = lang, term = next_data[cur_type], alt = (cur_num + 1) .. "&nbsp;&nbsp;→&nbsp;", tr = "-"}
		end
	end
	
	-- Link to number times ten and divided by ten
	-- Show this only if the number is a power of ten times a number 1-9 (that is, of the form x000...)
	local up_display
	local down_display
	
	-- This test *could* be done numerically, but this is nice and simple and it works.
	if tostring(cur_num):find("^[1-9]0*$") then
		up_num = cur_num * 10
		local up_data = m_data.numbers[up_num]
		
		if up_data and up_data[cur_type] then
			if type(up_data[cur_type]) == "table" then
				up_display = up_num .. multiple_num_links(up_data[cur_type], lang)
			else
				up_display = full_link({lang = lang, term = up_data[cur_type], alt = up_num, tr = "-"})
			end
		end
		
		-- Only divide by 10 if the number is at least 10
		if cur_num >= 10 then
			local down_num = cur_num / 10
			local down_data = m_data.numbers[down_num]
			
			if down_data and down_data[cur_type] then
				if type(down_data[cur_type]) == "table" then
					down_display = down_num .. multiple_num_links(down_data[cur_type], lang)
				else
					down_display = full_link({lang = lang, term = down_data[cur_type], alt = down_num, tr = "-"})
				end
			end
		end
	end
	
	local canonical_name = lang:getCanonicalName()
	local appendix1 = canonical_name .. ' број'
	local appendix2 = canonical_name .. ' бројеви'
	local appendix
	local title
	if mw.title.new(appendix1, "Додатак").exists then
		appendix = appendix1
	elseif mw.title.new(appendix2, "Додатак").exists then
		appendix = appendix2
	end
	
	if appendix then
		title = '[[Додатак:' .. appendix .. '|' .. appendix2 .. ']]'
	else
		title = appendix2
	end
	
	local edit_link = ' (<span class="plainlinks">[' ..
		tostring(mw.uri.fullUrl(module_name, "action=edit")) ..
		" унос]</span>)"
	
	return [=[{| class="floatright" cellpadding="5" cellspacing="0" style="background: #ffffff; border: 1px #aaa solid; border-collapse: collapse; margin-top: .5em;" rules="all" 
|+ ''']=] .. title .. edit_link .. "'''" ..
(up_display and [=[

|- style="text-align: center; background:#dddddd;"
|
| style="font-size:smaller;" | ]=] .. up_display .. [=[

|
]=] or "\n") .. [=[|- style="text-align: center;"
| style="min-width: 6em; font-size:smaller; background:#dddddd;" | ]=] .. prev_display .. [=[

! style="min-width: 6em; font-size:larger;" | ]=] .. cur_display .. [=[

| style="min-width: 6em; font-size:smaller; background:#dddddd;" | ]=] .. next_display .. [=[

]=] .. (down_display and [=[|- style="text-align: center; background:#dddddd;"
|
| style="font-size:smaller;" | ]=] .. down_display .. [=[

|
]=] or "") .. [=[|-
| colspan="3" | ]=] .. table.concat(forms, "<br/>") .. [=[

|}]=]	
end


local trim = mw.text.trim

-- Assumes string or nil (or false), the types that can be found in an args table.
local function if_not_empty(val)
	if val and trim(val) == "" then
		return nil
	else
		return val
	end
end


function export.show_box_manual(frame)
	local m_links = require("Модул:links")
	local num_type = frame.args["type"]
	
	local args = {}
	--cloning parent's args while also assigning nil to empty strings
	for pname, param in pairs(frame:getParent().args) do
		args[pname] = if_not_empty(param)
	end
	
	local lang = args[1] or (mw.title.getCurrentTitle().nsText == "Шаблон" and "und") or error("Код језика није наведен. Проследите параметар 1 у образац.")
	local sc = args["sc"]; 
	local headlink = args["headlink"]
	local wplink = args["wplink"]
	local alt = args["alt"]
	local tr = args["tr"]
	
	local prev_symbol = if_not_empty(args[2])
	local cur_symbol = if_not_empty(args[3]);
	local next_symbol = if_not_empty(args[4])
	
	local prev_term = if_not_empty(args[5])
	local next_term = if_not_empty(args[6])	

	local cardinal_term = args["card"]; local cardinal_alt = args["cardalt"]; local cardinal_tr = args["cardtr"]
	
	local ordinal_term = args["ord"]; local ordinal_alt = args["ordalt"]; local ordinal_tr = args["ordtr"]
	
	local adverbial_term = args["adv"]; local adverbial_alt = args["advalt"]; local adverbial_tr = args["advtr"]
	
	local multiplier_term = args["mult"]; local multiplier_alt = args["multalt"]; local multiplier_tr = args["multtr"]
	
	local distributive_term = args["dis"]; local distributive_alt = args["disalt"]; local distributive_tr = args["distr"]
	
	local collective_term = args["coll"]; local collective_alt = args["collalt"]; local collective_tr = args["colltr"]
	
	local fractional_term = args["frac"]; local fractional_alt = args["fracalt"]; local fractional_tr = args["fractr"]
	
	local optional1_title = args["opt"]
	local optional1_term = args["optx"]; local optional1_alt = args["optxalt"]; local optional1_tr = args["optxtr"]
	
	local optional2_title = args["opt2"]
	local optional2_term = args["opt2x"]; local optional2_alt = args["opt2xalt"]; local optional2_tr = args["opt2xtr"]
	

	lang = require("Модул:languages").getByCode(lang) or error("The language code \"" .. lang .. "\" is not valid.")
	sc = (sc and (require("Модул:scripts").getByCode(sc) or error("The script code \"" .. sc .. "\" is not valid.")) or nil)
	
	require("Модул:debug").track("number list/" .. lang:getCode())
	
	if sc then
		require("Модул:debug").track("number list/sc")
	end
	
	if headlink then
		require("Модул:debug").track("number list/headlink")
	end
	
	if wplink then
		require("Модул:debug").track("number list/wplink")
	end
	
	if alt then
		require("Модул:debug").track("number list/alt")
	end
	
	if cardinal_alt or ordinal_alt or adverbial_alt or multiplier_alt or distributive_alt or collective_alt or fractional_alt or optional1_alt or optional2_alt then
		require("Модул:debug").track("number list/xalt")
	end
	
	local lang_type = lang:getType()
	local subpage = mw.title.getCurrentTitle().subpageText
	local is_reconstructed = lang_type == "reconstructed" or mw.title.getCurrentTitle().nsText == "Reconstruction"
	alt = alt or (is_reconstructed and "*" or "") .. subpage
	
	if num_type == "cardinal" then
		cardinal_term = (is_reconstructed and "*" or "") .. subpage
		cardinal_alt = alt
		cardinal_tr = tr
	elseif num_type == "ordinal" then
		ordinal_term = (is_reconstructed and "*" or "") .. subpage
		ordinal_alt = alt
		ordinal_tr = tr
	end
	
	local header = lang:getCanonicalName() .. " " .. num_type .. " бројеви"
	
	if headlink then
		header = "[[" .. headlink .. "|" .. header .. "]]"
	end
	
	local previous = ""
	
	if prev_term or prev_symbol then
		previous = m_links.full_link({lang = lang, sc = sc, term = prev_term, alt = "&nbsp;&lt;&nbsp;&nbsp;" .. prev_symbol, tr = "-"})
	end
	
	local current = m_links.full_link({lang = lang, sc = sc, alt = cur_symbol, tr = "-"})
	
	local next = ""
	
	if next_term or next_symbol then
		next = m_links.full_link({lang = lang, sc = sc, term = next_term, alt = next_symbol .. "&nbsp;&nbsp;&gt;&nbsp;", tr = "-"})
	end
	
	local forms = {}
	
	if cardinal_term then
		table.insert(forms, " &nbsp;&nbsp;&nbsp; ''[[кардинални број|Кардинал]]'' : " .. m_links.full_link({lang = lang, sc = sc, term = cardinal_term, alt = cardinal_alt, tr = cardinal_tr}))
	end
	
	if ordinal_term then
		table.insert(forms, " &nbsp;&nbsp;&nbsp; ''[[редни број|Редни]]'' : " .. m_links.full_link({lang = lang, sc = sc, term = ordinal_term, alt = ordinal_alt, tr = ordinal_tr}))
	end
	
	if adverbial_term then
		table.insert(forms, " &nbsp;&nbsp;&nbsp; ''[[adverbial number|Adverbial]]'' : " .. m_links.full_link({lang = lang, sc = sc, term = adverbial_term, alt = adverbial_alt, tr = adverbial_tr}))
	end
	
	if multiplier_term then
		table.insert(forms, " &nbsp;&nbsp;&nbsp; ''[[multiplier|Multiplier]]'' : " .. m_links.full_link({lang = lang, sc = sc, term = multiplier_term, alt = multiplier_alt, tr = multiplier_tr}))
	end
	
	if distributive_term then
		table.insert(forms, " &nbsp;&nbsp;&nbsp; ''[[distributive number|Distributive]]'' : " .. m_links.full_link({lang = lang, sc = sc, term = distributive_term, alt = distributive_alt, tr = distributive_tr}))
	end
	
	if collective_term then
		table.insert(forms, " &nbsp;&nbsp;&nbsp; ''[[collective number|Collective]]'' : " .. m_links.full_link({lang = lang, sc = sc, term = collective_term, alt = collective_alt, tr = collective_tr}))
	end
	
	if fractional_term then
		table.insert(forms, " &nbsp;&nbsp;&nbsp; ''[[разломак|Разломак]]'' : " .. m_links.full_link({lang = lang, sc = sc, term = fractional_term, alt = fractional_alt, tr = fractional_tr}))
	end
	
	if optional1_title then
		table.insert(forms, " &nbsp;&nbsp;&nbsp; ''" .. optional1_title .. "'' : " .. m_links.full_link({lang = lang, sc = sc, term = optional1_term, alt = optional1_alt, tr = optional1_tr}))
	end
	
	if optional2_title then
		table.insert(forms, " &nbsp;&nbsp;&nbsp; ''" .. optional2_title .. "'' : " .. m_links.full_link({lang = lang, sc = sc, term = optional2_term, alt = optional2_alt, tr = optional2_tr}))
	end
	
	local footer = ""
	
	if wplink then
		footer =
			"[[w:" .. lang:getCode() .. ":Main Page|" .. lang:getCanonicalName() .. " Wikipedia]] article on " ..
			m_links.full_link({lang = lang, sc = sc, term = "w:" .. lang:getCode() .. ":" .. wplink, alt = alt, tr = tr})
	end
	
	return [=[{| class="floatright" cellpadding="5" cellspacing="0" style="background: #ffffff; border: 1px #aaa solid; border-collapse: collapse; margin-top: .5em;" rules="all" 
|+ ''']=] .. header .. [=['''
|-
| style="width: 64px; background:#dddddd; text-align: center; font-size:smaller;" | ]=] .. previous .. [=[

! style="width: 98px; text-align: center; font-size:larger;" | ]=] .. current .. [=[

| style="width: 64px; text-align: center; background:#dddddd; font-size:smaller;" | ]=] .. next .. [=[

|-
| colspan="3" | ]=] .. table.concat(forms, "<br/>") .. [=[

|-
| colspan="3" style="text-align: center; background: #dddddd;" | ]=] .. footer .. [=[

|}]=]
end

return export