Jump to content

Module:Auto date formatter: Difference between revisions

From Wikipedia, the free encyclopedia
Content deleted Content added
No edit summary
No edit summary
Line 3: Line 3:
local global_df = mw.loadData ('Module:Citation/CS1/Configuration').global_df; -- fetch global date format specified by {{use xxx dates}} template
local global_df = mw.loadData ('Module:Citation/CS1/Configuration').global_df; -- fetch global date format specified by {{use xxx dates}} template


local format_date = mw.language.getContentLanguage().formatDate; -- function to reformat dates
local lang_obj = mw.language.getContentLanguage(); -- function to reformat dates


--[[--------------------------< F O R M A T T E R >------------------------------------------------------------
--[[--------------------------< F O R M A T T E R >------------------------------------------------------------
Line 24: Line 24:
local function formatter (date, format, style)
local function formatter (date, format, style)
local function do_format (format_str, date) -- local function to do the actual formatting
local function do_format (format_str, date) -- local function to do the actual formatting
local good, new_date = pcall (format_date, format_str, date); -- attempt to reformat; on success, <good> is boolean true
local good, new_date = pcall (lang_obj.formatDate, lang_obj, format_str, date); -- attempt to reformat; on success, <good> is boolean true
return (good and new_date) or date; -- when <good> is boolean true, return <new_date>; return <date> else
return (good and new_date) or date; -- when <good> is boolean true, return <new_date>; return <date> else
end
end

Revision as of 00:32, 17 March 2025

require ('strict');

local global_df = mw.loadData ('Module:Citation/CS1/Configuration').global_df;	-- fetch global date format specified by {{use xxx dates}} template

local lang_obj = mw.language.getContentLanguage();								-- function to reformat dates

--[[--------------------------< F O R M A T T E R >------------------------------------------------------------

local function to format <date> according to <format> (dmy or mdy) and <style> (long, short, year initial) as
specified by the |cs1-dates= parameter of the {{use xxx dates}} template.

valid |cs1-dates= format character strings are:
	l, ll, all	– default; long-form publication and access- / archive-dates; 'all' when |cs1-dates= omitted or empty
	ls			– long-form publication dates; abbreviated access- / archive-dates
	ly			– long-form publication dates; year-initial numeric access- / archive-dates (ymd)
	s, ss		– abbreviated publication and access- / archive-dates
	sy			– abbreviated publication dates; year-initial numeric access- / archive-dates (ymd)
	y, yy		– year-initial numeric publication, access- and archive-dates (ymd); overrides base format

note: 'all' is added by get_date_format() in Module:Citation/CS1/Configuration when |cs1-dates= is omitted or empty

]]

local function formatter (date, format, style)
	local function do_format (format_str, date)									-- local function to do the actual formatting
		local good, new_date = pcall (lang_obj.formatDate, lang_obj, format_str, date);			-- attempt to reformat; on success, <good> is boolean true
		return (good and new_date) or date;										-- when <good> is boolean true, return <new_date>; return <date> else
	end

	if 'l' == style then														-- long month-name format
		if 'dmy' == format then
			return do_format ('j F Y', date);
		else																	-- must be mdy
			return do_format ('F j, Y', date);
		end

	elseif 's' == style then													-- abbreviated month-name format
		if 'dmy' == format then
			return do_format ('j M Y', date);
		else																	-- must be mdy
			return do_format ('M j, Y', date);
		end

	else																		--<style> must be 'y', year-initial numeric format; overrides dmy/mdy in <format>
		return do_format ('Y-m-d', date);
	end
end


--[[--------------------------< P U B _ D A T E _ F O R M A T >------------------------------------------------

For publication dates |date=, |publication-date=, etc

	{{#invoke:Sandbox/Trappist the monk/df|pub_date_format|16 March 2025}}

]]

local function pub_date_format (frame)
	if not global_df then
		return date;															-- when article does not have {{use xxx dates}}, abandon
	end

	local date = frame.args[1];													-- <args[1]> is date to be formatted
	
	local base_format = global_df:match ('^(%a%a%a)');							-- get {{use xxx dates}} base date format: 'dmy' or 'mdy'
	local date_style = global_df:match ('^%a%a%a%-(.+)');						-- get |cs1-dates= style modifiers

	if ({['all'] = true, ['ll'] = true, ['l'] = true, ['ls'] = true, ['ly'] = true})[date_style] then	-- default long-form style
		return formatter (date, base_format, 'l');
	elseif ({['s'] = true, ['ss'] = true, ['sy'] = true})[date_style] then		-- abbrviated style
		return formatter (date, base_format, 's');
	else																		-- must be year-initial style; overrides format in <base_format>
		return formatter (date, base_format, 'y');
	end
end


--[[--------------------------< A C C E S S _ A R C H I V E _ F O R M A T >------------------------------------

For access and archive dates |access-date=, |accessdate=, archive-date=, archivedate=

	{{#invoke:Sandbox/Trappist the monk/df|access_archive_format|16 March 2025}}

]]

local function access_archive_format (frame)
	if not global_df then
		return date;															-- when article does not have {{use xxx dates}}, abandon
	end

	local date = frame.args[1];													-- <args[1]> is date to be formatted

	local base_format = global_df:match ('^(%a%a%a)');							-- get {{use xxx dates}} base date format: 'dmy' or 'mdy'
	local date_style = global_df:match ('^%a%a%a%-(.+)');						-- get |cs1-dates= style modifiers

	if ({['all'] = true, ['ll'] = true, ['l'] = true})[date_style] then			-- default long-form style
		return formatter (date, base_format, 'l');
	elseif ({['ls'] = true, ['s'] = true, ['ss'] = true})[date_style] then		-- abbrviated style
		return formatter (date, base_format, 's');
	else																		-- must be year-initial style; overrides format in <base_format>
		return formatter (date, base_format, 'y');
	end
end


--[[--------------------------< E X P O R T S >----------------------------------------------------------------
]]

return {
	pub_date_format = pub_date_format,
	access_archive_format = access_archive_format,
	}