Modul:Source: Porovnání verzí

Z Wikislovníku
Skočit na navigaci Skočit na vyhledávání
Smazaný obsah Přidaný obsah
umožnění vlastního formátování i nezadávaných parametrů
optimalizace kódu; unifikace; DRY refaktorizace
Řádek 4: Řádek 4:
-- @author
-- @author
-- [[meta:User:Danny B.]]
-- [[meta:User:Danny B.]]
local Source = {}
local _module = {}
----------------------------------------
----------------------------------------


Řádek 19: Řádek 19:
-- @return
-- @return
-- Preprocessed wikitext
-- Preprocessed wikitext
function Source.print( frame )
function _module.print( frame )
local source = frame.args[1]
local source = mw.text.trim( frame.args[1] or "" )
if not source then
if source == "" then
error( "Missing first argument – name of the source module" )
error( "Missing the first argument – name of the source module" )
end
end
local sourceTitle = frame:getTitle() .. "/" .. source
source = mw.text.trim( source )
local sourceObjectExist, sourceObject = pcall( require, sourceTitle )
local sourceObjectExist, sourceObject = pcall( require, frame:getTitle() .. "/" .. source )
if not sourceObjectExist then
if not sourceObjectExist then
error( "Missing \"" .. frame:getTitle() .. "/" .. source .. "\" module" )
error( "Missing \"" .. sourceTitle .. "\" module" )
end
end
if sourceObject.editions and not sourceObject.editions.switch then
if sourceObject.editions and not sourceObject.editions.switch then
error( "\"switch\" field is missing in " .. frame:getTitle() .. "/" .. source .. ".editions" )
error( "\"switch\" field is missing in " .. sourceTitle .. ".editions" )
end
end
Řádek 45: Řádek 44:
local errors = {}
local errors = {}
local data = {}
local data = {}
local argumentDefaults = {
["díl"] = {
description = "díl"
},
["heslo"] = {
description = "heslo",
fillIn = "kapitola",
fill = function ( args )
return "„" .. args["heslo"] .. "“"
end
},
["rok"] = {
description = "rok vydání"
},
["strany"] = {
description = "citovaná strana či strany"
},
["vydání"] = {
description = "vydání"
},
["url"] = {
description = "odkaz na online verzi"
}
}
for param, value in pairs( templateArgs ) do
for param, value in pairs( templateArgs ) do
Řádek 55: Řádek 79:
for param, value in pairs( sourceObject.arguments or {} ) do
for param, value in pairs( sourceObject.arguments or {} ) do
if value.required and not args[param] then
if value.required and not args[param] then
table.insert( errors, { missingValue = { paramName = param, paramDesc = value.description } } )
table.insert( errors, { missingValue = { paramName = param, paramDesc = value.description or argumentDefaults[param].description } } )
end
end
end
end


if sourceObject.editions and args[sourceObject.editions.switch] and not sourceObject.editions[args[sourceObject.editions.switch]] then
if sourceObject.editions and args[sourceObject.editions.switch] and not sourceObject.editions[args[sourceObject.editions.switch]] then
table.insert( errors, { unknownValue = { paramName = param, paramDesc = value.description } } )
table.insert( errors, { unknownValue = { paramName = param, paramDesc = value.description or argumentDefaults[param].description } } )
end
end
Řádek 100: Řádek 124:
if sourceObject.arguments then
if sourceObject.arguments then
for param, value in pairs( sourceObject.arguments ) do
for param, value in pairs( sourceObject.arguments ) do
data[( value.fillIn or param )] = ( value.fill and value.fill( args ) or args[param] )
data[( value.fillIn or argumentDefaults[param].fillIn or param )] = ( value.fill and value.fill( args ) or argumentDefaults[param].fill and argumentDefaults[param].fill( args ) or args[param] )
end
end
end
end
Řádek 121: Řádek 145:


----------------------------------------
----------------------------------------
return Source
return _module

Verze z 28. 1. 2016, 22:05

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 = {
		["díl"] = {
			description = "díl"
		},
		["heslo"] = {
			description = "heslo",
			fillIn = "kapitola",
			fill = function ( args )
				return "„" .. args["heslo"] .. "“"
			end
		},
		["rok"] = {
			description = "rok vydání"
		},
		["strany"] = {
			description = "citovaná strana či strany"
		},
		["vydání"] = {
			description = "vydání"
		},
		["url"] = {
			description = "odkaz na online verzi"
		}
	}
	
	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 = param, paramDesc = value.description or argumentDefaults[param].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].fillIn or param )] = ( value.fill and value.fill( args ) or 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