Módulo:contexto

De Wikcionario, el diccionario libre
Documentación del módulo
Esta documentación está transcluida desde Módulo:contexto/doc.
Los editores pueden experimentar en la zona de pruebas de este módulo.
Por favor, añade las categorías e interwikis a la subpágina de documentación. Subpáginas de este módulo.
local m_str = require("Módulo:String")

local ucfirst = m_str.ucfirst
local lcfirst = m_str.lcfirst
local cap = m_str.ucfirst
local minus = m_str.lcfirst
local strupper = m_str.upper
local strlower = m_str.lower
local strfind = m_str.find
local strmatch = m_str.match
local substr = m_str.sub

local insert = table.insert
local concat = table.concat

local obtener_idioma = require("Módulo:lenguas").cod_a_idioma
local sortkey = require("Módulo:sortkey").generarSortkey
local generar_error = require("Módulo:traza")

local export = {}

-- funciones nuevas
function export.ambito(frame)
	local t = {}
	local m = mw.loadData("Módulo:contexto/ámbito")
	local parent_frame = frame:getParent()
	local args = parent_frame.args
	
    local leng = args["leng"] or "es"
    local leng_upper = strupper(leng)
    local idioma_obj = obtener_idioma(leng)
    local ns = mw.title.getCurrentTitle().namespace
    local pagename = mw.title.getCurrentTitle().text
    
    local prohibido = {
        [','] = true,--'<span class="ib-comma"><span class="qualifier-comma">,</span></span>&nbsp;',
        ['o']= true,
        ['u']= true,
        ['y']= true,
        ['e']=	true,
        [" "] = true,
        ["_"] = true,
    }

    local espacios = {
    }
    
    local function f_cat(cat, clave)
        return "[[Categoría:"..leng_upper..":"..cat..(clave and "|"..clave.."]]" or "]]")
    end
    
    local cats = {}
    
    for i, arg in ipairs(args) do
    	if prohibido[arg] then
    		error(arg.. ": parámetro obsoleto")	
    	end
    	local etiqueta, tcat, nota = arg, nil, nil
    	local x = m[arg]
    	if x then
    		etiqueta = x[1]
    		tcat = x[2]
    		nota = x[3]
    		insert(cats, f_cat(etiqueta, sortkey(pagename, leng, idioma_obj)))
    	end
    	nota = args["nota"..tostring(i)] or nota
    	local st = etiqueta or arg
    	if nota then
    		if nota:len() > 20 then
    			st = st..frame:extensionTag("ref", nota)
    		else
    			st = st.." ("..nota..")"
    		end
    	end
    	insert(t, st)
    end
    return concat(t, ", ")..concat(cats)
end

function export.uso(frame)
	local t = {}
	local m = mw.loadData("Módulo:contexto/uso")
	local parent_frame = frame:getParent()
	local args = parent_frame.args
	
    local leng = args["leng"] or "es"
    local leng_upper = strupper(leng)
    local idioma_obj = obtener_idioma(leng)
    local ns = mw.title.getCurrentTitle().namespace
    local pagename = mw.title.getCurrentTitle().text
    
    local prohibido = {
        [','] = true,--'<span class="ib-comma"><span class="qualifier-comma">,</span></span>&nbsp;',
        ['o']= true,
        ['u']= true,
        ['y']= true,
        ['e']=	true,
        [" "] = true,
        ["_"] = true,
    }

    local espacios = {
    }
    
    local function f_cat(cat, clave)
        return "[[Categoría:"..leng_upper..":"..cat..(clave and "|"..clave.."]]" or "]]")
    end
    
    local cats = {}
    
    for i, arg in ipairs(args) do
    	if prohibido[arg] then
    		error(arg.. ": parámetro obsoleto")	
    	end
    	arg = strlower(arg)
    	local etiqueta, ucat, nota = arg, nil, nil
    	local x = m[arg]
    	if x then
    		etiqueta = x[1]
    		ucat = x[2]
    		nota = x[3]
    		if ucat and ucat ~= "" then
    			insert(cats, f_cat(ucat, sortkey(pagename, leng, idioma_obj)))
    		end
    	end
		nota = args["nota"..tostring(i)] or nota
    	local st = etiqueta or arg
    	if nota then
    		if nota:len() > 20 then
    			st = st..frame:extensionTag("ref", nota)
    		else
    			st = st.." ("..nota..")"
    		end
    	end
    	insert(t, st)
    end
    return concat(t, ", ")..concat(cats)
