Modulo:Lingvonomo/provejo2

El Vikipedio, la libera enciklopedio
Salti al navigilo Salti al serĉilo

Dokumentado por ĉi tiu modulo povas esti kreata ĉe Modulo:Lingvonomo/provejo2/dokumentado

-- modul lingvonomo -- 2015-04-13
-- main functions:
-- language -- creates a link to a language article and an adverb like text
-- flag -- creates a flag and a link to a language article and an adverb like text
-- lang - creates a link to a language article and an adverb like text followed by a parsed {{Lang}} template. 
-- exist -- returs true, if a code is a language code, else false

-- the functions "language" and "flag" have one needed parameter only 
-- all functons have the optional parameters:
-- "koloro" (color), that defines the color of the text
-- "fono" (background), that defines the background color of the text
-- "grando" (size), that defines the size of the text
-- "pezo" (weight), that defines the font-weight

-- error handling:
-- factory
-- message
-- analyze
-- message2
-- analyze2

-- helping function:
-- getLanguages
-- getAccusative
-- getFlags
-- template
-- image
-- testweight

local messagePrefix = "lua-module-Lingvonomo-"
local l10nDef = {}
l10nDef[ "en" ] = {
    noLanguage    = "Error in language - in the function",
    emptyLanguage = "Language is empty - in the function",
    noText        = "Error in the text - in the function",
    emptyText     = "Text is empty - in the function", 
    noFlag        = "The page [[Modulo:Lingvonomo/listo/flagoj]] doesnt have a suitable flag."
}
l10nDef[ "de" ]  = {
    noLanguage    = "Fehler bei Sprache - in der Funktion",
    emptyLanguage = "Sprache ist leer - in der Funktion",
    noText        = "Fehler im Text - in der Funktion",
    emptyText     = "Text ist leer - in der Funktion",  
    noFlag        = "Auf der Seite [[Modulo:Lingvonomo/listo/flagoj]] gibt es keine geeignete Fahne."
}
l10nDef[ "eo" ]  = {
    noLanguage    = "Eraro en la lingvo - en la funkcio",
    emptyLanguage = "Lingvo estas malplena - en la funkcio",
    noText        = "Eraro en la teksto - en la funkcio",
    emptyText     = "Teksto estas malplena - en la funkcio",  
    noFlag        = "La paĝo [[Modulo:Lingvonomo/listo/flagoj]] ne enhavas taŭgan flagon."
}

-- error handling
local function factory( say )
    -- Retrieve localized message string in content language
    -- Precondition:
    --     say  -- string; message ID
    -- Postcondition:
    --     Return some message string
    -- Uses:
    --     >  messagePrefix
    --     >  l10nDef
    --     mw.language.getContentLanguage()
    --     mw.message.new()
    local c = mw.language.getContentLanguage():getCode()
    local m = mw.message.new( messagePrefix .. say )
    local r = false
    if m:isBlank() then
        local l10n = l10nDef[ c ]
        if not l10n then
            l10n = l10nDef[ "en" ]
        end
        r = l10n[ say ]
    else
        m:inLanguage( c )
        r = m:plain()
    end
    if not r then
        r = "(((".. say .. ")))"
    end
    return r
end -- factory()

-- for one needed parameter only 
local function message (first, f)
    -- first - the first required parameter in the main function is wrong
    -- f - in which function it occurs
     local r = ""
     if (first ~="") then     
         r = factory (first) .. ": " .. f .. "()"
     end     

    -- error (r, 0)
     r = "<span class='error'>" .. r .. "</span>"
     return r
end -- message ()
 
local function analyze (first, f)
    -- first - the first required parameter has to be analyzed
    -- f - in which function it occurs
    -- r - message text
        local r =""
        if (first ==nil) or (first =="") then
           local param1 =""
           local r =""
           if (first == nil) then
              param1= "noLanguage"
           end    
           if (first=="") then
              param1= "emptyLanguage"
           end            
       
           r=message (param1, f)
           return r
        end

    return r
