Aller au contenu

Module:Quote

De Wikidébats, l'encyclopédie des débats et des arguments « pour » et « contre »

La documentation pour ce module peut être créée à Module:Quote/doc

--	Module:Quote (MW 1.43)
local p = {}

local mw			= mw
local tostring		= tostring
local type			= type
local tonumber		= tonumber
local table_insert	= table.insert
local table_concat	= table.concat
local t_trim		= mw.text.trim
local t_gsplit		= mw.text.gsplit

local V = (mw.ext and mw.ext.VariablesLua) or nil

local function vset(name, val)
	if V and type(V.vardefine) == "function" then
		pcall(V.vardefine, name, val ~= nil and tostring(val) or "")
	end
end

local function vget(name)
	if V and type(V.var) == "function" then
		local ok, res = pcall(V.var, name)
		if ok then
			return res
		end
	end
	return nil
end

local function escAttr(s)
	s = tostring(s or "")
	return s
		:gsub("&", "&")
		:gsub("<", "&lt;")
		:gsub(">", "&gt;")
		:gsub('"', "&quot;")
end

local function splitCSV(s)
	s = tostring(s or "")
	s = t_trim(s)
	if s == "" then
		return {}
	end

	local out = {}
	for part in t_gsplit(s, ",", true) do
		part = t_trim(part or "")
		if part ~= "" then
			table_insert(out, part)
		end
	end
	return out
end

local function appendCSVVar(varName, csvValue)
	csvValue = tostring(csvValue or "")
	csvValue = t_trim(csvValue)
	if csvValue == "" then
		return
	end

	local cur = tostring(vget(varName) or "")
	cur = t_trim(cur)

	if cur == "" then
		vset(varName, csvValue)
	else
		vset(varName, cur .. "," .. csvValue)
	end
end

local function renderWarnings(warningsCsv)
	warningsCsv = tostring(warningsCsv or "")
	warningsCsv = t_trim(warningsCsv)
	if warningsCsv == "" then
		return ""
	end

	local items = splitCSV(warningsCsv)
	if #items == 0 then
		return ""
	end

	local parts = {}
	for _, x in ipairs(items) do
		--	Laisse le parseur développer {{Bandeau ...}} (pas de preprocess Lua)
		table_insert(parts, "{{Bandeau " .. x .. "}}")
	end

	return "<div>" .. table_concat(parts, "&#32;") .. "</div>"
end

local function renderLinkOrText(url, text)
	url = tostring(url or "")
	url = t_trim(url)

	text = tostring(text or "")
	text = t_trim(text)

	if text == "" then
		return ""
	end

	if url ~= "" then
		return "[" .. url .. " " .. text .. "]"
	end

	return text
end

local function normalizeId(id)
	id = tostring(id or "")
	id = t_trim(id)
	id = id:match("(%d+)") or ""
	return id
end

function p._render(a)
	a = a or {}

	local id			= normalizeId(a.id)
	local quote			= tostring(a.quote or "")
	local authors		= tostring(a.authors or "")
	local article		= tostring(a.article or "")
	local work			= tostring(a.work or "")
	local volume		= tostring(a.volume or "")
	local number		= tostring(a.number or "")
	local page			= tostring(a.page or "")
	local location		= tostring(a.location or "")
	local publisher		= tostring(a.publisher or "")
	local place			= tostring(a.place or "")
	local date			= tostring(a.date or "")
	local link			= tostring(a.link or "")
	local warnings		= tostring(a.warnings or "")

	local nId = tonumber(id) or 0

	--	Enregistre le “max” vu
	if V and nId > 0 then
		vset("WD_QUOTES_MAX", id)
	end

	--	Accumulateurs (pour SMW batch dans Argument)
	do
		local as = t_trim(authors)
		if as ~= "" then
			appendCSVVar("WD_QUOTES_AUTHORS", as)
		end

		local ar = t_trim(article)
		if ar ~= "" then
			appendCSVVar("WD_QUOTES_ARTICLES", ar)
		end

		local wk = t_trim(work)
		if wk ~= "" then
			appendCSVVar("WD_QUOTES_WORKS", wk)
		end

		local pb = t_trim(publisher)
		if pb ~= "" then
			appendCSVVar("WD_QUOTES_PUBLISHERS", pb)
		end

		local pl = t_trim(place)
		if pl ~= "" then
			appendCSVVar("WD_QUOTES_PLACES", pl)
		end
	end

	--	Catégorie “sans auteur”
	local catNoAuthor = ""
	if t_trim(authors) == "" then
		catNoAuthor = "[[Catégorie: Citations sans auteur]]"
	end

	--	Blockquote (identique)
	local warnHtml = renderWarnings(warnings)

	local bq =
		'<blockquote data-wk-cite="' .. escAttr(id) .. '">'
		.. (warnHtml ~= "" and warnHtml or "")
		.. '« ' .. quote .. ' »'
		.. '</blockquote>'

	--	Référence (identique au comportement du template)
	local ref = {}
	table_insert(ref, '<div class="reference-citation" data-wk-cite="' .. escAttr(id) .. '">')

	if t_trim(authors) ~= "" then
		table_insert(ref, authors)
	else
		table_insert(ref, "Auteur non renseigné")
	end

	if t_trim(article) ~= "" then
		table_insert(ref, ', « ' .. renderLinkOrText(link, article) .. ' »')
	end

	if t_trim(work) ~= "" then
		if t_trim(article) ~= "" then
			table_insert(ref, ", ''" .. work .. "''")
		else
			local o = renderLinkOrText(link, work)
			table_insert(ref, ", ''" .. o .. "''")
		end
	end

	if t_trim(work) ~= "" and t_trim(number) ~= "" then
		if t_trim(volume) ~= "" then
			table_insert(ref, ", vol. " .. volume)
		end
		table_insert(ref, ", n°&#160;" .. number)
	end

	if t_trim(page) ~= "" then
		table_insert(ref, ", p." .. page)
	end

	if t_trim(location) ~= "" then
		table_insert(ref, ", " .. location)
	end

	if t_trim(publisher) ~= "" then
		table_insert(ref, ", " .. publisher)
	end

	if t_trim(place) ~= "" then
		table_insert(ref, ", " .. place)
	end

	if t_trim(date) ~= "" then
		table_insert(ref, ", " .. date)
	end

	table_insert(ref, ".</div>")

	local html = catNoAuthor .. bq .. "\n" .. table_concat(ref) .. "\n"

	--	Stocke les 4 premières (pour compilation dans Argument, sans scraping)
	if V and nId >= 1 and nId <= 4 then
		vset("WD_QUOTE_" .. id, html)
	end

	return html
end

function p.render(frame)
	local args = frame.args or {}
	return p._render(args)
end

return p