Modul:CitLib

A Wikidézetből, a szabad idézetgyűjteményből.

A modult a Modul:CitLib/doc lapon tudod dokumentálni

--Version 2014_09_02
require('Modul:No globals')
local ds = require('Modul:Dátumszűrés')

local args = {}
local used_args = {}
local config = {}
local lang = mw.getContentLanguage()

-- hibakeresésre szolgáló globális változó, ami tartalmazza az analízisre
-- szolgáló üzeneteket, az alábbi pop() függvény gyűjtögeti a tartalmát
local ou = ''

local function pop(...)  -- idézőjeles rész és bővítmények az ou számára
--[[ memóriatúllépést okoz, csak teszteléshez
	local list = {...}
	for _, var in pairs(list) do
		ou = ou .. tostring(var) .. '\n '
		mw.log(ou)
	end
]]
end


local function isRoman(v)
	if type(v) ~= 'string' then
		-- római számok stringek
		return false
	end
	return ( v:match( '^[IVXLCDM]+$' ) ~= nil )
end

local lans = {
	an = true,
	ar = true,
	aze = true,
	be = true,
	bg = true,
	bn = true,
	bs = true,
	ca = true,
	chm = true,
	cy = true,
	cs = true,
	da = true,
	de = true,
	ee = true,
	el = true,
	en = true,
	eo = true,
	es = true,
	et = true,
	eu = true,
	fa = true,
	fi = true,
	fo = true,
	fr = true,
	fy = true,
	ga = true,
	gd = true,
	gl = true,
	grc = true,
	gu = true,
	he = true,
	hi = true,
	hr = true,
	ht = true,
	hu = true,
	hy = true,
	hz = true,
	id = true,
	is = true,
	it = true,
	ja = true,
	jp = true,
	ka = true,
	kk = true,
	km = true,
	kn = true,
	ko = true,
	ku = true,
	ky = true,
	lat = true,
	lb = true,
	lo = true,
	lit = true,
	lv = true,
	mk = true,
	mn = true,
	mo = true,
	ms = true,
	mt = true,
	my = true,
	ne = true,
	nl = true,
	nn = true,
	no = true,
	oc = true,
	pa = true,
	pl = true,
	pt = true,
	rm = true,
	ro = true,
	ru = true,
	rue = true,
	sa = true,
	sah = true,
	scn = true,
	se = true,
	sh = true,
	sk = true,
	slo = true,
	sm = true,
	sq = true,
	sr = true,
	sv = true,
	tam = true,
	te = true,
	tet = true,
	tg = true,
	th = true,
	tk = true,
	tag = true,
	to = true,
	tr = true,
	tat = true,
	ty = true,
	uk = true,
	ur = true,
	uz = true,
	vi = true,
	yi = true,
	zh = true
}


local hibavan = false
local hiba_jelzes = {}
local kategorizalas_van = false

local function error(kategorizalas_kell, about)
	-- Enclose errorMsg with <span>
	-- Precondition:
	--     about  -- string
	hibavan = true
	if kategorizalas_kell then
		kategorizalas_van = true
	end
	local r = about
	if type( about ) == 'string' then
		if #about == 0 then
			r = 'Lua-hiba'
		end
	else
		r = tostring( about )
	end
	table.insert(hiba_jelzes, '<span class="error">' .. r .. '</span>')
end

local function getArgMain(names, le, suffix)
	for _, v in ipairs(names) do
		if suffix then
			v = v .. tostring(suffix)
		end
		used_args[v] = true
		if args[v] and args[v] ~= '' then
			if le and type(args[v]) == 'string' then
				-- lecsípjük az utolsó pontot vagy vesszőt
				return args[v]:gsub('[%.,]$', '')
			else
				return args[v]
			end
		end
	end
	return nil
end

local function getArg(le, ...) -- itt eredetileg a frame nem paraméter
	-- Retrieve template argument
	-- Precondition:
	-- 	   le: true értékkel jelzi, hogy levágható a paraméter végéről a felesleges vessző és pont
	--     ...: paraméternevek, az első megadott és nem üreset adja vissza

	return getArgMain({...}, le)
end

local function getArgMore(le, max, ...)
	local ret = {}
	local val = getArg(le, ...)
	if val then
		table.insert(ret, val)
	end
	for i = 2, max do
		val = getArgMain({...}, le, i)
		if val then
			table.insert(ret, val)
		end
	end
	return ret
end

local function checkArgs()
	for n, v in pairs(args) do
		if not used_args[n] then
			mw.log('CitLib: Nem használt paraméter: ' .. n)
			-- error(true, 'nem használt paraméter: ' .. n)
		end
	end
