Content deleted Content added
clean whitespace |
update from sandbox (commons version) |
||
(One intermediate revision by one other user not shown) | |||
Line 1:
--[[
__ __ _ _ ____ _
| \/ | ___ __| |_ _| | ___ _ / ___|___ _ __ ___ _ __ | | _____ __ __| | __ _| |_ ___
| |\/| |/ _ \ / _` | | | | |/ _ (_) | / _ \| '_ ` _ \| '_ \| |/ _ \ \/ / / _` |/ _` | __/ _ \
| | | | (_) | (_| | |_| | | __/_| |__| (_) | | | | | | |_) | | __/> < | (_| | (_| | || __/
Line 15:
* User:Sn1per - first draft of the original version
* User:Jarekt - corrections and expansion of the original version
]]
-- List of external modules and functions
local p = {Error = nil}
local i18n = require('Module:i18n/complex date') -- used for translations of date related phrases
local ISOdate = require('Module:ISOdate')._ISOdate -- used for parsing dates in YYYY-MM-DD and related formats
local Calendar -- loaded lazily
-- ==================================================
Line 39 ⟶ 38:
end
-- ==================================================
local function formatnum1(numStr, lang)
-- mostly require('Module:Formatnum').formatNum function used to translate a number to use different numeral characters,
Line 49:
end
-- ==================================================
local function getISODate(datestr, datetype, lang, num, case)
-- translate dates in the format YYYY, YYYY-MM, and YYYY-MM-DD
Line 61 ⟶ 62:
end
-- =======================================================================
local function translatePhrase(date1, date2, operation, lang, state)
-- use tables in Module:i18n/complex date to translate a phrase
Line 72 ⟶ 74:
end
if type(dateStr)=='function' then
local
local nDates = i18n.Translations[operation]['nDates']
if nDates==2 then -- 2 date phrase
else -- 1 date phrase
end
end
if type(dateStr)=='string' then
-- replace parts of the string '$date1' and '$date2' with date1 and date2 strings
Line 94 ⟶ 96:
end
-- =======================================================================
local function oneDatePhrase(dateStr, adj, era, units, lang, num, case, state)
-- translate a single date phrase
Line 99 ⟶ 102:
state.adj, state.era, state.units, state.precision = state.adj2, state.era2, state.units2, state.precision2
end
-- dateStr can have many forms: ISO date, year or a number for
-- decade, century or millennium
Line 107 ⟶ 110:
dateStr = translatePhrase(dateStr, '', units, lang, state)
end
-- add adjective ("early", "mid", etc.) or preposition ("before", "after",
-- "circa", etc.) to the date
Line 115 ⟶ 118:
dateStr = formatnum1(dateStr, lang)
end
-- add era
if era ~= '' then
Line 123 ⟶ 126:
end
-- =======================================================================
local function twoDatePhrase(date1, date2, state, lang)
-- translate a double date phrase
Line 149 ⟶ 153:
end
-- =======================================================================
local function otherPhrases(date1, date2, operation, era, lang, state)
-- translate specialized phrases
local dateStr = ''
if operation == 'islamic' then
if date2=='' then date2 = mw.getCurrentFrame():callParserFunction('#time', 'xmY', date1) end
Line 163 ⟶ 168:
if not date2 and date1 then -- Convert from Julian to Gregorian calendar date
if Calendar == nil then
Calendar = require("Module:Calendar") -- lazy loding (only if needed)
end
local JDN = Calendar._date2jdn(date1, 0)
Line 189 ⟶ 194:
dateStr = translatePhrase(date1, date2, operation, lang, state)
elseif operation == 'year unknown' then
dateStr = translatePhrase('', '', operation, lang, state) .. '<div style="display: none;">Unknown date</div>'
elseif operation == 'unknown' then
dateStr = tostring(mw.message.new( "exif-unknowndate" ):inLanguage( lang )) .. '<div style="display: none;">Unknown date</div>'
end
-- add era
if era ~= '' then
Line 201 ⟶ 206:
end
-- =======================================================================
local function checkAliases(str1, str2, sType)
-- some inputs have many aliases - reconcile them and ensure string is playing a proper role
local out = ''
if str1 and str1~='' then
out = a[1]
p.Error = string.format('<span style="background-color:red;">Error in [[Module:Complex date]]: %s is not recognized.</span>', str1)
end
elseif str2 and str2~='' then -- if "str1" of type "sType" is empty than maybe ...
local a = i18n.Synonyms[str2] -- ..."str2" is of the same type and is not empty
if a and a[2]==sType then
out = a[1]
str2 = ''
end
end
return out, str2
end
-- =======================================================================
local function datePrecision(dateStr, units)
-- "in this module "Units" is a string like millennium, century, or decade
--
-- based on string or numeric input calculate "Units" and "precision"
local precision
if type(units)=='number' then
Line 236 ⟶ 242:
end
elseif type(units)=='string' then
units = string.lower(units)
if units=='millennium' then precision=6
elseif units=='century' then precision=7
Line 260 ⟶ 266:
end
-- =======================================================================
local function isodate2timestamp(dateStr, precision, era)
-- convert date string to timestamps used by Quick Statements
Line 291 ⟶ 297:
tStamp = era .. d .. '500-00-00T00:00:00Z/6'
end
return tStamp
end
-- =======================================================================
local function oneDateQScode(dateStr, adj, era, precision)
-- create QuickStatements string for "one date" dates
Line 307 ⟶ 314:
spring='Q40720559' , summer='Q40720564' , autumn='Q40720568' , winter='Q40720553',
firsthalf='Q40719687', secondhalf='Q40719707' }
local qLUT = {['from']='P580', ['until']='P582', ['after']='P1319', ['before']='P1326', ['by']='P1326'}
local refine = rLUT[adj]
Line 325 ⟶ 332:
end
-- =======================================================================
local function twoDateQScode(date1, date2, state)
-- create QuickStatements string for "two date" dates
if state.adj1~='' or state.adj2~='' or state.era1~=state.era2 then
return '' -- QuickStatements string are not generated for two date phrases with adjectives
end
local outputStr = ''
Line 338 ⟶ 346:
-- find date with lower precision in common to both dates
local cd
local year1 = tonumber(string.sub(d1,2,5))
local year2 = tonumber(string.sub(d2,2,5))
local k = 0
for i = 1,10,1 do
Line 348 ⟶ 357:
cd = isodate2timestamp(string.sub(d1,2,8), 10, state.era1)
elseif k>=6 and k<9 then -- same year, since "+YYYY-" is in common
cd = isodate2timestamp(tostring(year1), 9, state.era1)
elseif k==4 then -- same decade(k=4, precision=8), since "+YYY" is in common
cd = isodate2timestamp(tostring(year1), 8, state.era1)
elseif k==3 then -- same century(k=3, precision=7) since "+YY" is in common
cd = isodate2timestamp( d, 7, state.era1)
elseif k==2 then -- same millennium (k=2, precision=6), since "+Y" is in common
local d = tostring(math.floor
cd = isodate2timestamp( d, 6, state.era1)
end
if not cd then
return ''
end
Line 365 ⟶ 375:
--
if (state.conj=='from-until') or (state.conj=='and' and year1==year2-1) then
outputStr = cd ..",P580,".. d1 ..",P582,".. d2
elseif (state.conj=='between') or (state.conj=='or' and year1==year2-1) then
outputStr = cd ..",P1319,".. d1 ..",P1326,".. d2
elseif state.conj=='circa2' then
Line 376 ⟶ 386:
end
-- =======================================================================
local function processInputParams(conj, adj1, date1, units1, era1, adj2, date2, units2, era2, lang, passNr)
-- process inputs and save date in state array
Line 416 ⟶ 403:
date1 = date2
date2 = ''
state = {adj1 = state.adj2, era1 = state.era2, units1 = state.units2,
end
if date2 ~= '' then state.nDates = 2
elseif date1 ~= '' then state.nDates = 1
else
end
Line 467 ⟶ 454:
state.units2 = ''
end
end
state.adj, state.era, state.units, state.precision = state.adj1, state.era1, state.units1, state.precision1
return date1, date2, state
end
-- ==================================================
-- === External functions ===========================
-- ==================================================
function p.Era(frame)
-- process inputs
local dateStr
local args = frame.args
if not (args.lang and mw.language.isSupportedLanguage(args.lang)) then
args.lang = frame:callParserFunction( "int", "lang" ) -- get user's chosen language
end
local lang = args['lang']
local dateStr = args['date'] or ''
local eraType = string.lower(args['era'] or '')
dateStr = ISOdate(dateStr, lang, '', '', 1)
if eraType then
eraType = checkAliases(eraType ,'','e')
dateStr = translatePhrase(dateStr, '', eraType, lang, {})
end
return dateStr
end
-- =======================================================================
function p._complex_date(conj, adj1, date1, units1, era1, adj2, date2, units2, era2, lang, passNr)
local Output=''
local state
-- process inputs and save date in state array
date1, date2, state = processInputParams(conj, adj1, date1, units1, era1, adj2, date2, units2, era2, lang, passNr)
if p.Error~=nil then
return nil
end
local errorStr = string.format(
'\n*conj=%s, adj1=%s, era1=%s, unit1=%s, prec1=%i, adj2=%s, era2=%s, unit2=%s, prec2=%i, special=%s',
state.conj, state.adj1, state.era1, state.units1, state.precision1,
state.adj2, state.era2, state.units2, state.precision2, state.special)
-- call specialized functions
Line 503 ⟶ 527:
end
-- =======================================================================
function p._complex_date_cer(conj, adj1, date1, units1, era1, adj2, date2, units2, era2, certainty, lang)
-- same as p._complex_date but with extra parameter for certainty: probably, possibly, presumably, etc.
local dateStr = p._complex_date(conj, adj1, date1, units1, era1, adj2, date2, units2, era2, lang, 1)
certainty = checkAliases(certainty, conj, 'r')
local LUT = {probably='Q56644435', presumably='Q18122778', possibly='Q30230067', circa='Q5727902' }
if certainty and LUT[certainty] then
local state = {}
date1, date2, state = processInputParams(conj, adj1, date1, units1, era1, adj2, date2, units2, era2, lang, 1)
dateStr = translatePhrase(dateStr, '', certainty, lang, state)
dateStr = string.gsub(dateStr, '(%<div style="display: none;"%>date QS:P,[^%<]+)(%</div%>)', '%1,P1480,' .. LUT[certainty] .. '%2' )
end
return dateStr
end
-- =======================================================================
function p.complex_date(frame)
-- process inputs
local dateStr
local args = frame.args
if not (args.lang and mw.language.isSupportedLanguage(args.lang)) then
Line 519 ⟶ 559:
local era1 = args['era1'] or args['era'] or ''
local era2 = args['era2'] or args['era'] or ''
local certainty = args['certainty']
local lang = args['lang']
dateStr = p.
if p.Error~=nil then
dateStr = p.Error .. '[[Category:Pages using Complex date template with incorrect parameter]]'
|