Module:frp-IPA/ref

From Wiktionary, the free dictionary
Jump to navigation Jump to search


local export = {}

local references = { "AIS", "ALF", "ALLy", "ALP", "ALV", "APV", "DFF", "DGL", "DLG", 
	"DPB", "DSV", "LPT", "MHN", "PVA", "SSV", "TPh", "VFC", "VIV" }
export.references = references

local VIV_code = {
	["A"] = "Abruzzo",
	["B"] = "Basilicata",
	["K"] = "Calabria",
	["C"] = "Campania",
	["E"] = "Emilia-Romagna",
	["F"] = "Friuli-Venezia Giulia",
	["R"] = "Lazio",
	["3"] = "Liguria",
	["L"] = "Lombardy",
	["8"] = "Molise",
	["P"] = "Piedmont",
	["7"] = "Apulia",
	["2"] = "Sardinia",
	["1"] = "Sicily",
	["9"] = "Tuscany",
	["6"] = "Trentino-South Tyrol",
	["5"] = "Umbria",
	["4"] = "Aosta",
	["V"] = "Veneto",
}
export.VIV_code = VIV_code

export.ref_data = {
AIS = {
	wikilink = "WT:AFRP#AIS",
	map = 1,
},
ALF = {
	wikilink = "WT:AFRP#ALF_2",
	map = 1,
},
ALLy = {
	wikilink = "WT:AFRP#ALLy_2",
	map = 1,
},
ALP = {
	wikilink = "WT:AFRP#ALP",
},
ALV = {
	wikilink = "WT:AFRP#ALV",
	url_format = "https://alaval.unine.ch/atlas?carte={2}&statement_id={3}"
},
APV = {
	wikilink = "WT:AFRP#APV",
	map = 1,
},
DFF = {
	wikilink = "WT:AFRP#DFF",
	url_format = "https://dicofranpro.llm.umontreal.ca/?chercher={1}",
	quotes = 1,
},
DGL = {
	wikilink = "WT:AFRP#DGL",
	quotes = 1,
},
DLG = {
	wikilink = "WT:AFRP#DLG",
	url_format = "https://www.rose.uzh.ch/docling/charte.php?c={2}&o=Ordre&t={3}"
},
DPB = {
	wikilink = "WT:AFRP#DPB",
	quotes = 1,
},
DSV = {
	wikilink = "WT:AFRP#DSV",
	quotes = 1,
},
LPT = {
	wikilink = "WT:AFRP#LPT",
	quotes = 1,
},
MHN = {
	wikilink = "WT:AFRP#MHN",
},
PhL = {
	wikilink = "WT:AFRP#PhL",
},
PVA = {
	wikilink = "WT:AFRP#PVA",
	url_format = "https://www.patoisvda.org/moteur-de-recherche/{1}_{2}",
	quotes = 1,
},
SSV = {
	wikilink = "WT:AFRP#SSV",
},
TPh = {
	wikilink = "WT:AFRP#TPh_2",
},
VFC = {
	wikilink = "WT:AFRP#VFC",
	quotes = 1,
},
VIV = {
	wikilink = "WT:AFRP#VIV",
	text_format = "{0} ({2})",
	quotes = 1,
	preprocess = function(args)
		args[2] = args[2]:gsub("=","/"):gsub("m%w%d%d%d", function(code)
			return "[https://www2.hu-berlin.de/vivaldi/index.php?id=" .. code
				.. "&lang=it " .. VIV_code[code:sub(2,2)] .. "]"
		end)
	end
},
}

function export.format(text, args, field, mode)
	local used = {}
	text = text:gsub("{(%d)}", function(idx)
		idx = tonumber(idx)
		used[idx] = true
		if not args[idx] then
			error("Missing positional argument number " .. idx .. " for source " .. field)
		end
		return mode == "url" and mw.uri.encode(args[idx], "PATH") or args[idx]
	end)
	
	for i=2,#args do
		if not used[i] then
			error("Unused positional argument number " .. i .. " for source " .. field)
		end
	end
	
	return text
end

-- process each source according to the format specified in export.ref_data
-- within <content>, each item is separated by ","
-- in each item, the arguments are separated by "/"
-- in each argument, each sub-item is seprated by "="
function export.process(name, content)
	local data = export.ref_data[name]
	if not data then error("Unrecognised source: " .. name) end
	
	-- process the name according to data.link and data.wikilink
	if data.link then
		name = "[" .. data.link .. " " .. name .. "]"
	elseif data.wikilink then
		name = "[[" .. data.wikilink .. "|" .. name .. "]]"
	end
	
	-- prepend "map" if data.map is specified
	local items = mw.text.split(content, ",", true)
	local prepend = ""
	if data.map then
		prepend = "map" .. (#items > 1 and "s " or " ")
	end
	
	for i=1,#items do
		if data.url_format or data.quotes or data.text_format then
			local args = mw.text.split(items[i], "/", true)
			
			args[0] = data.quotes and ('“' .. args[1] .. '”') or args[1]
			
			if data.preprocess then
				data.preprocess(args)
			end
			
			if data.url_format then
				items[i] = "[" .. export.format(data.url_format, args, name, "url") .. " " .. args[0] .. "]"
			elseif data.text_format then
				items[i] = export.format(data.text_format, args, name)
			else
				if args[2] then
					error("Source " .. name .. " should not have more than one argument.")
				end
				items[i] = args[0]
			end
		end
	end
	
	return name .. ": " .. prepend .. table.concat(items, ", ")
end

-- each source is denoted as "<name>:<content>" and separated by ";"
function export.format_ref(text, filter)
	if not text then return nil end
	local result = ""
	local seen = {}
	for source in mw.text.gsplit(text, ";", true) do
		name, content = source:match("^([%l%u]+):(.+)$")
		if not name then error("Wrong format: " .. source) end
		if seen[name] then error("Duplicated source: " .. name) end
		seen[name] = 1
		if (not filter) or filter[name] then
			result = result .. "* " .. export.process(name, content) .. "\n"
		end
	end
	if filter then
		for _, ref in ipairs(references) do
			if filter[ref] and not seen[ref] then
				error("Reference used but not specified: " .. ref)
			end
		end
	end
	return result
end

function export.make_ref(sources, cf, note, filter)
	local formatted_sources = export.format_ref(sources, filter)
	return (sources and "\nSources:\n" .. formatted_sources or "")
		.. (cf and "\nCf. also:\n" .. export.format_ref(cf) or "")
		.. (note and mw.getCurrentFrame():preprocess(note) or "")
end

return export