Jump to content

Module:CS1 translator

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Trappist the monk (talk | contribs) at 23:25, 18 August 2021 (create;). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.
(diff) ← Previous revision | Latest revision (diff) | Newer revision → (diff)

require ('Module:No globals');
local _month_xlate = require ('Module:Month translator')._month_xlate;

local getArgs = require ('Module:Arguments').getArgs;

local literatur_params_map = {
	['Titel'] = 'title',
	['Herausgeber'] = 'editor',
	['Sammelwerk'] = 'periodical',
	['Verlag'] = 'publisher',
	['Ort'] = 'location',
	['Jahr'] = 'year',
	['ISBN'] = 'isbn',
	['Seiten'] = 'page',
	['Online'] = 'url',
	['Band'] = 'volume',
	['Datum'] = 'date',
	['Tag'] = '_day',
	['Monat'] = '_month',
	['Jahr'] = 'year',
	['Nummer'] = 'number',
	['Autor'] = 'author',
	['Originalsprache'] = 'language',
	['Originaltitel'] = 'title',
	['Originaljahr'] = 'orig-date',
	}


--[[--------------------------< I S _ S E T >------------------------------------------------------------------
Returns true if argument is set; false otherwise. Argument is 'set' when it exists (not nil) or when it is not an empty string.
]]

local function is_set( var )
	return not (var == nil or var == '')
end


--[[--------------------------< L I T E R A T U R >------------------------------------------------------------
]]

local function literatur (frame)
	local args_t = getArgs (frame);
	local cite_args_t = {};
--error (mw.dumpObject (args_t))
	if not args_t['Datum'] and args_t['Jahr'] and args_t['Monat'] then
		table.insert (cite_args_t, table.concat ({'date', '=', _month_xlate ({mw.text.trim ((args_t['Tag'] or '') .. ' ' .. args_t['Monat'] .. ' ' .. args_t['Jahr'])})}));
	elseif args_t['Datum'] then
		table.insert (cite_args_t, table.concat ({'date', '=', _month_xlate ({args_t['Datum']})}));
	end
	args_t['Tag'] = nil;
	args_t['Monat'] = nil;
	args_t['Jahr'] = nil;
	args_t['Datum'] = nil;

	for param, val in pairs (args_t) do
		if val then
			if literatur_params_map[param] then									-- if literatur param maps to cs1|2 template parameter
				table.insert (cite_args_t, table.concat ({literatur_params_map[param], '=', val}));	-- use the cs1|2 parameter
			else
				table.insert (cite_args_t, table.concat ({param, '=', val}));	-- use literatur param 
			end
		end
	end
	
	table.sort (cite_args_t);
--error (mw.dumpObject (cite_args_t))
	local out_t = {}
	
	for _, arg in ipairs (cite_args_t) do
		local param, val = arg:match ('([^=]+)=(.+)');
		out_t[param] = val;
	end
--error (mw.dumpObject (out_t))
	return frame:expandTemplate{title = 'citation', args = out_t};
end

return {literatur = literatur}