Modul:CitLib

A Wikipédiából, a szabad enciklopédiából

CitLib[mi ez?] • [dokumentáció: mutat, szerkeszt] • [tesztek: létrehozás]

-- Version 2020.06.20
require('strict')
local ds = require('Modul:Dátumszűrés')
local nyksz = require('Modul:Nyelvkódszűrő')

local args = {}
local used_args = {}
local config = {}
local suta={}


local lang = mw.getContentLanguage()

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

local oumax = 1000 -- a tesztüzenetek számának maximuma
local oumarad = oumax

local function kimenetre(x) 
		if x then table.insert(suta,x) end
end



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
		oumarad = oumarad - 1
		if oumarad == 0 then return end
		table.insert(ou, tostring(var) .. '\n ')
		mw.log(tostring(var) .. '\n ')
	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 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
			local msg = 'CitLib: Nem használt paraméter: ' .. n
			mw.log(msg)
			mw.addWarning(msg)
			kategorizalas_van = true
		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
	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, vol, 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')
	vol = getArg(true, 'vol', 'kötet')
	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?)'
		--pop('megkérdőjelezte a címet', tit)
		error(true, 'nincs elsődleges cím')
		table.insert(alkategoriak, 'Hibásan használt CitLib – cím nélkül')
	end
	
	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)

	if vol then
		CoinS_tit = CoinS_tit .. vol
	end

	-- 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
	
    -- nyelv kezelése
    local tagok={}
    local i,j,szel
if lan then    
    local   nyelvek,hibak,nyomok = nil
    nyelvek,hibak,nyomok = nyksz.nyelvszuro(lan)
       if hibak then
          --pop("kapott hibalista=", nyomok)
          error(true, hibak ..'hibás nyelvkód')	
          table.insert(alkategoriak, 'Hibásan használt CitLib – nyelvkódhiba')
       end 
       if nyelvek then
          --pop("kapott nyelvlista=", nyelvek)
          lan = nyelvek
       end          
       if nyomok then
          --pop("nyomkövetés a nyelvszűrésen belül=", nyomok)
       end   
       if #tit == 0 then
	  error(true, 'nyelv cím nélkül')	
	  table.insert(alkategoriak, 'Hibásan használt CitLib – nyelv cím nélkül')
       end         
end --if lan

	
-- 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
        
        if sablon_kimenetek_kellenek then
                if capaut then kimenetre(capaut) end
                if cap then kimenetre(cap) end
                if aut then kimenetre(aut) end
                if tit then kimenetre(tit) end
                if vol then kimenetre(vol) end
                if lan then kimenetre(lan) end
                if ass then kimenetre(ass) end
                if edi then kimenetre(edi) end
                if kiadas then kimenetre(kiadas) end
                if ann then kimenetre(ann .. '. ') end
                if pag then kimenetre(pag) end
                if ser then kimenetre(ser) end
                if sernr then kimenetre(sernr) end
                if isbn then kimenetre(isbn) end
                if archd then kimenetre(archd) end
                if accd then kimenetre(accd) end
                if misc then kimenetre(misc) end
                if coins then kimenetre(coins) end
        end
		--pop('Kimenetre täbläja',table.concat(suta,'{}') )
		if ou then kimenetre(table.concat(ou,'<###>')) end 		     						--nyomkövetésnél 
        checkArgs()

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

        if kategorizalas_van then
                 kimenetre('[[Kategória:Hibás paraméterezésű CitLib sablont tartalmazó lapok]]')
        end
        if alkategoriak_kellenek and #alkategoriak > 0 then
                 kimenetre('[[Kategória:' .. table.concat(alkategoriak, ']][[Kategória:') .. ']]')
        end
        local ks = ''
		if #suta ~= 0 then ks = table.concat(suta, ' ') end
        return mw.text.trim(ks)
end -- function _citlib
-- Lua-hiba a(z) Modul:Homokozó/Pepo41/MCitlib modulban a(z) 585. sorban: 
 --invalid value (table) at index 6 in table for 'concat' 

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