end  -- analyze ()     

-- for two needed parameters 
local function message2 (first, second, f)
    -- first - the first required parameter in the main function is wrong
    -- second - the second required parameter in the main function is wrong
    -- f - in which function it occurs
     local r = ""
     if (first ~="") then     
         r = factory (first) .. ": " .. f .. "()"
     end     
     if (second ~="") then
         if (r ~="") then
         r = r .. "<br />" .. factory (second) .. ": " .. f .. "()"
         else r = factory (second) .. ": " .. f .. "()"
         end
     end     
    -- error (r, 0)
     r = "<span class='error'>" .. r .. "</span>"
     return r
end -- message2 ()

local function analyze2 (first, second, f)
    -- first - the first required parameter has to be analyzed
    -- second - the second required parameter has to be analyzed
    -- f - in which function it occurs
    -- r - message text
        local r =""
        if (first ==nil) or (first =="") or (second ==nil) or (second=="") then
           local param1 =""
           local param2 =""
           local r =""
           if (first == nil) then
              param1= "noLanguage"
           end    
           if (first=="") then
              param1= "emptyLanguage"
           end            
           if (second == nil)  then
              param2 ="noText"
           end  
           if (second=="") then
              param2 ="emptyText"
           end         
           r=message2 (param1, param2, f)
           return r
        end
        local lenfirst=mw.ustring.len(first)
        local lensecond=mw.ustring.len(second)
        -- if the first (language) is longer than the second (text), then it returns a text, which
        -- in the main function exchanges the values of the variables
        if (lenfirst>lensecond) then
            r="change"
            return r
        end
    return r
end  -- analyze2 ()  

-- gets the adverb and name according to a language code using mw.language.fetchLanguageName
local function getLanguages(s)
	-- Gets an adverb and name using the Lua function mw.language.fetchLanguageName.
	local r1, r2
	if s == "grc" then
    	r1, r2 = "en la antikva greka", "antikva greka lingvo"
    elseif s == "eo" then
    	r1, r2 =  "esperante", "esperanto"
    elseif s == "pap" then
    	r1, r2 = "papiamente", "papiamento"
    elseif s == "ast" then
    	r1, r2 = "asturie", "asturia lingvo"       	
    elseif s == "ceb" then
    	r1, r2 = "cebue", "cebuo" 
    elseif s == "ch" then
    	r1, r2 = "ĉamore", "ĉamora lingvo"  
    elseif s == "hsb" then
    	r1, r2 = "suprasorabe", "suprasoraba lingvo"    
    elseif s == "kg" then
    	r1, r2 = "konge", "konga lingvo"       	
    elseif s == "la" then
    	r1, r2 = "latine", "latina lingvo"     	
    elseif s == "tpi" then
    	r1, r2 = "tokpisine", "tokpisino"    
    elseif s == "udm" then
    	r1, r2 = "udmurte", "udmurta lingvo"        	
	else
    	r1 = mw.language.fetchLanguageName( s, "eo" )
    	if r1 then
    		r1 = r1:gsub( "a$", "e")
        	r2 = r1:gsub( "e$", "a lingvo" )
    	end
	end
	return r1, r2
end

