«Մոդուլ:Complex date»–ի խմբագրումների տարբերություն

Content deleted Content added
Նոր էջ «--[[ This module is intended for creation of complex date phrases in variety of languages. Once deployed, please do not modify this code without applying the changes first...»:
(Տարբերություն չկա)

21:04, 15 Հոկտեմբերի 2015-ի տարբերակ

Documentation for this module may be created at Մոդուլ:Complex date/doc

--[[  
 
This module is intended for creation of complex date phrases in variety of languages.
 
Once deployed, please do not modify this code without applying the changes first at Module:Complex date/sandbox and testing 
at Module:Complex date/sandbox/testcases.
 
Authors and maintainers:
* 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')
local langSwitch = require('Module:Fallback')._langSwitch
local ISOdate    = require('Module:ISOdate')._ISOdate
local Date       = require('Module:Date')._Date
local formatnum  = require('Module:Formatnum ')._main
local linguistic = require('Module:Linguistic')

-- ==================================================
-- === Internal functions ===========================
-- ==================================================

function formatnum1(numStr, lang)
-- mostly require('Module:Formatnum ')._main function used to translate a number to use different numeral characters, 
-- except that it it does not call  that function unless the language is on the list "LList"
	local LList = {bn=1,bpy=1,kn=1,hi=1,mr=1,new=1,pa=1,gu=1,fa=1,glk=1,mzn=1,ur=1,ar=1,ckb=1,ks=1,lo=1,['or']=1,bo=1,['ml-old']=1,mn=1,te=1,th=1}
	if LList[lang] then -- call only when the language is on the list
		numStr = formatnum(numStr, lang, 1)
	end
	return numStr
end

function getISODate(datestr, datetype, lang, num, case)
-- translate dates in the format YYYY, YYYY-MM, and YYYY-MM-DD
	if  not case and i18n.Translations[datetype] then
		-- look up the grammatical case needed and call ISOdate module
		local rec = langSwitch(i18n.Translations[datetype], lang)
		if type(rec)=='table' then
			case = rec.case[num]
		end
	end
	return ISOdate(datestr, lang, case, '', 1)
end

function translatePhrase(date1, date2, operation, lang, state)
-- use tables in Module:i18n/complex date to translate a phrase
	if not i18n.Translations[operation] then
		p.Error = string.format('<span style="background-color:red;">Error in [[Module:Complex date]]: input parameter "%s" is not recognized.</span>', operation or 'nil')
		return ''
	end
	local dateStr = langSwitch(i18n.Translations[operation], lang)
	if type(dateStr)=='table' then
		dateStr = dateStr[1]
	end
	if type(dateStr)=='function' then
		local success
		local nDates = i18n.Translations[operation]['nDates']
		if nDates==2 then -- 2 date phrase
			success, dateStr = pcall(dateStr, date1, date2, state)
		else  -- 1 date phrase
			success, dateStr = pcall(dateStr, date1, state)
		end
	end
	-- replace parts of the string '$date1' and '$date2' with date1 and date2 strings
	dateStr = mw.ustring.gsub(dateStr, '$date1', date1)
	dateStr = mw.ustring.gsub(dateStr, '$date2', date2)
	return dateStr
end

function oneDatePhrase(dateStr, adj, era, units, lang, num, case, state)
-- translate a single date phrase
	-- dateStr can have many forms: ISO date, year or a number for 
	-- decade, century or millennium
	if units == '' then -- unit is "year", "month", "day"
		dateStr = getISODate(dateStr, adj, lang, num, case)
	else -- units is "decade", "century", "millennium''
		dateStr = translatePhrase(dateStr, '', units, lang, state)
	end
	
	-- add adjective ("early", "mid", etc.) or preposition ("before", "after", 
	-- "circa", etc.) to the date
	if adj ~= '' then
		dateStr = translatePhrase(dateStr, '', adj, lang, state)
	else -- only era?
		dateStr = formatnum1(dateStr, lang)
	end
	
	-- add era
	if era ~= '' then
		dateStr = translatePhrase(dateStr, '', era, lang, state)
	end
	return dateStr
end

function twoDatePhrase(date1, date2, state, lang)
-- translate a double date phrase
	local dateStr, case
	local era=''
	if state.era1 == state.era2 then
		-- if both eras are the same than add it only once
		era = state.era1
		state.era1 = ''
		state.era2 = ''
	end
	case = {nil, nil}
	if i18n.Translations[state.conj] then
		local rec = langSwitch(i18n.Translations[state.conj], lang)
		if type(rec)=='table' then
			case = rec.case
		end
	end
	date1   = oneDatePhrase(date1, state.adj1, state.era1, state.units1, lang, 1, case[1], state)
	date2   = oneDatePhrase(date2, state.adj2, state.era2, state.units2, lang, 2, case[2], state)
	dateStr = translatePhrase(date1, date2, state.conj, lang, state)
	if era ~= '' then
		dateStr = translatePhrase(dateStr, '', era, lang, state)
	end
	return dateStr
end

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
		date1 = getISODate(date1, operation, lang, 1, nil)
		date2 = getISODate(date2, operation, lang, 2, nil)
		if era == '' then era = 'ad' end
		dateStr = translatePhrase(date1, '', era, lang, state) .. ' (' .. translatePhrase(date2, '', 'ah', lang, state) .. ')'
		era = ''
	elseif operation == 'julian' then
		date1 = getISODate(date1, operation, lang, 1, nil)
		date2 = getISODate(date2, operation, lang, 2, nil)
		dateStr = translatePhrase(date1, date2, operation, lang, state)
	elseif operation == 'turn of the year' or operation == 'turn of the decade' or operation == 'turn of the century' then 
		if operation == 'turn of the decade' then dt=10 else dt=1 end
		if not date2 or date2=='' then date2=tostring(tonumber(date1)-dt) end
		if era~='bp' and era~='bc' then date1, date2 = date2, date1 end
		if operation == 'turn of the year' then
			date1 = ISOdate(date1, lang, '', '', 1)
			date2 = ISOdate(date2, lang, '', '', 1)
		else
			date1 = formatnum1(date1, lang)
			date2 = formatnum1(date2, lang)
		end
		dateStr = translatePhrase(date1, date2, operation, lang, state)
	elseif operation == 'year unknown' then
		dateStr = translatePhrase('', '', operation, lang, state)
	elseif operation == 'unknown' then
		dateStr = tostring(mw.message.new( "exif-unknowndate" ):inLanguage( lang ))
	end
	
	-- add era
	if era ~= '' then
		dateStr = translatePhrase(dateStr, '', era, lang, state)
	end
	return dateStr
end

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
	a = i18n.Synonyms[str1] -- look up synonyms of "str1"
	if a then
		out = a[1]
	else
		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 ...
	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

function datePrecision(dateStr, units)	
-- "in this module "Units" is a string like millennium, century, or decade
--	"precision" is wikibase compatible date precision number: 6=millennium, 7=century, 8=decade, 9=year, 10=month, 11=day
-- based on string or numeric input calculate "Units" and "precision"

	if type(units)=='number' then
		precision = units
		if precision>11 then precision=11 end -- clip the range of precision values
		if     precision==6 then units='millenniom' 		
		elseif precision==7 then units='century'
		elseif precision==8 then units='decade'
		else units = ''
		end
	elseif type(units)=='string' then
		units = string.lower(units);
		if     units=='millenniom' then precision=6
		elseif units=='century'    then precision=7
		elseif units=='decade'     then precision=8
		else precision=9
		end
	end
	if units=='' or precision==9 then
		local sLen = mw.ustring.len(dateStr)
		if     sLen<= 4 then precision=9
		elseif sLen== 7 then precision=10
		elseif sLen>=10 then precision=11
		end
		units=''
	end
	return units, precision
end

-- ==================================================
-- === External functions ===========================
-- ==================================================

function p.Era(frame)
    -- process inputs
	local dateStr
	local args    = frame.args
	local dateStr = args['date'] or ''
	local eraType = string.lower(args['era']  or '')
	local lang    = args['lang'] or ''
	if lang == '' then
		lang = mw.message.new( "lang" ):plain()
	end

	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(frame)
    -- process inputs
	local dateStr, Error
	local args   = frame.args
	local date1  = args['date1'] or args['2'] or args['date'] or ''
	local date2  = args['date2'] or args['3'] or ''
	local conj   = args['conj']  or args['1'] or ''
	local adj1   = args['adj1']  or args['adj'] or ''
	local adj2   = args['adj2'] or ''
	local units1 = args['precision1'] or args['precision'] or ''
	local units2 = args['precision2'] or args['precision'] or ''
	local era1   = args['era1'] or args['era'] or ''
	local era2   = args['era2'] or args['era'] or ''
	local lang   = args['lang'] or ''
	if lang == '' then
		lang = mw.message.new( "lang" ):plain()
	end
	dateStr = p._complex_date(conj, adj1, date1, units1, era1, adj2, date2, units2, era2, lang)
	if p.Error~=nil then
		dateStr = p.Error .. '[[Category:Pages using Complex date template with incorrect parameter]]'
	end
	return dateStr
end
	
function p._complex_date(conj, adj1, date1, units1, era1, adj2, date2, units2, era2, lang)
	local Output=''

    -- process inputs
	local state  = {} 
	state.conj = string.lower(conj or '')
	state.adj1 = string.lower(adj1 or '')
	state.adj2 = string.lower(adj2 or '')
	state.era1 = string.lower(era1 or '')
	state.era2 = string.lower(era2 or '')
	
	-- calculate date precision value
	state.units1, state.precision1 = datePrecision(date1, units1 or '')
	state.units2, state.precision2 = datePrecision(date2, units2 or '')
	  
	-- if date 1 is missing but date 2 is provided than swap them
	if date1 == '' and date2 ~= '' then
		date1 = date2
		date2 = ''
		state = {adj1 = state.adj2, era1 = state.era2, units1 = state.units2, precision1=state.precision2,
		         adj2 = '',         era2 = '',         units2 = '',           precision2='', conj=state.conj}
	end
	if     date2 ~= '' then state.nDates = 2 
	elseif date1 ~= '' then state.nDates = 1 
	else	                state.nDates = 0
	end
	
	-- reconcile alternative names for text inputs
	local conj         = checkAliases(state.conj ,''  ,'j')
	state.adj1 ,conj   = checkAliases(state.adj1 ,conj,'a')
	state.units1,conj  = checkAliases(state.units1,conj,'p')
	state.era1 ,conj   = checkAliases(state.era1 ,conj,'e')
	state.special,conj = checkAliases('',conj,'c')
	state.adj2         = checkAliases(state.adj2 ,'','a')
	state.units2       = checkAliases(state.units2,'','p')
	state.era2         = checkAliases(state.era2 ,'','e')
	state.conj         = conj
	state.lang         = lang
	if p.Error~=nil then
		return nil
	end
	
	-- handle special cases
	if state.adj1=='circa' and state.nDates == 2 then
		state.conj = 'circa2'
		state.adj1 = ''
		state.adj2 = ''
	end
	if state.nDates == 2 and state.adj1=='late' and state.adj2=='early' and state.conj=='and' and state.units1==state.units2 and state.era1==state.era2 then
		if state.units1=='century' then
			state.conj='turn of the century'
		elseif state.units1=='decade' then
			state.conj='turn of the decade'
		elseif state.units1=='' then
			state.conj='turn of the year'
		end
		state.adj1 = ''
		state.adj2 = ''
		state.units1 = ''
		state.units2 = ''
	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
	if state.special~='' then
		Output = otherPhrases(date1, date2, state.special, state.era1, lang, state)	
	elseif state.conj~='' then
		Output = twoDatePhrase(date1, date2, state, lang)
	elseif state.adj1~='' or state.era1~='' or state.units1~='' then
		Output = oneDatePhrase(date1, state.adj1, state.era1, state.units1, lang, 1, nil, state)
	elseif date1~='' then
		Output = ISOdate(date1, lang, '', 'dtstart', '100-999')
	end
	if p.Error~=nil then
		return errorStr
	end

	-- if there is any wikicode in the string than execute it
	if mw.ustring.find(Output, '{') then
		Output = mw.getCurrentFrame():preprocess(Output)
	end

	return Output
end

return p