end

function export.csem(frame)
	local t = {}
	local m = mw.loadData("Módulo:contexto/csem")
	local parent_frame = frame:getParent()
	local args = parent_frame.args
	
    local leng = args["leng"] or "es"
    local leng_upper = strupper(leng)
    local idioma_obj = obtener_idioma(leng)
    local ns = mw.title.getCurrentTitle().namespace
    local pagename = mw.title.getCurrentTitle().text
    
    local prohibido = {
        [','] = true,--'<span class="ib-comma"><span class="qualifier-comma">,</span></span>&nbsp;',
        ['o']= true,
        ['u']= true,
        ['y']= true,
        ['e']=	true,
        [" "] = true,
        ["_"] = true,
    }

    local espacios = {
    }
    
    local function f_cat(cat, clave)
        return "[[Categoría:"..leng_upper..":"..cat..(clave and "|"..clave.."]]" or "]]")
    end
    
    local cats = {}
    
    for i, arg in ipairs(args) do
    	if prohibido[arg] then
    		error(arg.. ": parámetro obsoleto")	
    	end
    	if arg == "csem" then
			generar_error("csem")    		
    	end
    	arg = ucfirst(arg)
    	local etiqueta, scat, nota = arg, nil, nil
    	local x = m[arg]
    	if x then
    		etiqueta = x[1]
    		scat = x[2]
    		nota = x[3]
    		if scat and scat ~= "" then
    			insert(cats, f_cat(scat, sortkey(pagename, leng, idioma_obj)))
    		end
    	end
		nota = args["nota"..tostring(i)] or nota
    	local st = etiqueta or arg
    	insert(t, i > 1 and lcfirst(st) or st)
    end
    return concat(t, ", ")..concat(cats)
end

