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

-- @brief
--  Backend for {{Překlady}}.
-- 
-- @details
--  Generates the list of translations.
-- 
-- @author
--  [[meta:User:Danny B.]]
local _module = {}
----------------------------------------


local Collation = require( "Module:Collation" )
local Error = require( "Module:Error" )
local Language = require( "Module:Language" )
local Maintenance = require( "Module:Maintenance" )


-- @brief
--  Creates the iterator for table sorted by its keys.
-- 
-- @param
--  tbl Table to be sorted
-- 
-- @return
--  Iterator Table sorted by keys
function kpairs( tbl )
	local keys = {}
	
	for key in pairs( tbl ) do
		table.insert( keys, key )
	end
	table.sort( keys, Collation.sortCompare )
	
	local i = 0
	local iterator = function ()
		i = i + 1
		if keys[i] == nil then
			return nil
		else
			return keys[i], tbl[keys[i]]
		end
	end
	
	return iterator
end


-- @brief
--  Write the list of translations.
-- 
-- @param
--  frame The current frame object
-- 
-- @return
--  Preprocessed wikitext
function _module.print( frame )
	
	local output = ""
	local templateArgs = frame:getParent().args
	local argCount = 0
	
	local errorData = { template = "Překlady" }
	local maintenance = Maintenance( "Překlady" )
	local html
	
	local descriptionArg = "význam"
	local description = mw.text.trim( templateArgs[descriptionArg] or "" )
	local translations = {}
	local errors = {}
	
	local numberOfMeanings = 0
	local numberedTranslations = 0
	local unnumberedTranslations = 0
	local pageText = mw.title.getCurrentTitle():getContent()
	
	html = mw.html.create( "div" )
		:attr( "class", "translations" )
	
	if pageText then
		local czechSection = mw.ustring.match( pageText, "== *čeština *==(.-)\n==[^=]" ) or mw.ustring.match( pageText, "== *čeština *==(.*)" )
		if czechSection then
			for h3 in mw.ustring.gmatch( czechSection, "\n===[^=]-===\n" ) do
				h3 = mw.ustring.gsub( h3, "([%(%)])", "%%%1" )
				local s3 = "\n" .. ( mw.ustring.match( czechSection, h3 .. "(.-)\n===[^=]" ) or mw.ustring.match( czechSection, h3 .. "(.-)$" ) )
				local meanings, translations = mw.ustring.match( s3, "\n==== ?význam ?====\n(.-)\n==== ?překlady ?====\n(.*)" )
				if meanings then
					numberOfMeanings = 0
					numberedTranslations = 0
					unnumberedTranslations = 0
					meanings = "\n" .. ( mw.ustring.match( meanings, "(.-)\n====[^=]" ) or mw.ustring.match( meanings, "(.-)$" ) )
					for _ in mw.ustring.gmatch( meanings, "\n#[^%*]" ) do
						numberOfMeanings = numberOfMeanings + 1
					end
					translations = "\n" .. ( mw.ustring.match( translations, "(.-)\n====[^=]" ) or mw.ustring.match( translations, "(.-)$" ) )
					for _ in mw.ustring.gmatch( translations, "\n# *{{%s*Překlady" ) do
						numberedTranslations = numberedTranslations + 1
					end
					for _ in mw.ustring.gmatch( translations, "\n{{%s*Překlady" ) do
						unnumberedTranslations = unnumberedTranslations + 1
					end
					if unnumberedTranslations > 0 or numberOfMeanings ~= numberedTranslations then
						maintenance:setCategory( "Sjednotit počet překladů s počtem významů", true )
						html:wikitext( maintenance:getCategory() )
					end
				end
			end
		end
	end
	
	for param, value in pairs( templateArgs ) do
		argCount = argCount + 1
	end
	
	if argCount == 0 or (argCount == 1 and templateArgs[descriptionArg] == "") then
		html:wikitext( "—[[Kategorie:Monitoring:Překlady/]]" )
	else
		
		if description == "" then
			if unnumberedTranslations > 0 then
				maintenance:valueMissing({ name = descriptionArg, desc = "překlad pro který význam" })
				html:wikitext( maintenance:getText() )
			end
		else
			html
				:tag( "dfn" )
					:wikitext( mw.text.nowiki( description ) )
					:done()
		end
		
		for langTag, translation in pairs( templateArgs ) do
			local langName = Language:getFullName( langTag )
			if langName then
				local langAttrs = string.gmatch( translation, ' lang="([a-z]+)"' )
				local isWrongLangAttr = false
				for langAttr in langAttrs do
					if langAttr ~= langTag then
						isWrongLangAttr = true
					end
				end
				if isWrongLangAttr then
					errorData.text = "Chybně vložený překlad (<nowiki>{{</nowiki>[[Šablona:P|P]]<nowiki>}}</nowiki> obsahuje jiný kód jazyka)."
					errorData.category = "Opravit vložení překladu"
					translation = translation .. Error.getText( errorData )
				end
				translations[langName] = translation
			elseif langTag ~= descriptionArg then
				errors[langTag] = translation
			end
		end
		
		html = html:tag( "ul" )
		
		for langName, translation in kpairs( translations ) do
			if not string.match( translation, "class=\"translation%-item\"" ) then
				errorData.text = "Chybně vložený překlad (neobsahuje <nowiki>{{</nowiki>[[Šablona:P|P]]<nowiki>}}</nowiki>)."
				errorData.category = "Opravit vložení překladu"
				translation = translation .. Error.getText( errorData )
			end
			html:tag( "li" )
				:attr( "style", "page-break-inside: avoid;" )   -- Diskuse_k_šabloně:P#rozdělení_složeného_slova_do_více_odstavců
				:wikitext(
					langName,
					": ",
					translation
				)
				:done()
		end
		
		for langTag, translation in kpairs( errors ) do
			errorData.text = "Neznámý kód jazyka „" .. langTag .."“."
			errorData.category = "Zkontrolovat jazyk"
			html:tag( "li" )
				:wikitext( Error.getText( errorData ) )
				:done()
		end
		
	end
	
	html = html:allDone()
	
	output = frame:preprocess( tostring( html ) )
	
	return output
	
end


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