https://northumberlandclimbing.co.uk/index.php?title=Module:Ordinal&feed=atom&action=historyModule:Ordinal - Revision history2024-03-28T21:23:30ZRevision history for this page on the wikiMediaWiki 1.35.0https://northumberlandclimbing.co.uk/index.php?title=Module:Ordinal&diff=1061&oldid=prevIanbirtwistle: 1 revision imported2020-08-02T13:17:51Z<p>1 revision imported</p>
<table class="diff diff-contentalign-left diff-editfont-monospace" data-mw="interface">
<tr class="diff-title" lang="en">
<td colspan="1" style="background-color: #fff; color: #202122; text-align: center;">← Older revision</td>
<td colspan="1" style="background-color: #fff; color: #202122; text-align: center;">Revision as of 13:17, 2 August 2020</td>
</tr><tr><td colspan="2" class="diff-notice" lang="en"><div class="mw-diff-empty">(No difference)</div>
</td></tr></table>Ianbirtwistlehttps://northumberlandclimbing.co.uk/index.php?title=Module:Ordinal&diff=1060&oldid=prevnewcastle>Traveler100: copy from Commons2019-01-06T13:02:01Z<p>copy from Commons</p>
<p><b>New page</b></p><div>--[[ <br />
<br />
This template will add the appropriate ordinal suffix to a given integer.<br />
<br />
Please do not modify this code without applying the changes first at Module:Ordinal/sandbox and testing <br />
at Module:Ordinal/sandbox/testcases and Module talk:Ordinal/sandbox/testcases.<br />
<br />
Authors and maintainers:<br />
* User:RP88<br />
<br />
]]<br />
<br />
-- =======================================<br />
-- === Dependencies ======================<br />
-- =======================================<br />
local i18n = require('Module:I18n/ordinal') -- get localized translations of ordinals<br />
local LangSwitch = require('Module:LangSwitch') -- get LangSwitch function<br />
local yesno = require('Module:Yesno') -- boolean value interpretation<br />
local formatnum = require('Module:Formatnum') -- number formatting<br />
local roman = require('Module:Roman') -- roman numeral conversion (primarily for French)<br />
<br />
-- =======================================<br />
-- === Private Functions =================<br />
-- =======================================<br />
<br />
--[[<br />
Helper function to generate superscripted content<br />
]]<br />
local function Superscript( str, superscript, nosup, period )<br />
if superscript and (not nosup) and (str ~= '') then<br />
return period .. '<sup>' .. str .. '</sup>'<br />
else<br />
return str<br />
end<br />
end<br />
<br />
<br />
--[[<br />
Helper function to call Formatnum.<br />
]]<br />
local function FormatNum( value, lang )<br />
if lang == 'roman' then<br />
return roman._Numeral(value)<br />
else<br />
return formatnum.formatNum(value, lang)<br />
end<br />
end<br />
<br />
--[[<br />
Helper function to add append a category to a message.<br />
]]<br />
local function output_cat( message, category )<br />
return message .. '[[Category:' .. category .. ']]'<br />
end<br />
<br />
--[[<br />
Helper function to handle error messages.<br />
]]<br />
local function output_error( error_str, value )<br />
error_str = '<strong class="error"><span title="Error: ' .. error_str .. '">' .. value .. '</span></strong>'<br />
return output_cat(error_str, 'Errors reported by Module Ordinal');<br />
end<br />
<br />
--[[<br />
This function is the core functionality for adding the appropriate ordinal suffix to a given integer.<br />
]]<br />
local function OrdinalCore( value, lang, style, gender, nosup ) <br />
-- Just in case someone breaks the internationalization code, fix the english scheme<br />
if i18n.SchemeFromLang['en'] == nil then<br />
i18n.SchemeFromLang['en'] = 'en-scheme'<br />
end <br />
if i18n.Scheme['en-scheme'] == nil then<br />
i18n.Scheme['en-scheme'] = {rules = 'skip-tens', superscript = true, suffix = 'th', suffix_1 = 'st', suffix_2 = 'nd', suffix_3 = 'rd'}<br />
end<br />
<br />
-- Add the default scheme (i.e. "<value>.")<br />
if i18n.SchemeFromLang['default'] == nil then<br />
i18n.SchemeFromLang['default'] = 'period-scheme'<br />
end <br />
if i18n.Scheme['period-scheme'] == nil then<br />
i18n.Scheme['period-scheme'] = {rules = 'suffix', suffix = '.'}<br />
end<br />
<br />
-- which scheme should we use to format the ordinal value? <br />
-- Use Fallback module to handle languages groups that map to a supported language<br />
local schemeSpecifier = LangSwitch._langSwitch(i18n.SchemeFromLang, lang)<br />
<br />
-- Look up scheme based on scheme specifier (and possibly style)<br />
local scheme = i18n.Scheme[schemeSpecifier .. '/' .. style] or i18n.Scheme[schemeSpecifier]<br />
<br />
-- process scheme by applying rules identified by Scheme<br />
local output = ''<br />
local period = (scheme.period and '.') or ''<br />
local rules = scheme.rules<br />
if rules == 'skip-tens' then<br />
local suffix<br />
local mod100 = math.floor(math.abs(value)) % 100<br />
if (mod100 >= 10) and (mod100 <= 19) then<br />
suffix = scheme.suffix or ''<br />
else<br />
local mod10 = math.floor(math.abs(value)) % 10<br />
suffix = scheme['suffix_'..mod10] or scheme.suffix or ''<br />
end<br />
output = FormatNum(value, scheme.formatlang or lang) .. Superscript( suffix, scheme.superscript, nosup, period)<br />
elseif rules == 'suffix' then<br />
output = FormatNum(value, scheme.formatlang or lang) .. Superscript( scheme.suffix or '', scheme.superscript, nosup, period)<br />
elseif rules == 'prefix' then<br />
output = (scheme.prefix or '') .. FormatNum(value, scheme.formatlang or lang)<br />
elseif rules == 'mod10-suffix' then<br />
local index = math.floor(math.abs(value)) % 10<br />
local suffix = scheme['suffix_'..index] or scheme.suffix or ''<br />
output = FormatNum(value, scheme.formatlang or lang) .. Superscript( suffix, scheme.superscript, nosup, period)<br />
elseif rules == 'gendered-suffix' then<br />
local suffix = scheme['suffix_'..gender] or scheme.suffix or ''<br />
output = FormatNum(value, scheme.formatlang or lang) .. Superscript( suffix, scheme.superscript, nosup, period)<br />
elseif rules == 'gendered-suffix-one' then<br />
local suffix<br />
if value == 1 then<br />
suffix = scheme['suffix_1_'..gender] or scheme['suffix_1'] or scheme.suffix or ''<br />
else<br />
suffix = scheme['suffix_'..gender] or scheme.suffix or ''<br />
end<br />
output = FormatNum(value, scheme.formatlang or lang) .. Superscript( suffix, scheme.superscript, nosup, period)<br />
elseif rules == 'gendered-suffix-n' then<br />
local suffix<br />
if value <= 9 then<br />
suffix = scheme['suffix_'..value..'_'..gender] or scheme['suffix_'..value] or scheme['suffix_'..gender] or scheme.suffix or ''<br />
else<br />
suffix = scheme['suffix_'..gender] or scheme.suffix or ''<br />
end<br />
output = FormatNum(value, scheme.formatlang or lang) .. Superscript( suffix, scheme.superscript, nosup, period)<br />
elseif rules == 'suffix-one' then<br />
local prefix, suffix<br />
if value == 1 then<br />
prefix = scheme['prefix_1'] or scheme.prefix or ''<br />
suffix = scheme['suffix_1'] or scheme.suffix or ''<br />
else<br />
prefix = scheme.prefix or ''<br />
suffix = scheme.suffix or ''<br />
end<br />
output = prefix .. FormatNum(value, scheme.formatlang or lang) .. Superscript( suffix, scheme.superscript, nosup, period)<br />
elseif rules == 'mod10-gendered-suffix-skip-tens' then<br />
local suffix<br />
local mod100 = math.floor(math.abs(value)) % 100<br />
if (mod100 >= 10) and (mod100 <= 19) then<br />
suffix = scheme['suffix_'..gender] or scheme.suffix or ''<br />
else<br />
local mod10 = math.floor(math.abs(value)) % 10<br />
suffix = scheme['suffix_'..mod10..'_'..gender] or scheme['suffix_'..mod10] or scheme['suffix_'..gender] or scheme.suffix or ''<br />
end<br />
output = FormatNum(value, scheme.formatlang or lang) .. Superscript( suffix, scheme.superscript, nosup, period)<br />
elseif rules == 'uk-rules' then<br />
local suffix<br />
local mod100 = math.floor(math.abs(value)) % 100<br />
local mod1000 = math.floor(math.abs(value)) % 1000<br />
if (mod1000 == 0) then<br />
suffix = scheme['suffix_1000_'..gender] or scheme.suffix or ''<br />
elseif (mod100 == 40) then<br />
suffix = scheme['suffix_40_'..gender] or scheme.suffix or ''<br />
elseif (mod100 >= 10) and (mod100 <= 19) then<br />
suffix = scheme['suffix_'..gender] or scheme.suffix or ''<br />
else<br />
local mod10 = math.floor(math.abs(value)) % 10<br />
suffix = scheme['suffix_'..mod10..'_'..gender] or scheme['suffix_'..mod10] or scheme['suffix_'..gender] or scheme.suffix or ''<br />
end<br />
output = FormatNum(value, scheme.formatlang or lang) .. Superscript( suffix, scheme.superscript, nosup, period)<br />
else<br />
output = FormatNum(value, lang)<br />
end <br />
<br />
return output<br />
end<br />
<br />
-- =======================================<br />
-- === Public Functions ==================<br />
-- =======================================<br />
<br />
local p = {}<br />
--[[<br />
Ordinal<br />
<br />
This function converts an integer value into a numeral followed by ordinal indicator. The output string might <br />
contain HTML tags unless you set nosup=y.<br />
<br />
Usage:<br />
{{#invoke:Ordinal|Ordinal|1=|lang=|style=|gender=|nosup=|debug=}}<br />
{{#invoke:Ordinal|Ordinal}} - uses the caller's parameters<br />
<br />
Parameters<br />
1: Positive integer number. <br />
lang: language<br />
style: Presentation style. Different options for different languages. In English there is "style=d" adding -d suffixes to all numbers.<br />
gender: Gender is used in French and Polish language versions. Genders: m for male, f for female and n for neuter. <br />
nosup: Set nosup=y to display the ordinals without superscript.<br />
debug: Set debug=y to output error messages.<br />
<br />
Error Handling:<br />
Unless debug=y, any error results in parameter 1 being echoed to the output. This reproduces the behavior of the original Ordinal template.<br />
]]<br />
function p.Ordinal( frame )<br />
-- if no argument provided than check parent template/module args<br />
local args = frame.args<br />
if args[1]==nil then<br />
args = frame:getParent().args <br />
end<br />
<br />
-- if we don't have a specified language, attempt to use the user's language<br />
local lang = args.lang<br />
if not lang or lang == '' or not mw.language.isValidCode( lang ) then<br />
lang = frame:preprocess('{{int:lang}}')<br />
end<br />
<br />
local nosup = yesno(args["nosup"] or '', false) -- nosup can be true or false<br />
local debugging = yesno(args["debug"], false) -- debugging can be nil, true, or false<br />
<br />
-- also enable debugging if debug is unspecified, and "nosup" is false<br />
debugging = debugging or ((debugging == nil) and not nosup)<br />
<br />
local output = p._Ordinal( <br />
args[1], -- positive integer number<br />
lang, -- language<br />
args["style"], -- allows to set presentation style<br />
args["gender"], -- allows to specify gender (m, f, or n)<br />
nosup, -- set nosup to "y" to suppress superscripts<br />
debugging -- Set debug=y to output error messages<br />
)<br />
<br />
-- Add maintenance category<br />
if (i18n.SchemeFromLang[lang] == nil) and debugging then <br />
output = output_cat(output, 'Pages with calls to Module Ordinal using an unsupported language')<br />
end<br />
<br />
return output<br />
end<br />
<br />
<br />
--[[<br />
This function will add the appropriate ordinal suffix to a given integer. <br />
<br />
Parameters<br />
input: Numeral as a positive integer or string.<br />
lang: Language code as a string (e.g. 'en', 'de', etc.).<br />
style: Presentation style as a string (e.g. 'd', 'roman', etc.).<br />
gender: Gender as a string ('m', 'f', 'n'). Use empty string '' to leave gender unspecified.<br />
nosup: Boolean, set to true to force the ordinals to display without superscript.<br />
debug: Boolean, set to true to output error messages.<br />
<br />
Error Handling:<br />
Unless debug is true, any error results in value being echoed to the output.<br />
]]<br />
function p._Ordinal( input, lang, style, gender, nosup, debugging ) <br />
local output = input<br />
<br />
if input then<br />
local value = tonumber(input)<br />
if value and (value > 0) then<br />
<br />
-- Normalize style, the style 'roman year' is an alias for 'roman'<br />
style = string.lower(style or '')<br />
if style == 'roman year' then<br />
style = 'roman'<br />
end<br />
<br />
-- Normalize gender parameter<br />
gender = string.lower(gender or '')<br />
if (gender ~= 'm') and (gender ~= 'f') and (gender ~= 'n') then<br />
gender = ''<br />
end<br />
<br />
-- if no language is specified, default to english (caller might want to get user's language)<br />
if not lang or lang == '' then<br />
lang = 'en';<br />
end<br />
<br />
output = OrdinalCore( value, lang, style, gender, nosup )<br />
else<br />
if debugging then<br />
output = output_error( "not a number", input )<br />
end<br />
end<br />
else<br />
if debugging then<br />
output = output_error( "not a number", '' )<br />
end<br />
end<br />
<br />
return output<br />
end<br />
<br />
return p</div>newcastle>Traveler100