Modul:Datum

Z Wikizpráv

Dokumentaci tohoto modulu lze vytvořit na stránce Nápověda:Modul:Datum

-- @brief
--  Backend for {{Datum}}.
-- 
-- @details
--  Handles the date printing and appropriate categorization.
-- 
-- @author
--  [[meta:User:Danny B.]]
local _module = {}
----------------------------------------


local DateTime = require( "Module:DateTime" )
local Error = require( "Module:Error" )


-- @brief
--  Write the date and date categories.
-- 
-- @param
--  frame The current frame object
-- 
-- @return
--  Preprocessed wikitext
function _module.print( frame )
	
	local output = ""
	local templateArgs = frame:getParent().args
	local titleObject = mw.title.getCurrentTitle()
	local errorData = { template = "Datum" }
	local errorFunction = titleObject.namespace == 0 and "getText" or "getMessage"
	
	local published = templateArgs[1]
	local updated = templateArgs["aktualizace"]
	local publishedYear, publishedMonth, publishedDay
	local updatedYear, updatedMonth, updatedDay
	
	
	if not published then
		errorData.missingValue = { paramName = 1, paramDesc = "datová značka ve tvaru rrrr-mm-dd" }
		output = output .. Error[errorFunction]( errorData )
	else
		published = mw.text.trim( published )
		publishedYear, publishedMonth, publishedDay = string.match( published, "(%d%d%d%d)\-(%d%d)\-(%d%d)" )
		if not ( publishedDay and publishedMonth and publishedYear ) then
			errorData.wrongType = { paramName = 1, paramDesc = "datová značka ve tvaru rrrr-mm-dd" }
			output = output .. Error[errorFunction]( errorData )
		end
	end
	
	if updated then
		updated = mw.text.trim( updated )
		updatedYear, updatedMonth, updatedDay = string.match( updated, "(%d%d%d%d)\-(%d%d)\-(%d%d)" )
		if not ( updatedDay and updatedMonth and updatedYear ) then
			errorData.wrongType = { paramName = "aktualizace", paramDesc = "datová značka ve tvaru rrrr-mm-dd" }
			output = output .. Error[errorFunction]( errorData )
		end
	end
	
	if output == "" then
		
		local publishedDateObject = os.date( "*t", os.time( { year = publishedYear, month = publishedMonth, day = publishedDay } ) )
		local publishedTimestamp = string.format( "%04d%02d%02d", publishedDateObject.year, publishedDateObject.month, publishedDateObject.day )
		local publishedDatetime = string.format( "%04d-%02d-%02d", publishedDateObject.year, publishedDateObject.month, publishedDateObject.day )
		local sortkey = string.format( " %08d %s", 100000000 - tonumber( publishedTimestamp ), titleObject.text )
		local publishedVerbalDayOfYear = string.format( "%d. %s", publishedDateObject.day, DateTime.months[publishedDateObject.month].name )
		local publishedVerbalDate = string.format( "%s %d", publishedVerbalDayOfYear, publishedDateObject.year )
		local publishedVerbalDateGenitive = string.format( "%d. %s %d", publishedDateObject.day, DateTime.months[publishedDateObject.month].genitive, publishedDateObject.year )
		local lang = mw.language.new( "cs" )
		
		if titleObject.namespace == 0 then
			output = output .. "__NOTOC__"
			output = output .. "{{DEFAULTSORT:" .. sortkey .. "}}"
			output = output .. "[[Kategorie:⌂| " .. publishedTimestamp .. "]]"
			output = output .. "[[Kategorie:" .. publishedVerbalDate .. "| " .. titleObject.text .. "]]"
			output = output .. "[[Kategorie:" .. publishedVerbalDayOfYear .. "| " .. titleObject.text .. "]]"
		end
		
		titleObject = mw.title.new( publishedVerbalDate )
		-- expensive++
		
		published = ( tonumber( publishedYear ) >= 2008 and titleObject.exists ) and string.format( "[[%s|<time datetime=\"%s\">%s</time>]]", publishedVerbalDate, publishedDatetime, publishedVerbalDateGenitive ) or publishedVerbalDateGenitive
		
		output = output .. string.format( "'''%s %s'''", lang:ucfirst( DateTime.days[publishedDateObject.wday].name ), published )
		
		if updated then
			
			local updatedDateObject = os.date( "*t", os.time( { year = updatedYear, month = updatedMonth, day = updatedDay } ) )
			local updatedVerbalDateGenitive = string.format( "%d. %s %d", updatedDateObject.day, DateTime.months[updatedDateObject.month].genitive, updatedDateObject.year )
			
			output = output .. "<br />"
			output = output .. "<small>(aktualizováno " .. updatedVerbalDateGenitive .. ")</small>[[Kategorie:Aktualizováno " .. updatedVerbalDateGenitive .."]]"
			
		end
		
	end
	
	output = frame:preprocess( output )
	
	return output
	
end


----------------------------------------
return _module