-- gets the accusative and name according to a language code using mw.language.fetchLanguageName
local function getAccusative(s)
	-- Gets an accusative and name using the Lua function mw.language.fetchLanguageName.
	local r1, r2
	if s == "grc" then
    	r1, r2 = "antikvan grekan lingvon", "antikva greka lingvo"
    elseif s == "eo" then
    	r1, r2 =  "esperanton", "esperanto"
    elseif s == "pap" then
    	r1, r2 = "papiamenton", "papiamento" 
    elseif s == "ast" then
    	r1, r2 = "asturian lingvon", "asturia lingvo"     	
    elseif s == "ceb" then
    	r1, r2 = "cebuon", "cebuo" 
    elseif s == "ch" then
    	r1, r2 = "ĉamoran lingvon", "ĉamora lingvo" 
    elseif s == "hsb" then
    	r1, r2 = "suprasoraban lingvon", "suprasoraba lingvo" 
    elseif s == "kg" then
    	r1, r2 = "kongan lingvon", "konga lingvo"     	
    elseif s == "la" then
    	r1, r2 = "latinan lingvon", "latina lingvo"     	
    elseif s == "tpi" then
    	r1, r2 = "tokpisinon", "tokpisino"  
    elseif s == "udm" then
    	r1, r2 = "udmurtan lingvon", "udmurta lingvo"      	
	else
    	local r = mw.language.fetchLanguageName( s, "eo" )
    	if r then
    		r1 = r:gsub( "a$", "an lingvon")
        	r2 = r:gsub( "a$", "a lingvo" )
    	end
	end
	return r1, r2
end

-- gets the flag according to a language code from [[Modulo:Lingvonomo/listo/flagoj]]
local function getFlags(s)
	-- Gets an flag from the flag data page.
	local languageData = mw.loadData('Modulo:Lingvonomo/listo/flagoj')
	for language, aliases in pairs(languageData) do
		if (s==language) then
			local adverb = aliases[1]
			-- local name = aliases[2]
			return adverb, name
		end	
	end
end

-- template - parses the {{Lang}} template with two parameters: 
-- l - language code and t - text
local function template (l, t)
	local frame = mw.getCurrentFrame()		
	local r = frame:expandTemplate{
           title = 'Lang',
           args = {
       			l, 
       			t
       			}
           }
    return r		
end	

-- image tests, if a text could be the name of an image
local function image (t)
	local text = mw.ustring.lower(t)  -- it changes letters into lower case letters
	local ending = mw.ustring.sub(text,-4,-1)
	if (ending ==".gif") or (ending == ".jpg") or (ending == ".png") or (ending == ".svg") then
		local error = false
		return t, error	
	else
		local ending = mw.ustring.sub(text,-5,-1) 
		if (ending==".jpeg") then
			local error = false			
			return t, error
		else
			local r = message("noFlag","flag")
			local error = true
			return r, error
		end	
	end	
	local r = message("noFlag","flag")
	local error = true
	return r, error
end	

-- testweight analyzes if the given value in w is a valid value of the CSS feature font-weight
local function testweight (w)
	local text = mw.ustring.lower(w) .. ""
	if (text=="normal") or (text=="bold") or (text=="bolder") or (text=="lighter") 
	or (text=="100") or (text=="200") or (text=="300") or (text=="400") or (text=="500") 
	or (text=="600") or (text=="700") or (text=="800") or (text=="900") then
		return w
	else
		return "normal"
	end	
end	

-- Export
--local p = {}
-- main functions:
-- language - creates a link to a language article and an adverb like text
-- has only one needed parameter, the code of the language
-- it is used for the templates {{Ar}} until {{Zh}}
function _language (args) 

    code = args[1]	
    -- it analyzes, if the required parameter is wrong or not
    r=analyze (code, "language")
    if (r~="") then return r -- if r is some other than an empty string, it returns the message 
    -- and finishes the function
	end
	-- optional parameters
	color = args['koloro'] or args['color']
	background = args['fono'] or args['background']
	size = args['grando'] or args['size']
	weight = args['pezo'] or args['weight']
	accusative = args['akuzativo'] or args['accusative'] -- it is in this function only
	if (color ==nil) or (color == "") then
		c = "grey"
	else 
		c = color
	end	
	if (background~=nil) and (background~="") then
		b = "background-color:" .. background .. ";"
	else
		b = ""
	end	
	if (size~=nil) and (size~="") then
		s = "font-size:" .. size .. ";"
	else
		s = ""
	end	
	if (weight~=nil) and (weight~="") then
		w = "font-weight:" .. testweight(weight) .. ";"
	else
		w = ""
	end		
	
    code = mw.ustring.lower(code)
    
    -- if accusative has one of the following values, get the accusative. else the adverb
    if (accusative == "jes") or (accusative=="yes") then
    	text, name = getAccusative(code)
    else	
    	text, name = getLanguages(code) 
    end

	if (text ~= "") and (name ~="") then	
