Module:Citation

De ProleWiki
local p = {}

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

local function italics(var)
	return "<i>" .. var .. "</i>"
end

local function parenthesis(var)
	return "(" .. var .. ")"
end

function p.cite(frame)
	local pframe = frame:getParent()
	local Auteur = pframe.args.author
	local Annee = pframe.args.year or pframe.args['date']
	local Chapitre = pframe.args.chapter
	local URLChapitre = pframe.args['chapter-url']
	local Section = pframe.args.section
	local Titre = pframe.args.title
	local URLTitre = pframe.args['title-url']
	local TitreBibliotheque = pframe.args['library-title']
	local Volume = pframe.args.volume
	local Serie = pframe.args.series or pframe.args.journal
	local Traduction = pframe.args['trans-title']
	local LangueTrad = pframe.args['trans-lang']
	local Page = pframe.args.page or pframe.args.pages
	local PDF = pframe.args.pdf
	local Ville = pframe.args.city
	local Editeur = pframe.args.publisher or pframe.args.via
	local ISBN = pframe.args.isbn or pframe.args.issn
	local ISSN = false
	if is_set(pframe.args.issn) then
		ISSN = true
	end
	local DOI = pframe.args.doi
	local LG = pframe.args.lg
	local MIA = pframe.args.mia
	local Citation = pframe.args.quote
	local Image = pframe.args.image
	local LegendeImage = pframe.args['image-caption']
	local SansPointFinal = pframe.args.dotless


	if is_set(Auteur) then
		if is_set(Annee) then
			first_part = Auteur .. " " .. parenthesis(Annee) .. "."
		else
			first_part = Auteur .. "."
		end
	else
		first_part = ''
	end

	if is_set(Citation) then
		Citation = string.gsub(Citation, "\n", "<br>")
		Citation = "“" .. Citation .. "”"
		local div = mw.html.create('div')
		div
			:attr("style", "width:80%; margin-left:10%;")
			:wikitext(Citation)
		local br = mw.html.create('br', selfClosing)
		first_part = italics(tostring(div)) .. tostring(br) .. first_part
	end

	if is_set(Image) then
		local span = mw.html.create('span')
		span
			:wikitext('[[File:' .. Image .. '|center|300px|frameless]]')

		if is_set(LegendeImage) then
			local caption = mw.html.create('div')
			caption
				:attr("style", "text-align:center;font-size:85%;")
				:wikitext(LegendeImage)
			first_part = tostring(span) .. tostring(caption) .. first_part
		else
			first_part = tostring(span) .. first_part
		end

	end

	-- deuxieme partie: Titre(URL), Serie, Volume, URLChapitre, Annee, Page, Traduction(Langue), PDF
	local deuxieme_partie = ''

	if is_set(Titre) then
		if is_set(URLTitre) then
			Titre = "[" .. URLTitre .. " " .. Titre .. "]"
		else
			if is_set(TitreBibliotheque) then
				Titre = "[[Library:" .. TitreBibliotheque .. "|" .. Titre .. "]]"
			end
		end
		Titre = italics(Titre)
		if not is_set(Serie) then
			if is_set(Volume) then
				Titre = Titre .. ", vol. " .. Volume
			end
		end
		deuxieme_partie = Titre
		if is_set(Chapitre) then
			if is_set(URLChapitre) then
				Chapitre = "[" .. URLChapitre .. " " .. Chapitre .. "]"
			end
			if is_set(Section) then
				Chapitre = Chapitre .. "; " .. Section
			end
			deuxieme_partie = deuxieme_partie .. ": " .. "'" .. Chapitre .. "'"
		end

		if not is_set(Auteur) then
			if is_set(Annee) then
				deuxieme_partie = deuxieme_partie .. " " .. parenthesis(Annee)
			end
		end

		if is_set(Traduction) then
			if is_set(LangueTrad) then
				deuxieme_partie = deuxieme_partie .. " " .. "(" .. LangueTrad .. ": " .. Traduction .. ")"
			else
				deuxieme_partie = deuxieme_partie .. " " .. parenthesis(Traduction)
			end
		end
		if is_set(Page) and not is_set(Volume) then
			if tonumber(Page) ~= nil then
				Page = "p." .. " " .. Page
			else
				Page = "pp." .. " " .. Page
			end
			deuxieme_partie = deuxieme_partie .. " " .. parenthesis(Page) .. "."
		else
			deuxieme_partie = deuxieme_partie .. "."
		end
	end

	if is_set(Serie) then
		if is_set(Volume) then
			Serie = Serie .. ", vol." .. Volume
		end

		if is_set(Page) and is_set(Volume) and is_set(Serie) then
			if tonumber(Page) ~= nil then
				Page = "p." .. " " .. Page
			else
				Page = "pp." .. " " .. Page
			end
			deuxieme_partie = deuxieme_partie .. " " .. Serie .. " " .. parenthesis(Page) .. "."
		else
			deuxieme_partie = deuxieme_partie .. " " .. Serie .. "."
		end
	end

	if is_set(PDF) then
		local span = mw.html.create('span')
		span
			:attr('title', 'Telecharger en PDF')
			:addClass('plainlinks')
			:tag('small')
			:wikitext('[' .. PDF .. " " .. mw.text.nowiki('[PDF]') .. ']')
		deuxieme_partie = deuxieme_partie .. " " .. tostring(span)
	end

	-- troisieme partie: Ville, Editeur

	local troisieme_partie = ''

	if is_set(Ville) then
		if is_set(Editeur) then
			troisieme_partie = Ville .. ": " .. Editeur .. "."
		else
			troisieme_partie = Ville .. "."
		end
	end

	if not is_set(Ville) then
		if is_set(Editeur) then
			troisieme_partie = Editeur .. "."
		else
			troisieme_partie = ''
		end
	end

	-- quatrieme partie: ISBN, DOI, Library Genesis, Marxists Internet Archive

	local quatrieme_partie = ''

	if is_set(ISBN) then
		local span = mw.html.create('span')
		span
			:addClass('plainlinks')
		if (ISSN) then
			span
				:tag('small')
				:attr('title', 'Portail ISSN')
				:wikitext('[' .. "https://portal.issn.org/resource/ISSN/" .. ISBN .. " " .. "ISSN " .. ISBN .. ']')
		else
			span
				:tag('small')
				:attr('title', 'WorldCat')
				:wikitext('[' .. "https://www.worldcat.org/search?qt=worldcat_org_all&q=" .. ISBN .. " " .. "ISBN " .. ISBN .. ']')
		end
		quatrieme_partie = quatrieme_partie .. tostring(span)
	end

	if is_set(DOI) then
		local doispan = mw.html.create('span')
		doispan
			:tag('small')
			:wikitext("doi: " .. DOI)
		quatrieme_partie = quatrieme_partie .. " " .. tostring(doispan)
		local span = mw.html.create('span')
		span
			:attr('title', 'Lien Sci-Hub')
			:addClass('plainlinks')
			:tag('small')
			:wikitext('[' .. "https://sci-hub.se/" .. DOI .. " " .. mw.text.nowiki('[HUB]') .. ']')
		quatrieme_partie = quatrieme_partie .. " " .. tostring(span)
	end

	if is_set(LG) then
		local span = mw.html.create('span')
		span
			:attr('title', 'Lien Library Genesis')
			:addClass('plainlinks')
			:tag('small')
			:wikitext('[' .. LG .. " " .. mw.text.nowiki('[LG]') .. ']')
		quatrieme_partie = quatrieme_partie .. " " .. tostring(span)
	end

	if is_set(MIA) then
		local span = mw.html.create('span')
		span
			:attr('title', 'Lien Marxists Internet Archive')
			:addClass('plainlinks')
			:tag('small')
			:wikitext('[' .. MIA .. " " .. mw.text.nowiki('[MIA]') .. ']')
		quatrieme_partie = quatrieme_partie .. " " .. tostring(span)
	end


	if is_set(deuxieme_partie) then
		deuxieme_partie = " " .. deuxieme_partie
	end

	if is_set(troisieme_partie) then
		troisieme_partie = " " .. troisieme_partie
	end

	if is_set(quatrieme_partie) then
		quatrieme_partie = " " .. quatrieme_partie
	end

	local rendu = first_part .. deuxieme_partie .. troisieme_partie .. quatrieme_partie

	-- supprimer les espaces au debut et à la fin
	rendu = string.gsub(rendu, "^%s*(.-)%s*$", "%1")

	if SansPointFinal then
		rendu = rendu:sub(1, -2)
	end

	return rendu
end
return p