-- función vieja
function export.mostrar(frame)
	
	local t = {}

    local params = {
        ["nota"] = {lista = true},
        ["leng"] = {},
        ["etiqueta"] = {},
        ["catuso"] = {},
        ["catgram"] = {},
        ["catreg"] = {},
        ["catsem"] = {},
        ["cat"] = {},
        ["clave"] = {},
    }

    local parent_frame = frame:getParent()
	local args = require("Módulo:parámetros").obtener_parametros(parent_frame.args, params)

    local N = #args
    local leng = args["leng"] or "es"
    local leng_upper = strupper(leng)
    local ns = mw.title.getCurrentTitle().namespace
    local pagename = mw.title.getCurrentTitle().text

    local i = 0 --índice cero: refiere al título mismo. Así al llamar a {{electricidad|electrónica}} se procesa electricidad (indice 0) y luego electrónica (índice 1)
    local j = 0
    local poner_coma = false

    local especiales = {
        [','] = ',&nbsp;',--'<span class="ib-comma"><span class="qualifier-comma">,</span></span>&nbsp;',
        ['o']='&nbsp;o&nbsp;',
        ['u']='&nbsp;u&nbsp;',
        ['y']='&nbsp;y&nbsp;',
        ['e']='&nbsp;e&nbsp;',
    }

    local espacios = {
        [" "] = true,
        ["_"] = true,
    }

    local function f_cat(cat, clave)
        return "[[Categoría:"..leng_upper..":"..cat..(clave and "|"..clave.." "..pagename.."]]" or "]]")
    end

    local function iteracion(etiqueta, catuso, catgram, catreg, catsem, cat, clave)
	    local x = especiales[args[i]]
	    
	    if x or espacios[args[i]] then
	    	generar_error("especiales")
	    end
	    
    	if poner_coma and not x then
        	insert(t, ',&nbsp;')
    	end
    
    	if etiqueta then
        	if i == 0 then --si es el comienzo, convierto a mayúscula
	        	insert(t, cap(etiqueta))
	        elseif not catreg then --si no hay categoría regional, asumo que va en minúscula la primera
	        	insert(t, minus(etiqueta))
	        else --sino, lo dejo como está
	        	insert(t, etiqueta)
	        end
	        if args["nota"][j] then
            	insert(t, "&nbsp;("..args["nota"][j]..")")
        	end
			poner_coma = true
    	else
	        if x then
	            insert(t, x)
	            poner_coma = false
	            i = i + 1
	            return
	        end
	
	        if espacios[args[i]] then
	            i = i + 1
	            return
	        end
	        
	        if args[i] then
		        insert(t, args[i])
		        if args["nota"][j] then
	            	insert(t, "&nbsp;("..args["nota"][j]..")")
	        	end
				poner_coma = true
	        end
    	end
    	
    	i = i + 1
		j = j + 1
    	
        if ns == 0 then
            if catuso then
                insert(t, f_cat(catuso, clave))
            end
            if catgram then
                insert(t, f_cat(catgram, clave))
            end
            if catreg then
                insert(t, f_cat(catreg, clave))
            end
            if catsem then
                insert(t, f_cat(catsem, clave))
            end
            if cat then
                insert(t, f_cat(cat, clave))
            end
        end
    end

    iteracion(args["etiqueta"], args["catuso"], args["catgram"], args["catreg"], args["catsem"], args["cat"], args["clave"]) --la primera iteración es obligatoria porque pueden no haber parámetros posicionales

    while i <= N do
        local etiqueta, catuso, catgram, catreg, catsem, cat, clave
        local title = args[i]
        if not especiales[title] and not espacios[title] then
	        while true do
	        	local p = mw.title.new(title, 10)
		        if p and p.exists then
		            local s = p:getContent()
		            if strfind(s, "#REDIRECCIÓN") or strfind(s, "#REDIRECCION") or strfind(s, "#REDIRECT") then
		            	title = strmatch(s, "%[%[Plantilla:[a-zA-ZÀ-ž ]+%]%]")
		            	if title then
		            		title = substr(title, 13, -3) -- xq -3 y no -2??
		            	else
		            		break
		            	end
		            else
			            if strfind(s, "{{contexto") then
			                local etiqueta_ = {strmatch(s, "|%s*etiqueta%s*=%s*([a-zA-ZÀ-ž][a-zA-ZÀ-ž ]-)%s*[|}]")}
			                local catuso_ = {strmatch(s, "|%s*catuso%s*=%s*([a-zA-ZÀ-ž][a-zA-ZÀ-ž ]-)%s*[|}]")}
			                local catgram_ = {strmatch(s, "|%s*catgram%s*=%s*([a-zA-ZÀ-ž][a-zA-ZÀ-ž ]-)%s*[|}]")}
			                local catreg_ = {strmatch(s, "|%s*catreg%s*=%s*([a-zA-ZÀ-ž][a-zA-ZÀ-ž ]-)%s*[|}]")}
			                local catsem_ = {strmatch(s, "|%s*catsem%s*=%s*([a-zA-ZÀ-ž][a-zA-ZÀ-ž ]-)%s*[|}]")}
			                local cat_ = {strmatch(s, "|%s*cat%s*=%s*([a-zA-ZÀ-ž][a-zA-ZÀ-ž ]-)%s*[|}]")}
			                local clave_ = {strmatch(s, "|%s*clave%s*=%s*([a-zA-ZÀ-ž][a-zA-ZÀ-ž ]-)%s*[|}]")}
			                
			                etiqueta = etiqueta_[1]
			                catuso = catuso_[1]
			                catgram = catgram_[1]
			                catreg = catreg_[1]
			                catsem = catsem_[1]
			                cat = cat_[1]
			                clave = clave_[1]
			            end
		            	break
		            end
		        else
		        	break
		        end
	        end
        end
        iteracion(etiqueta, catuso, catgram, catreg, catsem, cat, clave)
    end

    if ns == 10 then
        if args["catuso"] then
            insert(t, "[[Categoría:Plantillas de marcas de uso]]")
        end
        if args["catgram"] then
            insert(t, "[[Categoría:Plantillas de categorías gramaticales]]")
        end
        if args["catreg"] then
            insert(t, "[[Categoría:Plantillas de ámbito regional]]")
        end
        if args["catsem"] then
            insert(t, "[[Categoría:Plantillas de campo semántico]]")
        end
        if args["cat"] then
            insert(t, "[[Categoría:Plantillas de categorías sin especificar]]")
        end
    end

    return concat(t)
end

return export