Vés al contingut

Mòdul:exemples

De Viccionari
Icona de documentació de mòdul Documentació del mòdul[mostra] [modifica] [refresca]

A continuació es mostra la documentació transclosa de la subpàgina /ús. [salta a la caixa de codi]


Mòdul per generar exemples i citacions de les accepcions. És usat per les plantilles {{ex-us}} i {{ex-cit}}.

local p = {}

local m_languages = require("Module:llengua")

-- microformat2 classes, see https://phabricator.wikimedia.org/T138709
local class = {
	container_ux = 'h-usage-example',
	container_quotation = 'h-quotation',
	example = 'e-example',
	quotation = 'e-quotation',
	quotation_with_citation = 'e-quotation cited-passage',
	translation = 'e-translation',
	transliteration = 'e-transliteration',	
	transcription = 'e-transcription',
	literally = 'e-literally',
	source = 'e-source',
	footer = 'e-footer'
}

-- helper functions

local function wrap(tag, class, text, langcode)
	if langcode then
		langcode = ' lang="' .. langcode .. '"'
	else
		langcode = ""
	end
	
	if text and class then
		return table.concat{'<', tag, ' class="', class, '"', langcode, '>', text, '</', tag, '>'}
	else
		return nil
	end
end

local function span(class, text) return wrap('span', class, text) end
local function div(class, text) return wrap('div', class, text) end