end

local function getConf(conf)
	local v = config[conf]
	return (v ~= '' and v) or nil
end

local function urlencode(str)
	-- URL kódolása
	do return str end
	return (str
		:gsub('%s', '%%20')
		:gsub('"', '%%22')
		:gsub("'", '%%27')
		:gsub('<', '%%3c')
		:gsub('>', '%%3e')
		:gsub('%[', '%%5b')
		:gsub('%]', '%%5d')
		:gsub('{', '%%7b')
		:gsub('|', '%%7c')
		:gsub('}', '%%7d')
	)
end

-- Gets the display text for a wikilink like [[A|B]] or [[B]] gives B
local function removewikilink( str )
	return (str:gsub( '%[%[([^%[%]]*)%]%]', function (l)
		return l:gsub( '^[^|]*|(.*)$', '%1' ):gsub('^%s*(.-)%s*$', '%1');
	end))
end


local OCinSoutput = setmetatable( {}, {
	__newindex = function(self, key, value)
		if value ~= nil and value ~= '' then
			rawset( self, #self+1, table.concat{ key, '=', mw.uri.encode( removewikilink( value ) ) } );
				--> OCinSoutput[1],[2],[3]... dagad
				-- az egész konstrukció a rawset és a __newindex  segítségével növeli e táblát a key value párokkal
				-- pop('mit tesz a concat:', self[#self]); --> rft_val_fmt=info:ofi/fmt:kev:mtx:book
			end
		end
	}
)


local function _citlib()
	local alkategoriak = {}

	local cap, capaut, aut, tit, subtit, ass, edi, red, red2, loc, loc2
	local ann, pag, ser, sernr, isbn, misc, url, accd, aurl, archd, lan
	local alkategoriak_kellenek = getConf('alkategóriák kellenek')
	--pop('Kellenek az alkategóriák??', alkategoriak_kellenek) -- igen, vagy nem a lehetséges válasz
	if alkategoriak_kellenek then
		if alkategoriak_kellenek == 'igen' then
			--pop('Kellenek az alkategóriák', alkategoriak_kellenek)
			alkategoriak_kellenek = true
		else
			alkategoriak_kellenek = false
		end
	end

	local sablon_kimenetek_kellenek = getConf('sablon kimenetek kellenek') -- kívánságra kikapcsolható
	--pop('Sablon kimenetek kellenek?', sablon_kimenetek_kellenek) -- igen, vagy nem a lehetséges válasz
	if sablon_kimenetek_kellenek then
		if sablon_kimenetek_kellenek == 'igen' then
			--pop('Kellenek az alkategóriák', alkategoriak_kellenek)
			sablon_kimenetek_kellenek = true
		else
			sablon_kimenetek_kellenek = false
		end
	else
		-- ha a getConf nil értékkel tért vissza
		sablon_kimenetek_kellenek = true
	end

	cap = getArg(true, 'cap', 'fejezetcím')
	capaut = getArgMore(true, 3, 'capaut', 'fejezetszerző')
	aut = getArgMore(true, 3, 'szerző', 'aut')
	tit = getArg(true, 'cím', 'tit')
	subtit = getArg(true, 'alcím', 'subtit')
	ass = getArgMore(true, 3, 'ass', 'közreműködők')
	isbn = getArg(true, 'isbn')
	edi = getArg(true, 'edi', 'kiadás')
	loc = getArg(true, 'loc', 'hely')
	loc2 = getArg(true, 'loc2', 'hely2')
	red = getArg(true, 'red', 'kiadó')
	red2 = getArg(true, 'red2', 'kiadó2')
	ann = getArg(true, 'év', 'ann')
	--if ann then ann = string.gsub(ann, '[%[%]]', '')end Pasztilla közlése szerint
	pag = getArg(true, 'oldal', 'pag')
	ser = getArg(true, 'ser', 'sorozat')
	sernr = getArg(true, 'sernr', 'sorozatszám')
	misc = getArg(true, 'egyéb', 'misc')
	url = getArg(false, 'url')
	accd = getArg(true, 'elér', 'accd')
	if accd then accd = string.gsub(accd, '[%[%]]', '') end
	aurl = getArg(true, 'archívurl', 'aurl')
	archd = getArg(true, 'archívdátum', 'archd')
	if archd then archd = string.gsub(archd, '[%[%]]', '') end
	lan = getArg(true, 'nyelvkód', 'lan')

	-- fejezetszerzők összeállítása capautban
	if #capaut > 0 then
		capaut = table.concat(capaut, '&#8201;&#8211;&#8201;') .. ': '
	else
		capaut = nil
	end

	-- fejezetcím
	local CoinS_cap
	if cap then
		CoinS_cap = cap
	end

	-- szerzők összeállítása
	if #aut > 0 then
		for _, v in ipairs(aut) do
			OCinSoutput['rft.au'] = v
		end
		aut = table.concat(aut, '&#8201;&#8211;&#8201;') .. ': '
	else
		aut = nil
	end

	--cím és alcím
	local CoinS_tit = nil
	if tit then
		tit = mw.text.trim(lang:ucfirst(tit))
	else
		tit = '(cím?)'
		error(true, 'nincs elsődleges cím')
		table.insert(alkategoriak, 'Hibásan használt CitLib – cím nélkül')
	end
	--pop('megkérdőjelezte a címet', tit)
	if subtit then
		subtit = lang:ucfirst(subtit)
		-- ha a cím végén kettőspont van, leszedjük, hogy ne legyen kettő egymás után
		tit = tit:gsub(':$', '') .. ': ' .. subtit
	end
	CoinS_tit = tit:gsub('%.$', '')
	if not tit:match('[%.%?!,\166]$') then
		tit = tit .. '.'
	end

	tit = "''" .. tit .. "''"
	--pop('cím in italic', tit)

	-- asszisztencia
	if #ass > 0 then
		ass = lang:ucfirst(table.concat(ass, '&#8211;')) .. '.'
	else
		ass = nil
	end

	-- edi
	local CoinS_edi = nil
	if edi then
		CoinS_edi = edi
		edi =  edi .. '.'
	end

	local kiadas = ''
	local CoinS_loc, CoinS_red
	if loc then
		if loc2 then
			if red then
				if red2 then --loc,loc2,red,red2
					kiadas = loc..': '.. red..'; '.. loc2..': '.. red2..'.'
					            CoinS_loc = loc ; CoinS_red = red
				else         --loc,loc2,red,not red2
					kiadas = loc..'; '.. loc2..': '.. red..'.'
				end
			else
				if red2 then --loc,loc2,not red, red2
					kiadas = loc..': (kiadó nélkül); '.. loc2..': '.. red2..'.'
					            CoinS_loc = loc2 ; CoinS_red = red2
				else         --loc,loc2,not red, not red2
					kiadas = loc..'; '.. loc2..': '.. '(kiadó nélkül)'..'.'
				end
			end
		else
			if red then
				if red2 then  --loc,not loc2, red, red2
					kiadas = loc..': '.. red..'; '.. '(hely nélkül)'..': '.. red2..'.'
					            CoinS_loc = loc ; CoinS_red = red
				else          --loc,not loc2, red, not red2
					kiadas = loc..': '.. red..'.'
					            CoinS_loc = loc ; CoinS_red = red
				end
			else
				if red2 then  --loc,not loc2, not red, red2
					kiadas = loc..': '.. '(kiadó nélkül)'..'; '..'(hely nélkül)'..': '.. red2..'.'
				else          --loc,not loc2, not red, not red2
					kiadas = loc..': '.. '(kiadó nélkül)'..'.'
				end
			end
		end
	else
		if loc2 then
			if red then
				if red2 then  --not loc,loc2, red,red2
					kiadas = '(hely nélkül)'..': '.. red..'; '..loc2..': '.. red2..'.'
								CoinS_loc = loc2 ; CoinS_red = red2
				else          --not loc,loc2,red, not red2
					kiadas = '(hely nélkül)'..': '.. red..'; '..loc2..': '.. '(kiadó nélkül)'..'.'
				end
			else
				if red2 then   --not loc,loc2, not red,red2
					kiadas = loc2..': '.. red2..'.'
					            CoinS_loc = loc2 ; CoinS_red = red2
				else           --not loc,loc2, not red,not red2
					kiadas = loc2..': '.. '(kiadó nélkül).'
				end
			end
		else
			if red then
				if red2 then   --not loc, not loc2,  red,red2
					kiadas = '(hely nélkül)'..': '.. red..';'.. red2..'.'
				else           --not loc, not loc2,  red, not red2
					kiadas = '(hely nélkül)'..': '.. red..'.'
				end
			else
				if red2 then   --not loc, not loc2,  not red,red2
					kiadas = '(hely nélkül)'..': '.. red2..'.'
				else   			--not loc, not loc2,  not red, not red2

				end
			end
		end
	end

  -- év

	local CoinS_ann = nil
	if ann then
		local count = 0
		-- kiskötőjelből nagy
		ann = ann:gsub('[%-]', '–')
		-- az évben csak számjegyek és nagykötőjel, valamint szögletes zárójelek maradhatnak
		ann, count = ann:gsub('[^%–%[%]0123456789]', '')
		if count > 0 then
			error(false, 'tiltott karakter az év paraméterben')
			table.insert(alkategoriak, 'Hibásan használt CitLib – tiltott karakter az év paraméterben')
		end
		pop('ev a gsubolása után', ann)
		CoinS_ann = ann
	end

	-- oldal adatok
	local CoinS_pag = nil
	if pag then
		pag = (pag
			:gsub('-', '–') -- kiskötőjelből nagykötőjel
			:gsub('—', '–') -- hetvenkvirtesből nagykötőjel
			:gsub('%.', '') -- pontból üreset
			:gsub(',', '.,')) -- végül vesszőből pontot követő vesszőt
		CoinS_pag = pag
		pag = '&#x20;' .. pag .. '. o.'
	end

	-- sorozat kezelése
	local sernum_van_de_ser_nincs = false
	local CoinS_ser = nil
	local CoinS_sernr = nil
	if ser then
		CoinS_ser = ser
		ser = '= ' .. ser .. ', '
	end
	if sernr ~= nil and ser == nil then
		sernr = '(sorozatszám?)'
		error(true, 'sorozatszám sorozat nélkül')
		table.insert(alkategoriak, 'Hibásan használt CitLib – sorozatszám sorozat nélkül')
	else
		if sernr then
			local proba_sernr = sernr:gsub('[^%dIVXLCDM]', '')
			if tonumber(proba_sernr) then
				CoinS_sernr = sernr
				sernr = sernr .. '.'
			elseif not isRoman(proba_sernr) then
				error(true, 'helytelen sorozatszám')
				table.insert(alkategoriak, 'Hibásan használt CitLib – helytelen sorozatszám')
			end
		end
	end

	-- ISBN kezelése
	local CoinS_isbn = nil
	if isbn then
		pop('van isbn paraméter', isbn)
		local helyes, hiba_str, isbn_table
		helyes, isbn, hiba_str, isbn_table = require('Modul:CheckISBN').isISBN(isbn, true)

		-- isbn = string.gsub(isbn_str, 'ISBN ([%dXx]+)', '[[Speciális:Könyvforrások/%1|ISBN %1]]')
		CoinS_isbn = table.concat(isbn_table, ', ')
		table.insert(hiba_jelzes, hiba_str)

		if helyes then
			pop('helyesek a paraméterek')
		else
			hibavan = true
			table.insert(alkategoriak, 'Hibásan használt CitLib – helytelen ISBN kód')
		end
	end

	-- URL kezelése
	local CoinS_url= nil
	if url then
		CoinS_url = url
		-- SZÜKSÉGES CSERÉK:cím=[http://crypto.com/papers/jbug-Usenix06-final.pdf Keyboards and Covert Channels"]
		--newline 	[ 	     ] 	     |    Ezek kellenek, mert az URL ráhúzásakor bezavarnak
		--space    &#91; 	&#93; 	&#124;
		--pop('tud-e az url-ről?', url)
		url = urlencode(url)
		--pop('ráhúzás előtti cím', tit)
		if cap then
			cap = string.gsub(cap, '\n', ' ')
			cap = string.gsub(cap, '%[', '&#91;')
			cap = string.gsub(cap, '%]', '&#93;')
			cap = string.gsub(cap, '%|', '&#124;')
			cap = '[' .. url .. ' ' .. cap .. ']'
		elseif tit then
			tit = string.gsub(tit, '\n', ' ')
			tit = string.gsub(tit, '%[', '&#91;')
			tit = string.gsub(tit, '%]', '&#93;')
			tit = string.gsub(tit, '%|', '&#124;')
			tit = '[' .. url .. ' ' .. tit .. ']'
			--pop('ráhúzza?', tit)
		end
	end
	if cap then
		cap = cap .. '. In '
	end

	if lan then
		if tit then
			if lans[lan] then
				-- ha fenn van a listánkon, akkor nem lehet parancsfájlhiba
				lan = mw.getCurrentFrame():expandTemplate{ title = lan, args = { lan } }
			else
				-- különben mi magunk kiírjuk, mert így beszédesebb
				error(true, 'ismeretlen nyelvkód')
				table.insert(alkategoriak, 'Hibásan használt CitLib – ismeretlen nyelvkód')
			end
		else
			-- error(true, 'nyelv cím nélkül')
		end
	end

	-- archívdátum -- archívurl
	if archd then -- ha van dátum
		local outarchd, dou
		dou = select(2, ds.datumszuro(archd))
		if dou then
			outarchd = ds.honapnevesdate(dou)
			if aurl then
				aurl = urlencode(aurl)
				archd = '<span title="' .. outarchd .. '">[' .. aurl .. ' arch]</span>'
			else
				error(true, 'archívdátum van, de archívurl nélkül')
				table.insert(alkategoriak, 'Hibásan használt CitLib – archívdátum archívurl nélkül')
			end
		else
			error(true, 'hibás archívdátum')
			table.insert(alkategoriak, 'Hibásan használt CitLib – hibás archívdátum')
		end
	end

	-- elérés dátuma
	if accd then -- ha van elérés
		local nyom, dou = ds.datumszuro(accd)
		-- pop('a dátumszűrés eredménye', nyom)
		if dou then
			accd = 'Hozzáférés: ' .. ds.honapnevesdate(dou)
		else
			error(true, 'hibás elérési dátum')
			table.insert(alkategoriak, 'Hibásan használt CitLib – hibás elérési dátum')
		end
	end

	-- CoinS
	local empty_span = '<span style="display:none;">&nbsp;</span>'
	local this_page = mw.title.getCurrentTitle()
	local ctx_ver = 'Z39.88-2004'
	local coins = nil
	OCinSoutput['rft.atitle'] = CoinS_cap
	OCinSoutput.rft_val_fmt = 'info:ofi/fmt:kev:mtx:book'
	OCinSoutput['rft.genre'] = 'book'
	--OCinSoutput['rft.au'] = CoinS_aut
	OCinSoutput['rft.btitle'] = CoinS_tit
	OCinSoutput['rft.place'] = CoinS_loc
	OCinSoutput['rft.date'] = CoinS_ann
	OCinSoutput['rft.series'] = CoinS_ser
	OCinSoutput['rft.issue'] = CoinS_sernr
	OCinSoutput['rft.pages'] = CoinS_pag
	OCinSoutput['rft.edition'] = CoinS_edi
	OCinSoutput['rft.pub'] = CoinS_red
	OCinSoutput['rft.isbn'] = CoinS_isbn
	OCinSoutput.rft_id = CoinS_url
	OCinSoutput.rfr_id = table.concat{ 'info:sid/', mw.site.server:match( '[^/]*$' ), ':', this_page.prefixedText }
	OCinSoutput = setmetatable( OCinSoutput, nil );

	-- sort with version string always first, and combine.
	table.sort( OCinSoutput )
	table.insert( OCinSoutput, 1, 'ctx_ver=' .. ctx_ver )  -- such as "Z39.88-2004"
	coins = table.concat(OCinSoutput, '&')
	coins = '<small><span title="' .. coins .. '" class="Z3988">' .. empty_span .. '</span></small>';

	-- output összeállítása
	local s = ''
	if sablon_kimenetek_kellenek then
		if capaut  then s = s..capaut..'  '  end
		if cap  then s = s..cap..'  '  end
		if aut then s = s.. aut..'  '  end
		if tit  then s = s..tit..'  '  end
		if lan  then s = s..lan..'  ' end
		if ass  then s = s..ass..'  '  end
		if edi  then s = s..edi..'  ' end
		if kiadas then s = s..kiadas..'  ' end
		if ann  then s = s..' '..ann..'. '  end
		if pag  then s = s..pag..'  '  end
		if ser  then s = s..ser..'  '  end
		if sernr  then s = s..sernr..'  '  end
		if isbn  then s = s..isbn..'  '  end
		if archd then s = s..archd..'  '  end
		if accd then s = s..accd..'  ' end
		if misc  then s = s..misc..'  '  end
		if coins  then s = s..coins..'  '  end
		-- if ou  then s = s..ou  end  --ez csak nyomkövetésnél kell, de akkor nagyon
	end

	checkArgs()

	if hibavan then
		s = s .. table.concat(hiba_jelzes, ', ')
	end

	if kategorizalas_van then
		s = s .. '[[Kategória:Hibás paraméterezésű CitLib-et tartalmazó lapok]]'
	end
	if alkategoriak_kellenek and #alkategoriak > 0 then
		s = s .. '[[Kategória:' .. table.concat(alkategoriak, ']][[Kategória:') .. ']]'
	end

	return mw.text.trim(s)
end

local function _run(a, c)
	args = a
	config = c or {}
	return _citlib()
end

local function run(frame)
	args = frame:getParent().args -- a sablon hívásakor megadott paraméterek
	config = frame.args -- az #invoke utasításban átadott paraméterek
	return _citlib()
end

local p = {
	run = run,
	_run = _run
}

return p