--	if (adverb~=nil) and (name ~=nil) then
		local linktext = text
	    r = "[[" .. name .. '|<span style="color:' .. c .. ";" .. b .. s .. w .. '">' .. linktext .. "</span>]]"
	else r = "<span class='error'>La lingva kodo '".. code .. "' ne estas subtenata de la Lua-funkcio 'mw.language.fetchLanguageName'. - en la funkcio: language()</span>"    
    end	 
    return r
end

-- flag - puts an image and creates a link to a language article and an adverb like text
-- has only one needed parameter, the code of the language
-- it is used for the templates {{Flag-ar}} until {{Flag-zh}}
function _flag (args) 

    code = args[1]	
    -- it analyzes, if the required parameter is wrong or not
    r=analyze (code, "flag")
    if (r~="") then return r -- if r is some other than an empty string, it returns the message 
    -- and finishes the function
	end
	-- optional parameter
	color = args['koloro'] or args['color']
	background = args['fono'] or args['background']
	size = args['grando'] or args['size']
	weight = args['pezo'] or args['weight']
	if (color ~=nil) and (color ~= "") then
		c = "color:" .. color .. ";"
	else
		c = ""
	end		
	if (background~=nil) and (background~="") then
		b = "background-color:" .. background .. ";"
	else
		b = ""
	end	
	if (size~=nil) and (size~="") then
		s = "font-size:" .. size .. ";"
	else
		s = ""
	end		
	if (weight~=nil) and (weight~="") then
		w = "font-weight:" .. testweight(weight) .. ";"
	else
		w = ""
	end		
	
    code = mw.ustring.lower(code)
    
    local adverb, name = getLanguages(code) 
    local flag = getFlags(code)
	
	if (adverb ~= "") and (name ~="") then	
--	if (adverb~=nil) and (name ~=nil) then
	    if (flag~=nil) then
	    	local i, error = image(flag) -- test if the text from the page [[Modulo:Lingvonomo/listo/flagoj]] is an image or not
	    	if (error == false) then
	    		r = "[[Dosiero:" .. flag .. "|20px|border]]&nbsp;"
	    	elseif (error == true) then
	    		r = i
	    	end
	    else
	    	r = message("noFlag","flag") -- '<span class="error">La paĝo [[Modulo:Lingvonomo/listo/flagoj]] ne enhavas taŭgan flagon.</span>'
	    end
	    local linktext = adverb
	    local l = name:gsub( " lingvo", "" )
	    r = r .. '<span style="cursor:help;font-family:monospace;font-weight:bold;font-size:small" title="Lingvo: ' .. l .. '">' 
	    if (c~="") or (b~="") or (s~="") or (w~="") then 
	    	r = r .. "[[" .. name .. '|<span style="' .. c .. b .. s .. w .. '">' .. linktext .. "</span>]]"
	    else
	    	r = r .. "[[" .. name .. '|' .. linktext .. "]]"	    	
	    end	
	    r = r .. "</span>"
	else r = "<span class='error'>La lingva kodo '".. code .. "' ne estas subtenata de la Lua-funkcio 'mw.language.fetchLanguageName'. - en la funkcio: flag()</span>"    
    end	 
    return r
end

