local canonical_AD = 'AD'
local canonical_BCE = 'BCE'
local AD_limit = 999 -- maximum year to append CE
local export = {}
local function validate_year(yearstring)
local year = tonumber(yearstring)
if (year == nil) then
return false
end
if (year <= 0) then
return false
end
return year
end
local function validate_tp(tpstring)
-- validates ad, bc, bce, ...
local tpstring = string.upper(tpstring)
local tpstring = string.gsub(tpstring, "%.", "")
local bcetable = { = true, = true}
local adtable = { = true, = true}
if bcetable then
return canonical_BCE
end
if adtable then
return canonical_AD
end
return false
end
local function validate_approx(approxstring)
approxstring = string.lower(approxstring)
approxstring = string.gsub(approxstring, "%.", "")
local approx_table = { = true,
= true,
= true,
= true}
if approx_table then
return "a"
end
return false
end
local function parse_date(datestring)
local result = {}
local separators = {'-', '–'}
local canonical_separator = '-'
for i, separator in pairs(separators) do -- replace any nonstandard separators
datestring = string.gsub(datestring, separator, canonical_separator)
end
local datetable = mw.text.split(datestring, canonical_separator)
for i, yearstring in ipairs(datetable) do
local yeartable_temp = mw.text.split(yearstring, ' ')
local yeartable = {}
for i, v in ipairs(yeartable_temp) do -- remove any empty strings
if (v ~= '') then
table.insert(yeartable, v)
end
end
if (#yeartable == 1) then
-- only year is specified
table.insert(result, {'e', validate_year(yeartable), "?"})
end
if (#yeartable == 2) then
-- either year, BCE/AD or c, year
local yt1 = validate_year(yeartable)
local yt2 = validate_year(yeartable)
if (yt1 ~= false) then -- year, BCE/AD
table.insert(result, {'e', yt1, validate_tp(yeartable)})
end
if (yt1 == false) then -- c, year
table.insert(result, {validate_approx(yeartable), yt2, "?"})
end
end
if (#yeartable == 3) then
-- c, year, BCE/AD
table.insert(result, {validate_approx(yeartable), validate_year(yeartable), validate_tp(yeartable)})
end
end
for i, v in pairs(result) do
for i_, v_ in pairs(v) do
if (v_ == false) then
return false
end
end
end
if (#result == 1) then
if result == "?" then
result = canonical_AD
end
return result
end
if (#result == 2) then
-- add in missing time periods
if result == "?" then
if result == "?" then
result = canonical_AD
result = canonical_AD
end
if result == canonical_AD then
result = canonical_AD
end
if result == canonical_BCE then
result = canonical_BCE
end
end
if result == canonical_AD then
if result == "?" then
result = canonical_AD
end
if result == canonical_BCE then
return false
end
end
if result == canonical_BCE then
if result == "?" then
result = canonical_BCE
end
end
-- validate time ranges
if result == result then
if result == canonical_BCE then
if result > result then
return false
end
else
if result < result then
return false
end
end
else
if result == canonical_AD and result == canonical_BCE then
return false
end
end
return result
end
return false
end
local function display_date(parsed_date)
local separator = ' – '
local CE = "<small class='ce-date'>]</small>"
local BCE = "<small class='ce-date'>]</small>"
local circa = "'']''"
local result = {}
for i, v in ipairs(parsed_date) do
local s = ""
if v == 'a' then
s = s..circa.." "
end
s = s..tostring(v).." "
if v == canonical_BCE then
s = s..BCE
end
if v == canonical_AD then
if (v <= AD_limit) then
s = s..CE
end
end
table.insert(result, mw.text.trim(s))
end
return table.concat(result, separator)
end
function export.main(datestring)
local parsed_date = parse_date(datestring)
if (parsed_date ~= false) then
return display_date(parsed_date)
end
return datestring
end
return export