Module:Lengouistico

De Vouiquipèdia, l’enciclopèdia abada.

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

-- Fonccione ren qu’en arpetan. S’y at fôta, sè pôvont enspirar de [[wikidata:Module:Linguistic]] por apondre d’ôtres lengoues.

local p = {}
local lang = 'frp'
local langobj = mw.language.new(lang)

local vowels = 'aeiouyąăẵằẳặȃắâẫấầẩậãäǟāáàȁǎảẚåǻḁạǡæǣǽĕȇêễếềểệḙẽḛëēḕéḗèȅěẻẹęȩḝǝĭȋîĩḭïḯīíìȉǐỉịįıŏȏôỗốồổộõṏṍöōṑóṓòȍǒỏọǫǭơỡớờởợøǿŭȗûṷũṻṹṵüǖǘǜǚṳūúùȕǔủůụųưữứừửựŷỹÿȳýỳỷẙỵ'

-- i18n
local wordor = ' ou '
local wordand = ' et '
local comma = ', '
local fullstop = '. '
local wordsep = ' '

local function isin(str, pattern)
	if str and pattern and mw.ustring.find(str, pattern, 1, true ) then
		return true
	end
end


local function processgender(str)
	if (str == 'f') or (str == 'fem') or (str == 'feminine') then 
		return 'feminine'
	elseif (str == 'n') or (str == 'neutral') then 
		return 'neutral'
	else
		return 'masculine'
	end
end

local function processnumber(str)
	if (str == 'p') or (str == 'plural') then
		return 'plural'
	else 
		return 'singular'
	end
end

function p.vowelfirst (str)
	if str and #str > 0 then return isin(vowels, mw.ustring.lower(mw.ustring.sub(str, 1, 1))) end
end

function p.inparentheses(str, lang, space)
	if (not str) or str == '' then
		return str
	end
	str = '(' .. str .. ')'
	if not space then
		space = ' '
	end
	return space .. str
end

function p.of(word, gender, number, determiner, raw) 
	if not word then
		word = ''
	end
	word = mw.text.trim( word )
	if not raw then  --tèxto pas betâ en fôrma por fâre marchiér les èlisions
		raw = p.textoLim(word) or word
	end
	gender = processgender(gender)
	number = processnumber(number)
	local vowel = p.vowelfirst(raw)
	local feminine = (gender== 'feminine')
	-- raw is the string without the Wikiformatting so that it correctly analyses the string that is [[:fr:Italie|Italie]] -> 'italie'
	-- any way to automate this ?

	
	if number == 'plural' then
		return 'des ' .. word
		elseif feminine then
			return 'de les ' .. word
	elseif determiner and (determiner ~= '-') then-- de la, du // determiner ~= '-' vôt dére rensègnê coment vouedo
		if vowel then
			return 'de l’' .. word
		elseif feminine then
			return 'de la ' .. word
		else
			return 'du ' .. word
		end
	else
		if vowel then
			return 'd’' .. word
		else
			return 'de ' .. word
		end
	end
end

function p.noungroup(noun, adj)
	if not noun or noun == '' then 
		return nil -- not '' so that it is not counted as a string by mw.listToText
	end
	return noun .. wordsep(lang) .. adj -- quand o est en arpetan
end

function p.quickconj(args, conjtype)
	local separator, conjunction
	
	-- câs yô que separator ~= conj
	if type(conjtype) == 'function' then
		conjtype = conjtype()	
	end
	if (not conjtype) or conjtype == 'and' then
		separator, conjunction = comma, wordand
	elseif conjtype == 'or' then
		separator, conjunction = comma, wordor
	end
	if (separator and conjunction) then
		return  mw.text.listToText(args, separator, conjunction)
	end
	-- ôtros câs
	if conjtype == 'comma' then
		separator = comma
	elseif conjtype == 'new line' or conjtype == 'lowercase new line' then
		separator = '<br />'
		if conjtype == 'new line' then
			for i, j in pairs(args) do -- apond na granta lètra
				args[i] = p.ucfirst(j)
			end
		end
	else
		separator = conjtype
	end
	return table.concat(args, separator)
end

function p.conj(args, conjtype)
	if (not args) then
		return nil
	end
	local newargs = {}
	for i, j in pairs(args) do
		table.insert(newargs, j)
	end
	if #newargs == 0 then
		return nil
	end
	return p.quickconj(newargs, conjtype)
end
 
function p.conjfromWiki(frame)
	args = frame.args
	if not args or not args[1] then
		args = mw.getCurrentFrame():getParent().args
	end
	local conjtype = args.type
	newargs = {}  -- transform args metatable into a table so it can be concetenated
	for i, j in pairs(args) do
			if type(i) == 'number' then
				j = mw.text.trim(j)
				if j ~= '' then
					table.insert(newargs, j)
				end
			else 
				if i ~= 'type' and i ~= 'lang' then 
					return error('bad parameter in template:Conj:' .. i), '[[Category:Pages with incorrect template usage/Conj|A]]'
				end
			end
	end
	return p.conj(newargs, conjtype)
