Module:Date table sorting/sandbox

From Meta, a Wikimedia project coordination wiki
Jump to navigation Jump to search
Module documentation

Usage[edit]

Generates a <time data-sort-value=[standardized-date] datetime=[standardized-date]>[formatted date]</time> tag, where [standardized-date] is in ISO 8601 format (e.g. 2020-07-25), and [formatted date] is in a customizable format.

  • From a content page, you should use {{Date table sorting}}.
  • From a template, you should invoke main function {{#invoke|main}}
  • From a module, you should call _main function require('Module:Date table sorting')._main

Arguments can be passed through either template call or Module invokation.

Arguments[edit]

lang
Code of the language in which you want to display the date. In most cases, it should be {{PAGELANGUAGE}} (from a Lua module, you can get it with frame:preprocess( '{{PAGELANGUAGE}}' )).
format
Deprecated (kept for backware compatibility). Use phpformat instead.
phpformat
Date display format: any string supported by {{#time}} (you can read its documentation page). Defaults to "j xg Y" (e.g. “6 January 2020”).
precision
Instead of setting a phpformat, the module can guess it from date precision integer: 9 will display only year, 10 will display month and year, and 11 will display day, month and year. These values come from Wikidata date data model.
date
full date (takes precedence over other date arguments)
year
year (takes precedence over numbered date arguments)
month
month (works only if year is set)
day
day (works only if both year and month are set)
1
full date or year
2
month (works only if 1 is set)
3
day (works only if both 1 and 2 are set)
local Date = require('Module:Date')
local yesno = require('Module:Yesno')

local p = {}

--[[ Returns a date format convenient for {{#time}}, reading args table.
	
	"format" arg is used for backward compatibility, "phpformat" should be used
instead.
	About phpformat, @see mw:Help:Extension:ParserFunctions##time

	@param Str[] args Table with "phpformat", "format" or "precision" key set.
]]
local function readFormat(args)
	local legacyFormatStrings
	if( args['abbr'] and ( args['abbr'] == 'on' or yesno(args['abbr']) ) ) then
		leacyFormatStrings = {
			dmy = "j M Y",
			mdy = "M j, Y",
			dm = "j M",
			md = "M j",
			my = "M Y",
			y = "Y",
			m = "M",
			d = "j",
			hide = ''
		}
	else
		legacyFormatStrings = {
			dmy = "j F Y",
			mdy = "F j, Y",
			dm = "j F",
			md = "F j",
			my = "F Y",
			y = "Y",
			m = "F",
			d = "j",
			hide = ''
		}
	end
	
	if args['format'] and legacyFormatStrings[args['format']] then
		return legacyFormatStrings[args['format']]
	end
	
	if args['phpformat'] then
		return args['phpformat']
	end
	
	if args['precision'] then
		return Date.precisionToFormat( args['precision'] )
	end
	
	if args['format'] then --misuse of “format” argument instead of “phpformat”
		return args['format']
	end
	
	return "j xg Y" --defaults to “6 January 2012” format
end

--[[
	Needed args:
	- str date | 1 (in a format supported by {{#time}})
	or
	- int year | 1
	- int month | 2
	- int day | 3
	Optional args:
	- str lang: Defaults to 'en'
	- str phpformat: format string for {{#time}}
	- int precision: if phpformat is not given, get a format according to
mw:Wikibase/DataModel#TimeValue precision
]]
function p.main(moduleFrame)
	local templateFrame = moduleFrame:getParent()
	local dateStr = Date.simpleDate(templateFrame.args)
	
	local pageLangCode = templateFrame.args['lang'] or 'en'
	local pageLang = mw.language.new( pageLangCode )
	
	local dateFormat = readFormat(templateFrame.args)
	
	local formattedDate = pageLang:formatDate( dateFormat, dateStr, true )
	
	local html = mw.html.create()
		:tag('time')
		:attr('data-sort-value', dateStr)
		:attr('datetime', dateStr)
		:wikitext( formattedDate )

	return html
end

return p