-- lang - creates a link to a language article and an adverb like text
-- has two needed parameters, the code of the language and the text
function _lang (args) 

    code = args[1]	
    text = args[2]
    -- it analyzes, if the required parameters are wrong or not
    r=analyze2 (code, text, "lang")
    if (r~="") then return r -- if r is some other than an empty string, it returns the message 
    -- and finishes the function
	end
	-- optional parameter
	color = args['koloro'] or args['color']
	background = args['fono'] or args['background']	
	size = args['grando'] or args['size']
	weight = args['pezo'] or args['weight']	
	if (color ~=nil) and (color ~= "") then
		c = "color:" .. color .. ";"
	else
		c = ""
	end		
	if (background~=nil) and (background~="") then
		b = "background-color:" .. background .. ";"
	else
		b = ""
	end	
	if (size~=nil) and (size~="") then
		s = "font-size:" .. size .. ";"
	else
		s = ""
	end		
	if (weight~=nil) and (weight~="") and (s~="") then
		w = "font-weight:" .. testweight(weight) .. ";"
	elseif (weight==nil) or (weight=="") and (s~="") then
		w = "font-weight: normal;"		
	else
		w = ""
	end	
	-- html tags for the font-size, because this is used for both links
	if (s~="") or (w~="") then 
		link1 = '<b style="'
		link2 = '">'
		link3 = "</b>"
	else
		link1 = ""
		link2 = ""
		link3 = ""
	end	
	
    code = mw.ustring.lower(code)
    
    local adverb, name = getLanguages(code) 
	
	if (adverb ~= "") and (name ~="") then	
--	if (adverb~=nil) and (name ~=nil) then
		local linktext = adverb
		l = template (code, text) -- invoke the {{Lang}} template
		if (c~="") or (b~="") or (s~="") then
	    	r = link1 .. s .. w .. link2 .. "[[" .. name .. '|' .. linktext .. ']]:&nbsp;<span style="' .. c .. b .. '">' .. l .. "</span>" .. link3	
    	else
	    	r = "[[" .. name .. '|' .. linktext .. "]]:&nbsp;" .. l     	
		end	
	else r = "<span class='error'>La lingva kodo '".. code .. "' ne estas subtenata de la Lua-funkcio 'mw.language.fetchLanguageName'. - en la funkcio: lang()</span>"    
    end	 
    return r
end

-- exist analyzes if a code is a language code in mw.language.fetchLanguageName
function _exist (args) 

    code = args[1]	
    -- it analyzes, if the required parameter is wrong or not
    r=analyze (code, "exist")
    if (r~="") then return r -- if r is some other than an empty string, it returns the message 
    -- and finishes the function
	end
	
	local text, name = getLanguages(code)
	if (text~="") and (name~="") then
		return true
	else
		return false
	end	
    
end    

local function makeWrapper(funcName)
	-- Processes external arguments and sends them to the alia functions.
	return function (frame)
		-- If called via #invoke, use the args passed into the invoking
		-- template, or the args passed to #invoke if any exist. Otherwise
		-- assume args are being passed directly in from the debug console
		-- or from another Lua modulo.
		local origArgs
		if frame == mw.getCurrentFrame() then
			origArgs = frame:getParent().args
			for k, v in pairs(frame.args) do
				origArgs = frame.args
				break
			end
		else
			origArgs = frame
		end
		-- Trim whitespace and remove blank arguments.
		local args = {}
		for k, v in pairs(origArgs) do
			if type(v) == 'string' then
				v = mw.text.trim(v)
			end
			if v ~= '' then
				args[k] = v
			end
		end
		-- the parameters have to work not only using #invoke, but also in templates
		args.color = frame:getParent().args.koloro or args.koloro or args.color
		args.background = frame:getParent().args.fono or args.fono or args.background
		args.size = frame:getParent().args.grando or args.grando or args.size
		args.weight = frame:getParent().args.pezo or args.pezo or args.weight	
		args.accusative = frame:getParent().args.akuzativo or args.akuzativo or args.accusative		
		--return p[funcName](processLanguageArgs(args)) -- passes two tables to func: an array of language names, and a table of named arguments.
		return funcName( args )
	end
end
 
return {
    language = makeWrapper( _language ),
    flag = makeWrapper( _flag ),
    lang = makeWrapper( _lang ),
    exist = makeWrapper( _exist )
}