local function format_usex(data)
	local namespace = mw.title.getCurrentTitle().nsText
	
	local lang, sc, usex, translation, transliteration, transcription, noenum, inline,
		ref, quote, lit, substs, qualifiers, source, nocat, brackets, footer, sortkey =
			data.lang, data.sc, data.usex, data.translation, data.transliteration,
			data.transcription, data.noenum, data.inline, data.ref, data.quote,
			data.lit, data.substs, data.qualifiers, data.source, data.nocat,
			data.brackets, data.footer, data.sortkey
	
	--[[
	if lang:getType() == "reconstructed" or namespace == "Reconstruction" then
		error("Reconstructed languages and reconstructed terms cannot have usage examples, as we have no record of their use.")
	end
	]]
	
	if lit then
		lit = "(literalment, «" .. span(class.literally, lit) .. "»)"
	end
	
	if source then
		source = "(" .. span(class.source, source) .. ")"
	end
	
	if footer then
		footer = span(class.footer, footer)
	end
	
	local example_type = quote and "citació" or "exemple d'ús" -- used in error messages
	local categories = {}
	
	if not sc then -- really?
		sc = m_languages.script(lang.code)
	end
	
	-- tr=- means omit transliteration altogether
	if transliteration == "-" then
		transliteration = nil
	elseif sc ~= 'Latn' then
		-- Try to auto-transliterate
		if not transliteration and usex then
			local subbed_usex = require("Module:enllaç").remove_links(usex)
			
			if substs then
				local substs = mw.text.split(substs, ",")
				for _, subpair in ipairs(substs) do
					local subsplit = mw.text.split(subpair, mw.ustring.find(subpair, "//") and "//" or "/")
					subbed_usex = mw.ustring.gsub(subbed_usex, subsplit[1], subsplit[2])
				end
			end
			
			transliteration = m_languages.trans(lang.code, subbed_usex)
		end
		
		-- If there is still no transliteration, then add a cleanup category
		if not transliteration then
			transliteration = "(afegiu la transliteració)"
		end
	end
	if transliteration then
		transliteration = span(class.transliteration, transliteration)
	end
	if transcription then
		transcription = span(class.transcription, transcription)
	end
	
	if translation == "-" then
		translation = nil
	elseif translation then
		translation = span(class.translation, translation)
	elseif lang.code ~= "ca" and lang.code ~= "und" then
		-- add trreq category if translation is unspecified and language is not english or undetermined
		table.insert(categories, "Traduccions demanades en " .. lang.name)
		translation = "<small>(afegiu la traducció en català)</small>"
	end
	
	if usex then
		if usex:find("[[", 1, true) then
			usex = require("Module:enllaç").language_link({term = usex, lang = lang}, false)
		end
		
		local face
		if quote then
			usex = "«" .. usex .. "»"
			face = nil
		else
			face = "terme"
		end
		
		usex = require("Module:enllaç").tag_text(usex, lang.code, sc ~= 'Latn' and sc or '', face)
		usex = span(quote == "quote-meta" and class.quotation_with_citation or
			quote and class.quotation or class.example, usex)
	else
		-- TODO: Trigger some kind of error here
		usex = "<small>(afegiu el text en l'escriptura original)</small>"
	end
	
	local result = {}
	
	if m_languages.dir(lang.code) == "rtl" then -- TODO: add function getDirection(lang.sc)
		usex = "&rlm;" .. usex .. "&lrm;"
	end
	
	table.insert(result, usex)
	
	if qualifiers then
		table.insert(result, " (" .. span("qualifier-content", qualifiers) .. ")")
	end
	
	if ref then
		-- tracking ref tags
		local pattern_ref = string.format( "%c'%c`UNIQ%s%sref%s%s%sQINU`%c'%c",
                                    127, 34, "%-", "%-", "%-", "%x+",
                                    "%-", 34, 127 )
		if mw.ustring.find(ref, pattern_ref) then
			require('Mòdul:utilitats').track("ex-cit/ref")
		end
		
		ref = ' <span style="font-size:90%">(' .. ref .. ')</span>'
		--if string.sub(ref, 1, 1) ~= "(" and string.sub(ref, 1, 1) ~= "<" and not string.find(ref, "ref") then
		--	ref = '(' .. ref .. ')'
		--end
		--ref = ' ' .. ref
		--if string.sub(ref, 3, 1) ~= "{" and string.sub(ref, 2, 1) ~= "<" then
		--	ref = '<span style="font-size:90%">' .. ref .. '</span>'
		--end
	end
	table.insert(result, ref)
	
	if inline then
		if transliteration then
			table.insert(result, " ― " .. transliteration)
			if transcription then
				table.insert(result, " /" .. transcription .. "/")
			end
		elseif transcription then
			table.insert(result, " ― /" .. transcription .. "/")
		end
		
		if translation then
			table.insert(result, " ― " .. translation)
		end
		
		if lit then
			table.insert(result, " " .. lit)
		end
		
		if source then
			table.insert(result, " " .. source)
		end
		
		if footer then
			table.insert(result, " " .. footer)
		end
		
		if brackets then
			table.insert(result, "]")
		end
	elseif transliteration or translation or transcription or lit or source or footer then
		table.insert(result, "<dl>")
		local closing_tag = ""
		
		if transliteration then
			table.insert(result, closing_tag)
			table.insert(result, "<dd>" .. transliteration)
			closing_tag = "</dd>"
		end
		
		if transcription then
			table.insert(result, closing_tag)
			table.insert(result, "<dd>/" .. transcription .. "/")
			closing_tag = "</dd>"
		end
		
		if translation then
			table.insert(result, closing_tag)
			table.insert(result, "<dd>" .. translation)
			closing_tag = "</dd>"
		end
		
		if lit then
			table.insert(result, closing_tag)
			table.insert(result, "<dd>" .. lit)
			closing_tag = "</dd>"
		end
		
		local extra_indent, closing_extra_indent
		if transliteration or transcription or translation or lit then
			extra_indent = "<dd><dl><dd>"
			closing_extra_indent = "</dd></dl></dd>"
		else
			extra_indent = "<dd>"
			closing_extra_indent = "</dd>"
		end
		if source then
			table.insert(result, closing_tag)
			table.insert(result, extra_indent .. source)
			closing_tag = closing_extra_indent
		end
		
		if footer then
			table.insert(result, closing_tag)
			table.insert(result, extra_indent .. footer)
			closing_tag = closing_extra_indent
		end
		
		if brackets then
			table.insert(result, "]")
		end
		
		table.insert(result, closing_tag)
		
		table.insert(result, "</dl>")
	else
		if brackets then
			table.insert(result, "]")
		end
	end
	
	result = table.concat(result)
	result = div(quote and class.container_quotation or class.container_ux, result)
	result = result .. require("Module:utilitats").format_categories(categories, lang, sortkey)
	if noenum then
		result = "\n: " .. result
	end
	return result
end

-- from en:Module:usex/templates
function p.usex_t(frame)
	local args = frame:getParent().args
	args[3] = args[3] or args.t or args.trad or args.translation
	args.inline = args.inline and true or false
	args.noenum = args.noenum and true or false
	args.tr = args.tr or args.trans or args.transliteration
	args.ts = args.ts or args.transcription
	args.nocat = args.nocat and true or false
	args.brackets = args.brackets and true or false
	
	local quote = (frame.args["quote"] or "") ~= ""
	local template_inline = (frame.args["inline"] or "") ~= ""
	local lang = m_languages.getByCode(args[1] or "und")
	
	local data = {
		lang = lang,
		sc = args.sc or lang.sc,
		usex = args[2] and mw.text.trim(args[2]),
		translation = args[3] and mw.text.trim(args[3]),
		transliteration = args["tr"],
		transcription = args["ts"],
		noenum = args["noenum"],
		inline = args["inline"] or template_inline,
		ref = frame.args.ref or args.ref,
		quote = quote,
		lit = args["lit"],
		substs = args["subst"],
		qualifiers = args["q"],
		source = args["font"],
		footer = args["peu"],
		nocat = args["nocat"],
		brackets = args["claudators"],
		sortkey = args["ordre"],
	}
	
	return format_usex(data)
end

return p