Module:DateI18n: Difference between revisions
From Neodyland Wiki
More actions
m allow "case=-" to mean the same as "case=" |
Fulfilling edit request by CalendulaAsteraceae. |
||
| (2 intermediate revisions by 2 users not shown) | |||
| Line 8: | Line 8: | ||
This module is intended for processing of date strings. | This module is intended for processing of date strings. | ||
Please do not modify this code without applying the changes first at Module: | Please do not modify this code without applying the changes first at Module:DateI18n/sandbox | ||
at Module: | and testing at Module:DateI18n/sandbox/testcases and Module talk:DateI18n/sandbox/testcases. | ||
Authors and maintainers: | Authors and maintainers: | ||
* User:Parent5446 - original version of the function mimicking | * User:Parent5446 - original version of the function mimicking Template:ISOdate | ||
* User:Jarekt - original version of the functions mimicking | * User:Jarekt - original version of the functions mimicking Template:Date | ||
]] | ]] | ||
| Line 20: | Line 20: | ||
-- ======================================= | -- ======================================= | ||
require(' | require('strict') | ||
-- ======================================= | -- ======================================= | ||
| Line 277: | Line 277: | ||
end | end | ||
return timeStamp, datecode | return timeStamp, datecode | ||
end | |||
local function isValidLangCode(lang) | |||
if not lang then | |||
return false | |||
end | |||
lang = mw.text.trim(lang) | |||
return lang ~= '' and lang ~= '⧼Lang⧽' and mw.language.isValidCode(lang) | |||
end | end | ||
| Line 299: | Line 307: | ||
* class: CSS class for the <time> node, use "" for no metadata at all | * class: CSS class for the <time> node, use "" for no metadata at all | ||
]] | ]] | ||
function p._Date(datevec, lang, case, class, trim_year) | function p._Date(datevec, lang, case, class, trim_year) | ||
-- make sure inputs are in the right format | -- make sure inputs are in the right format | ||
if not | |||
lang = mw.getCurrentFrame():callParserFunction( "int", "lang" ) -- | -- set language | ||
if not isValidLangCode(lang) then | |||
-- get user's chosen language | |||
-- equivalent to {{int:lang}} | |||
lang = mw.getCurrentFrame():callParserFunction("int", "lang") | |||
if not isValidLangCode(lang) then | |||
-- if that doesn't work, use the project language | |||
-- this is useful on projects which import this module from Commons | |||
lang = mw.language.getContentLanguage().code | |||
if not isValidLangCode(lang) then | |||
-- if that doesn't work, use English | |||
lang = "en" | |||
end | |||
end | |||
end | end | ||
if lang == 'be-tarask' then | if lang == 'be-tarask' then | ||
| Line 391: | Line 414: | ||
-- =========================================================================== | -- =========================================================================== | ||
-- === | -- === Version of the function to be called from template namespace | ||
-- =========================================================================== | -- =========================================================================== | ||
| Line 410: | Line 433: | ||
]] | ]] | ||
function p.Date(frame) | function p.Date(frame) | ||
-- get args | |||
local args = {} | local args = {} | ||
for | for key, value in pairs(frame.args) do | ||
local trimmed_key = string.gsub(string.lower(mw.text.trim(key)), ' ', '_') | |||
args[ | local trimmed_value = mw.text.trim(value) | ||
if trimmed_key ~= 'class' and trimmed_value == '' then | |||
trimmed_value = nil | |||
end | |||
args[trimmed_key] = trimmed_value | |||
end | end | ||
-- default values | |||
-- Allows to set the html class of the time node where the date is included. This is useful for microformats. | |||
args.class = args.class or '-' | |||
if args.class == '' then | |||
args.class = 'dtstart' | |||
end | |||
-- By default, pad one- and two-digit years to be 4 digits long, while keeping three-digit years as-is. | |||
args.trim_year = args.trim_year or '100-999' | |||
return p._Date( | return p._Date( | ||
{ args.year, args.month, args.day, args.hour, args.minute, args.second, args.tzhour, args.tzmin }, | {args.year, args.month, args.day, args.hour, args.minute, args.second, args.tzhour, args.tzmin}, | ||
args.lang, | args.lang, | ||
args.case, | args.case, | ||
args.class | args.class, | ||
args.trim_year | args.trim_year | ||
) | ) | ||
end | end | ||
return p | return p | ||