Մոդուլ:Date table sorting

Վիքիպեդիայից՝ ազատ հանրագիտարանից

Documentation for this module may be created at Մոդուլ:Date table sorting/doc

local Date = require('Module:Date')
local yesno = require('Module:Yesno')
local getArgs = require('Module:Arguments').getArgs

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( tonumber(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(args)
	local dateStr = Date.simpleDate(args)
	
	local pageLangCode = args['lang'] or 'en'
	local pageLang = mw.language.new( pageLangCode )
	
	local dateFormat = readFormat(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

function p.main(moduleFrame)
	local args = getArgs(moduleFrame)
	return p._main(args)
end

return p