Modul:Source

Z Wikislovníku
Skočit na navigaci Skočit na vyhledávání

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

-- @brief
--  Generic wrapper for source templates.
-- 
-- @author
--  [[meta:User:Danny B.]]
local _module = {}
----------------------------------------


local Error = require( "Module:Error" )


-- @brief
--  Write the citation template for given source.
-- 
-- @param
--  frame The current frame object
-- 
-- @return
--  Preprocessed wikitext
function _module.print( frame )
	
	local source = mw.text.trim( frame.args[1] or "" )
	
	if source == "" then
		error( "Missing the first argument – name of the source module" )
	end
	
	local sourceTitle = frame:getTitle() .. "/" .. source
	local sourceObjectExist, sourceObject = pcall( require, sourceTitle )
	
	if not sourceObjectExist then
		error( "Missing \"" .. sourceTitle .. "\" module" )
	end
	
	if sourceObject.editions and not sourceObject.editions.switch then
		error( "\"switch\" field is missing in " .. sourceTitle .. ".editions" )
	end
	
	local output
	local templateArgs = frame:getParent().args
	local errorData = { template = source }
	local args = {}
	local errors = {}
	local data = {}
	
	local argumentDefaults = {
		["datum přístupu"] = {
			description = "datum, kdy byl naposledy odkazovaný dokument skutečně viděn"
		},
		["díl"] = {
			description = "díl"
		},
		["heslo"] = {
			description = "heslo",
			fillIn = "kapitola"
		},
		["kapitola"] = {
			description = "kapitola"
		},
		["rok"] = {
			description = "rok vydání"
		},
		["strany"] = {
			description = "citovaná strana či strany"
		},
		["url"] = {
			description = "odkaz na online verzi"
		},
		["vydání"] = {
			description = "vydání"
		}
	}
	
	for param, value in pairs( templateArgs ) do
		value = mw.text.trim( value )
		if value ~= "" then
			args[param] = value
		end
	end
	
	for param, value in pairs( sourceObject.arguments or {} ) do
		if value.required and not args[param] then
			table.insert( errors, { missingValue = { paramName = param, paramDesc = value.description or argumentDefaults[param].description } } )
		end
	end

	if sourceObject.editions and args[sourceObject.editions.switch] and not sourceObject.editions[args[sourceObject.editions.switch]] then
		table.insert( errors, { unknownValue = { paramName = sourceObject.editions.switch, paramDesc = sourceObject.arguments[sourceObject.editions.switch].description or argumentDefaults[sourceObject.editions.switch].description } } )
	end
	
	if sourceObject.fill then
		sourceObject.data = {}
		sourceObject.errors = {}
		sourceObject.fill( args )
		data = sourceObject.data
		errors = sourceObject.errors
	end
	
	if next( errors ) then
		local errorTexts = {}
		local method = mw.title.getCurrentTitle().namespace == 0 and "getText" or "getMessage"
		for index, err in ipairs( errors ) do
			for key, value in pairs( err ) do
				errorData[key] = value
			end
			table.insert( errorTexts, Error[method]( errorData ) )
		end
		output = table.concat( errorTexts, "<br />" )
		output = frame:preprocess( output )
	else
		local citeArgs = {
			["zdroj"] = source
		}
		
		if not sourceObject.fill then
			if sourceObject.common then
				for param, value in pairs( sourceObject.common ) do
					data[param] = ( type( value ) == "function" and value( args ) or value )
				end
			end
			if sourceObject.editions then
				for param, value in pairs( sourceObject.editions[args[sourceObject.editions.switch]] ) do
					data[param] = ( type( value ) == "function" and value( args ) or value )
				end
			end
			if sourceObject.arguments then
				for param, value in pairs( sourceObject.arguments ) do
					data[( value.fillIn or argumentDefaults[param] and argumentDefaults[param].fillIn or param )] = ( value.fill and value.fill( args ) or argumentDefaults[param] and argumentDefaults[param].fill and argumentDefaults[param].fill( args ) or args[param] )
				end
			end
		end
		
		for param, value in pairs( data ) do
			citeArgs[param] = value
		end
		
		output = frame:expandTemplate({
			title = "Citace " .. sourceObject.cite,
			args = citeArgs
		})
	end
	
	return output
	
end


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