end

local function findcomplement(str, beginswith) -- retôrne lo nom principâl et lo complèment du nom ou ben nil et nil se falyita
	local particles = {" de la ", " de l'", " des ", " de les ", " de l’", " de ", " d’", " d'", " du "}
	if beginswith and (not mw.ustring.find(str, "^" .. beginswith)) then
		return nil
	end
	for i, pattern in pairs(particles) do
		local pos = mw.ustring.find(str, pattern)
		if pos then
			local main = mw.ustring.sub(str, 1, pos -1)
			local comp = mw.ustring.sub(str, pos + string.len(pattern))
			return main, comp
		end
	end
	return nil
end


function p.keepcomplement(str, beginswith) -- per ègzemplo "gâra de Liyon" -> "Liyon"
	local main, compl = findcomplement(str, beginswith)
	if compl then
		return compl
	end
	return str
end

function p.removecomplement(str, beginswith) -- per ègzemplo "gâra de Liyon" -> "gâra"
	local main, compl = findcomplement(str, beginswith)
	if main then
		return main
	end
	return str
end

--[=[
	textoLim lo lim de dedens iniciâl '^[[lim|tèxto]]' de str et retôrne : tèxto, lim
	Se lo lim est '[[tèxto]]', retôrne : tèxto, tèxto.
	Se str  comence pas per un lim entèrvouiqui, retôrne : nil
]=]
function p.textoLim( str )
	if type( str ) == 'string' then
		local lim, texto = str:match( '^%[%[ *([^%[%]|]*)|? *([^%[%]]*)%]%]' )
		if not lim then
			lim, texto = str:match( '^%b<>%[%[ *([^%[%]|]*)|? *([^%[%]]*)%]%]' )
		end
		if lim then
			local testlim = string.lower( lim )
			local fichier = string.match( testlim, '^fichiér:' ) 
				or  string.match( testlim, '^émâge:' )
				or  string.match( testlim, '^file:' )
			if not fichier then
				texto = ( texto ~= '' and texto ) or lim
				return texto, lim
			end
		end
	end
	return nil
end

function p.ucfirst(str)
	if (type (str ) ~= 'string') or (string == "") then
		return str
	end
	local strTemp, tag, tagTemp = str, ''
		-- sèpâre les balises html iniciâles (span ou ben ôtres)
	while strTemp:match( '^%b<>' ) do
		tagTemp, strTemp = strTemp:match( '^(%b<>)(.*)$' )
		tag = tag .. tagTemp
	end
	local texto = p.textoLim( strTemp )
	if texto then
		-- apond los crochèts de fin de lim por étre de sûr de remplaciér ren que lo tèxto du lim
		texto = texto .. ']]'
		-- èchape los caractèros magicos
		local pattern = texto:gsub( '([$%%()*+%-.?()^])', '%%%1' )
		-- apond la granta lètra u tèxto du lim
		str = str:gsub( pattern, p.ucfirst( texto ), 1 )
	else
		str = tag .. langobj:ucfirst( strTemp )
	end
	return str
end

function p.ucfirstE(frame)
	return p.ucfirst(frame.args[1])
end

function p.lcfirst(str)
	if (type (str ) ~= 'string') or (string == "") then
		return str
	end
	local strTemp, tag, tagTemp = str, ''
		-- sèpâre les balises html iniciâles (span ou ben ôtres)
	while strTemp:match( '^%b<>' ) do
		tagTemp, strTemp = strTemp:match( '^(%b<>)(.*)$' )
		tag = tag .. tagTemp
	end
	local texto = p.textoLim( strTemp )
	if texto then
		-- apond los crochèts de fin de lim por étre de sûr de remplaciér ren que lo tèxto du lim
		texto = texto .. ']]'
		-- èchape los caractèros magicos
		local pattern = texto:gsub( '([$%%()*+%-.?()^])', '%%%1' )
		-- apond la granta lètra u tèxto du lim
		str = str:gsub( pattern, p.lcfirst( texto ), 1 )
	else
		str = tag .. langobj:lcfirst( strTemp )
	end
	return str
end

function p.lcfirstE(frame)
	return p.lcfirst(frame.args[1])
end

--[[
function p.toascii(str)
	local convtable = mw.loadData("Module:Lengouistico/ASCII")
	for i, j in pairs(convtable) do -- mancont les grantes lètres
		str = mw.ustring.gsub(str, '[' .. i .. ']', j)
	end
	return str
end
]]-- 
return p