<templatestyles src="Modul:Dokumentacija/styles.css"></templatestyles>
local p = {}
local mjeseciGen = {
'siječnja', 'veljače', 'ožujka', 'travnja', 'svibnja', 'lipnja',
'srpnja', 'kolovoza', 'rujna', 'listopada', 'studenoga', 'prosinca'
}
local mjeseciNom = {
'siječanj', 'veljača', 'ožujak', 'travanj', 'svibanj', 'lipanj',
'srpanj', 'kolovoz', 'rujan', 'listopad', 'studeni', 'prosinac'
}
local function linkify(text)
return '[[' .. text .. ']]'
end
local function isValidDate(d, m, y)
if not d or not m or not y then return true end
if d > 31 or m > 12 or y > 9999 then return false end
local test = os.time{year=y, month=m, day=d}
local actual = os.date('*t', test)
return actual.day == d and actual.month == m and actual.year == y
end
local function formatDate(day, month, year, link)
if not year then
return '<span class="error">Grješka: Nije unesena godina</span>[[Kategorija:Stranice koje koriste predložak Objavljeno s nevažećim nadnevkom]]'
end
year = tostring(year):gsub('%.', '')
if tonumber(year) and #year > 4 then
return '<span class="error">Greška: Nije unesen važeći nadnevak</span>[[Kategorija:Stranice koje koriste predložak Objavljeno s nevažećim nadnevkom]]'
end
local d = tonumber(day)
local m = tonumber(month)
local y = tonumber(year)
if (d and d > 31) or (m and m > 12) or not isValidDate(d, m, y) then
return '<span class="error">Grješka: Nije unesen važeći nadnevak</span>[[Kategorija:Stranice koje koriste predložak Datum s nevažećim datumom]]'
end
local yText = link == 'da' and linkify(y .. '.') or y .. '.'
if d and m then
local mText = mjeseciGen[m]
local dm = d .. '. ' .. mText
return (link == 'da' and linkify(dm) or dm) .. ' ' .. yText
elseif m then
local mText = mjeseciNom[m]
return (link == 'da' and linkify(mText) or mText) .. ' ' .. yText
else
return yText
end
end
local function microformat(day, month, year, isFinale)
if not year then return '' end
local d = tonumber(day)
local m = tonumber(month)
local y = tonumber(year)
local iso
if d and m then
iso = string.format('%04d-%02d-%02d', y, m, d)
elseif m then
iso = string.format('%04d-%02d', y, m)
else
iso = string.format('%04d', y)
end
local class = isFinale and 'dtend' or 'dtstart'
return string.format(
'<span class="bday %s published updated itvstart" style="display:none;" datetime="%s"><span>%s</span></span>',
class, iso, iso
)
end
local function callTimeAgo(day, month, year, frame)
if not year then return '' end
local d = tonumber(day) or 1
local m = tonumber(month) or 1
local y = tonumber(year)
local dateString = string.format('%04d-%02d-%02d', y, m, d)
return frame:preprocess('{{time ago|' .. dateString .. '|min_magnitude=months}}')
end
function p._render(frame, isFinale)
local args = frame:getParent().args
local day, month, year
local link, dodaj
local nums = {}
for i, v in ipairs(args) do
if v and v:match('^%d+$') then
table.insert(nums, v)
end
end
for k, v in pairs(args) do
local kLower = mw.ustring.lower(tostring(k or ''))
local vLower = mw.ustring.lower(tostring(v or ''))
if kLower == 'link' or kLower == 'links' or vLower == 'link' or vLower == 'links' or vLower == 'l' or vLower == 'l=d' or vLower == 'link=da' then
link = 'da'
end
if kLower == 'prije' or vLower == 'prije' or vLower == 'p' or vLower == 'dodaj=prije' or vLower == 'prije=da' then
dodaj = 'prije'
end
end
-- Detect year-first flipped format
if tonumber(args[1]) and args[1]:match('^%d%d%d%d$') then
year = args[1]
month = args[2]
day = args[3]
else
-- Find year anywhere
for _, v in ipairs(args) do
if v and v:match('^%d%d%d%d$') then
year = v
break
end
end
-- Assign day/month from remaining numeric values
local filtered = {}
for _, v in ipairs(args) do
if v and v:match('^%d+$') and v ~= year then
table.insert(filtered, v)
end
end
if #filtered == 1 and year then
day = nil
month = filtered[1]
else
day = filtered[1]
month = filtered[2]
end
end
if day then day = tostring(tonumber(day)) end
if month then month = tostring(tonumber(month)) end
local dateText = formatDate(day, month, year, link)
if dateText:match('Greška:') then
return dateText
end
local micro = microformat(day, month, year, isFinale)
local ago = dodaj == 'prije' and callTimeAgo(day, month, year, frame) or ''
return micro .. dateText .. (ago ~= '' and ' (' .. ago .. ')' or '')
end
function p.main(frame)
return p._render(frame, false)
end
function p.finale(frame)
return p._render(frame, true)
end
return p