Modulo:Provejo

El Vikipedio, la libera enciklopedio
Jump to navigation Jump to search
Dokumentado Dokumentado

Enhavo

Provejo

Ĉi tie povas esti farataj kodaj testoj aŭ aliaj provoj.


Se vi havas demandon pri ĉi tiu Lua-modulo, tiam vi povas demandi en la diskutejo pri Lua-moduloj. La Intervikiaj ligiloj estu metataj al Vikidatumoj. (Vidu Helpopaĝon pri tio.)
-- Ĉi tie okazu malgrandaj provoj de Lua
--[[
functions:
deg2slash - converts coordinates separated with spaces into coordinates separated with /

]]

-- Lua-module for {{Koordinato}} - 2014-01-30
-- 
local p = {}  
local Coor = require('Modulo:Provejo2')
-- local args 
local marco
-- some tables for analyzes of valide values
-- valide values for the parameters "text" and "article"
local formates = {
    "DMS",
    "DM",
    "D",
    "DEC",
    "CH1903",
    "UTM",
}
-- valide values for the parameter "type"
local types = {
    "country", 
    "state",
    "adm1st",
    "adm2nd", 
    "city",
    "isle",
    "airport",
    "mountain",
    "waterbody",
    "forest",
    "landmark",
    "event",
    "example",
}
-- valide values for the parameter "sortkey"
local sortkeys = {
    "NS",
    "EW",
}
-- valide values for the parameter "map"
local mappositions = {
    "left",
    "right",
    "center",
}
-- valide values for the parameter "maplevel"
local maplevels = {
    "global", 
    "continental", 
    "national", 
    "adm1st", 
    "adm2nd",
}

function IsFormat (format)
    local f = mw.ustring.gsub(format, '^[ ]*(.-)[ ]*$', '%1');  -- trim
    f = mw.ustring.upper(f) -- capitalize 
    for i,j in pairs(formates) do
        if f == j then
            return true
        end
    end
    return false
end    

function wd(property,link,separator)
--local wd = {}
   -- wd.args = {
        --property = propiedad,
       -- separator = separador,
       -- link = enlace
    --}
local dato = marco:expandTemplate{
            title = 'Property',
            args = {property,'',separator,link=link}
                     }
    --local dato = Wikidata.formatStatements(wd)   
    if dato ~= '' then
 
        return dato --Wikidata.formatStatements(wd)
    end
end

function errormessage(number,sub)
    local text = ""
    local cat = ""
    local hide = ""
    local pagename = page_name
    -- messages partly from {{CoordinateMSG}}
    if number == 1 then text ='Gradnumera eraro:' if sub == "NS" then text = text .. '[[Ŝablono:Koordinato#NS kaj EW|NS]]:' else text = text .. '[[Ŝablono:Koordinato#NS kaj EW|EW]]' end 
        cat = "[[Kategorio:Parametra eraro|1" .. pagename .. "]]"
    elseif number == 2 then text ='ne in [[Ŝablono:Coordinate to CH1903#Wertebereich|aro]] de CH1903'
        cat = "[[Kategorio:Parametra eraro|2" .. pagename .. "]]"
    elseif number == 3 then if sub ~= "" and sub ~= nil then text ='malĝusta valoro ›'.. sub .. '‹'
        else text ='neniu valoro' end text = text .. ' en [[Ŝablono:Koordinato#type|type]]-parametro'
        cat =	"[[Kategorio:Parametra eraro|3" .. pagename .. "]]"
	elseif number == 4 then text = "Ne estas numero" if sub ~= "" and sub ~= nil then text = text .. 'en' .. sub end
        cat = "[[Kategorio:Parametra eraro|4" .. pagename .. "]]"
    elseif number == 5 then text ='Nenomita parametro 1:' --{{{2}}}{{#if:{{{3|}}}|, '''2''':{{{3|}}}}}{{#if:{{{4|}}}|, '''3''':{{{4|}}}}}{{#if:{{{5|}}}|, '''4''':{{{5|}}}}} {{#if:{{{6|}}}|usw.}} </span>'
        cat = "[[Kategorio:Parametra eraro|5" .. pagename .. "]]"
	elseif number == 6 then text = ""
        hide = '<span style="display:none">[[Ŝablono:Koordinato/zorgado/region|6]]</span>'
        cat = "[[Kategorio:Parametra eraro|6" .. pagename .. "]]" --region not defined
	elseif number == 7 then if sub ~= "" and sub ~= nil then text = 'Informo pri alteco malĝusta aŭ pli ol du postkomaj ciferoj' end
        cat = "[[Kategorio:Parametra eraro|7" .. pagename .. "]]"
		hide = '<span style="display:none">[[Ŝablono:Coordinate/zorgado/elevation|7]]</span>' --elevation not defined
	elseif number == 8 then text ='Neniu [[Ŝablono:Koordinato#name|name]]-parametro en fluteksta koordinato'
        cat = "[[Kategorio:Parametra eraro|8" .. pagename .. "]]" --name not defined
	elseif number == 9 then text ='Nombro de loĝantoj en [[Ŝablono:Koordinato#pop kaj elevation|pop]]-parametro malĝustas'
        cat = "[[Kategorio:Parametra eraro|9" .. pagename .. "]]"
	elseif number == 10 then text ='La valoro de la parametro map malĝustas:' if sub ~= "" and sub ~= nil then text = text .. ' ' .. sub end
        cat = "[[Kategorio:Parametra eraro|10" .. pagename .. "]]"        
	else text ='Koordinata eraro'  -- default message
        cat = "[[Kategorio:Parametra eraro|!" .. pagename .. "]]"
	end
    text = text .. hide .. cat
    return text
end    

function ns0 ()
    -- analyzes the namespace and returns true, if it is an article, else false
    currentTitle = mw.title.getCurrentTitle()
    local ns   = currentTitle.namespace
    if ns == 0 then return true
    else return false    
    end    
end     

function testtype(type1)
    -- analyzes the value of type1
    local errors = {}
    if type1 ~= "" and type1 ~= nil then
       local t = mw.ustring.gsub(type1, '^[ ]*(.-)[ ]*$', '%1');  -- trim
       for i,j in pairs(types) do
          if t == j then
          	type1 = "type:" .. type1  
            return type1, errors  --correct value in type1
          end
       end 	
       if ns0 == true then table.insert(errors, {"testtype", errormessage(3,type1) }) end  
          --wrong value in type1
          type1 = ""
          return type1, errors
    else if ns0 == true then table.insert(errors, {"testtype", errormessage(3,"") }) end 
    	  --empty value in type1
          type1 = ""
    end  
    return type1, errors
end    

function testdim(dim)
    -- analyzes, if dim is a number
     local errors = {}
     local n 
     if dim ~= "" and dim ~= nil then
         n = tonumber (dim)
         if n == nil then -- if it isn't a number, then put an empty string 
             if ns0 == true then table.insert(errors, {"testdim", errormessage(4,dim) }) end
             dim = ""             
         else dim = "dim:" .. dim .. "_"    
         end
     else dim = ""     
     end
     return dim, errors
end    

function testmap(map)
    -- analyzes the value of map
    local errors = {}
    if map ~= "" and map ~= nil then
       local m = mw.ustring.gsub(map, '^[ ]*(.-)[ ]*$', '%1');  -- trim
       for i,j in pairs(mappositions) do
          if m == j then
            return map, errors  --correct value in map
          end
       end 	
       if ns0 == true then table.insert(errors, {"testmap", errormessage(10,map) }) end  
          --wrong value in type1
          map = "right"
          return map, errors
    end  
    return map, errors
end 

function p.coordinates(frame)
-- Creates the coordinates given in the parametres latd, lat, longd ... with the help of Modulo:Coordinates. 
-- Compatible with formates: deg (degrees with decimales without position), d (degrees with position), 
-- dm (degrees, minutes with position) and dms (degrees, minutes and secondes with position)    
    globalFrame = frame
    local args = frame.args
    local prefix = ""
    local suffix = ""
    local d = ""  -- display
    local f = ""  -- format
    local ft = "" -- formattitle
    local ft2 = "" -- second format for title
    local text = args['text']
    local article = args['article']
    local map = args['map']
    local globe = mw.ustring.lower(args['globe'] or "")
    local type1 = args['type'] or ""
    local typetest = args['type'] or ""    
    local dim = args['dim']
    local dimtest = args['dim']
    local region = args['region']
    local pop = args['pop']
    local elevation = args['elevation']
    local n = args['name']
    local note = args['notes'] or ""
    local errors = {}
    local w = "" -- the link text like a word, but not the link target    
    currentTitle = mw.title.getCurrentTitle()
    local ns   = currentTitle.namespace
    local prefix2 = '[[Geografia koordinata sistemo|Koordinatoj]]: <span style="white-space:nowrap">'
    local suffix2 = '</span>'
    local cat = args['category'] -- if one wishes to output a category only
    local k = args['sortkey'] 
    local co = {} 
    -- some tests and analyses 
    map, errors = testmap(map)
    -- 1. variant: text isn't empty, but article is empty
    if text ~= "" and text ~= nil and (article == "" or article == nil) then
        text = text:match( '^%s*(.-)%s*$' );  --remove whitespace        
        d = "inline"
        if text == "/" then f = "dms"
        elseif text:match("/") then 
            local explode=mw.text.split( text:match( '^%s*(.-)%s*$' ), '/' )
            if (#explode > 0) then
              f = explode[1]
            end    
            if(#explode>1) then 
               f2 = explode[2]
            end
            if(#explode>2) then 
               f3 = explode[3]
            end
            if(#explode>3) then 
               f4 = explode[4]
            end            
        elseif IsFormat(text) == true then f = text    
        else 
            f = "dms"
            w = w .. text 
        end
    -- 2. variant: text and article aren't empty    
    elseif text ~= "" and text ~= nil and article ~= "" and article ~= nil then
        text = text:match( '^%s*(.-)%s*$' );  --remove whitespace  
        article = article:match( '^%s*(.-)%s*$' );  --remove whitespace  
        d = "inline,title"
        if text == "/" then f = "dms"
        elseif text:match("/") then 
            local explode=mw.text.split( text:match( '^%s*(.-)%s*$' ), '/' )
            if (#explode > 0) then
              f = explode[1]
            end    
            if(#explode>1) then 
               f2 = explode[2]
            end
            if(#explode>2) then 
               f3 = explode[3]
            end
            if(#explode>3) then 
               f4 = explode[4]
            end              
        elseif IsFormat(text) == true then f = text
        else
            f = "dms"
            w = w .. text
        end
        if article == "/" then ft = "dms"
        elseif article == "CH1903" or article == "CH1903/DMS" or article =="DMS/CH1903" then
            ft = "DMS"
            ft2 = "CH1903"
        elseif article == "DEC" or article == "DEC/DMS" or article =="DMS/DEC" then
            ft = "DMS"
            ft2 = "DEC"            
        elseif IsFormat(article) == true then ft = article
        else ft = "dms"
        end    
    -- 2. variant: text is empty, but article isn't empty    
    elseif (text == "" or text == nil) and article ~= "" and article ~= nil then
        article = article:match( '^%s*(.-)%s*$' );  --remove whitespace  
        d = "title"
        if article == "/" then ft = "dms"
        elseif article == "CH1903" or article == "CH1903/DMS" or article =="DMS/CH1903" then
            ft = "DMS"
            ft2 = "CH1903"            
        elseif article == "DEC" or article == "DEC/DMS" or article =="DMS/DEC" then
            ft = "DMS"
            ft2 = "DEC"                 
        elseif IsFormat(article) == true then ft = article
        else ft = "dms"    
        end    
    else
        -- prefix = '[[Geografia koordinata sistemo|Koordinatoj]]: <span style="white-space:nowrap">'
        -- suffix = '</span>'
        if map ~= "" and map ~= nil then
          d = "map"	
          f = "dec"	
        else	
          d = "title"
          ft = "dms"
        end
    end    

    -- test if coordinates are defined
    if (args['NS'] == "" or args['NS'] == nil) and (args['EW'] == "" or args['EW'] == nil) then
      --  args['NS'] = "0/N"
      --  args['EW'] = "0/E"
        if (args['latd'] ~= "" and args['latd'] ~= nil and args['longd'] ~= "" and args['longd'] ~= nil) or
          (args['lat'] ~= "" and args['lat'] ~= nil and args['long'] ~= "" and args['long'] ~= nil) then
      -- d = "inline"
        elseif (args['coord'] ~= "" and args['coord'] ~= nil) or (args['koordinatoj'] ~= "" and args['koordinatoj'] ~= nil) 
    	or (args['Koordinatoj'] ~= "" and args['Koordinatoj'] ~= nil) then
      -- d = "inline"
        else    d="project"
        end    
        prefix = ""
        suffix = ""
    elseif args['NS'] ~= "" and args['NS']  ~= nil and (args['EW'] == "" or args['EW'] == nil) then
        table.insert(errors, {"coordinates", errormessage(1,"EW") }) 
        if string.find (args['NS'], '/') ~= nil then args['EW'] = "0/E"
        else args['EW'] = "0"
        end    
    elseif args['EW'] ~= "" and args['EW']  ~= nil and (args['NS'] == "" or args['NS'] == nil) then
        table.insert(errors, {"coordinates", errormessage(1,"NS") })  
        if string.find (args['EW'], '/') ~= nil then args['NS'] = "0/N"
        else args['NS'] = "0"    
        end    
    end
    -- if globe is empty, not defined or has the value "earth", then define the following variables
    if globe == "" or globe == nil or globe == "earth" then
        -- test for type1
        type1, errors=testtype(type1)
        if type1 ~= "" and type1 ~= nil then
        	maptype = typetest
        end
        -- test for dim
        dim, errors = testdim(dim)
        if dim ~= "" and dim ~= nil then
            mapdim = dimtest
        end
        
        -- pop has to be defined only, if type has one of the following values
        if typetest == "country" or typetest == "state" or typetest == "adm1st" 
           or typetest == "adm2nd" or typetest == "city" or typetest == "isle" then
            if pop ~= "" and pop ~= nil then
               if pop:match( "^[%s]*[0-9]+[%s]*$" ) then
                 type1 = type1 .. '(' .. pop .. ')'
                 mappop = pop
               else 
                 if ns0 == true then table.insert(errors, {"testpop", errormessage(9,pop) }) end
               end   
            else 
              if ns0 == true then table.insert(errors, {"testpop", errormessage(9,"") }) end
            end
        -- elevation has to be defined only, if type has the following value
        elseif typetest == "mountain" then
            if elevation ~= "" and elevation ~= nil then
                local s = elevation
                local cMin = mw.ustring.char(8722)   -- Unicode-Minus
                local scan = "^[%s]*([%-" .. cMin .. "]?)[0-9]+([,%.])[0-9][0-9][%s]*$"
                local sign, sep = mw.ustring.match(s, scan)
                if sep then
                  if sep == "," then
                    s = mw.ustring.gsub(s, ",", ".", 1)
                  end
                  if sign == cMin then
                    s = mw.ustring.gsub(s, cMin, "-", 1)
                  end
                    type1 = type1 .. '(' .. s .. ')'
                    mapelevation = elevation
                else 
                    if ns0 == true then table.insert(errors, {"testelevation", errormessage(7,elevation) }) end
                end
            else 
               if ns0 == true then table.insert(errors, {"testelevation", errormessage(7,"") }) end
            end
        end

        -- if region is defined, it creates a string
        if region ~= "" and region ~= nil then 
        	mapregion = region         	
        	region = "region:" .. region .. "_"
        else 
        	region = ""    
        end  
    -- if globe isn't empty or not "earth" and has a valid value
elseif globe == "mars" or globe =="moon" or globe == "venus" or globe == "mercury" then
        mapglobe = globe	
        globe = "globe:" .. globe 
        type1 = ""
        dim = ""
        region = ""
    -- if there is a value in Esperanto, it has to be changed into English one    
    elseif globe == "marso" then
        globe = "globe:mars"
        mapglobe = "mars"
        type1 = ""
        dim = ""
        region = ""
    elseif globe == "luno" then
        globe = "globe:moon"
        mapglobe = "moon"
        type1 = ""
        dim = ""
        region = ""
    elseif globe == "venuso" then
        globe = "globe:venus"
        mapglobe = "venus"
        type1 = ""
        dim = ""
        region = ""
    elseif globe == "merkurio" then
        globe = "globe:mercury"
        mapglobe = "mercury"
        type1 = ""
        dim = ""
        region = ""        
    else -- if globe isn't empty or not "earth" and has an invalid value
        globe = "" 
        type1 = ""    
        dim = ""
        region = ""
    end
    -- if name isn't defined, it put {{PAGENAME}} into the variable
    if n == "coordinates" or n == "koordinatoj" then n = ""
    elseif n == "" or n == nil then 
         if ns0 == true and text ~= "" and text ~= nil then table.insert(errors, {"coordinates", errormessage(8,"") }) end
         n = currentTitle.title --"{{PAGENAME}}"
    end      
    
  --  if wd('latitudo') then
  --   co.args = { 
  --   wd('latitudo'), wd('longitudo'),
  --  display='inline,title','type:' .. type,format="dms", gf=marco
  --          }
  --    return  prefix .. Coor.coord2(co) .. suffix
 
    --elseif args['latd'] or args['lat'] then
    if args['latd'] or args['lat'] then
        co.args = { 
            args['latd'] or args['lat'], args['latm'] or args['long'], 
            args['lats'], args['latNS'],args['longd'], args['longm'], 
            args['longs'], args['longEW'], notes=note,
            display=d, dim .. region .. type1 .. globe, format=f, format2=f2, format3=f3, 
            format4=f4, formatitle=ft, formatitle2=ft2, word=w, name=n, category=cat, 
            sortkey=k, mapr=mapregion, mapt=maptype, mapd=mapdim, mapg=mapglobe, mapp=mappop,
            mape=mapelevation, map=map, gf=frame --gf=marco
                  }
        return  prefix .. Coor.coord2(co) .. suffix .. errorPrinter(errors)
    elseif args['NS'] then
        co.args = { 
            args['NS'], args['EW'], notes=note,
            display=d, dim .. region .. type1 .. globe, format=f, format2=f2, format3=f3, 
            format4=f4, formatitle=ft, formatitle2=ft2, word=w, name=n, category=cat, 
            sortkey=k, mapr=mapregion, mapt=maptype, mapd=mapdim, mapg=mapglobe, mapp=mappop,
            mape=mapelevation, map=map, gf=frame --gf=marco
                  }
        return  prefix .. Coor.coord2(co) .. suffix .. errorPrinter(errors)
    elseif args['coord'] then 
    	co.args = {
		    args['coord'], notes=note,
		    display=d, dim .. region .. type1 .. globe, format=f, format2=f2, format3=f3,
		    format4=f4, formatitle=ft, formatitle2=ft2, word=w, name=n, category=cat,
		    sortkey=k, mapr=mapregion, mapt=maptype, mapd=mapdim, mapg=mapglobe, mapp=mappop,
            mape=mapelevation, map=map, gr=frame 
		           }
        return prefix .. Coor.coord2(co) .. suffix .. errorPrinter(errors)
    elseif args['koordinatoj'] then 
    	    	co.args = {
		    args['koordinatoj'], notes=note,
		    display=d, dim .. region .. type1 .. globe, format=f, format2=f2, format3=f3,
		    format4=f4, formatitle=ft, formatitle2=ft2, word=w, name=n, category=cat,
		    sortkey=k, mapr=mapregion, mapt=maptype, mapd=mapdim, mapg=mapglobe, mapp=mappop,
            mape=mapelevation, map=map, gr=frame 
		           }
        return prefix .. Coor.coord2(co) .. suffix .. errorPrinter(errors)
    elseif args['Koordinatoj'] then 
    	co.args = {
		    args['Koordinatoj'], notes=note,
		    display=d, dim .. region .. type1 .. globe, format=f, format2=f2, format4=f3,
		    format4=f4, formatitle=ft, formatitle2=ft2, word=w, name=n, category=cat,
		    sortkey=k, mapr=mapregion, mapt=maptype, mapd=mapdim, mapg=mapglobe, mapp=mappop,
            mape=mapelevation, map=map, gr=frame 
		           }    	
        return prefix .. Coor.coord2(co) .. suffix .. errorPrinter(errors)        
    elseif args['latitudo'] and args['longitudo'] then
        return
            '&bull;&nbsp;[[Latitudo]]: ' .. args['latitudo'] ..'<br>&bull;&nbsp;[[Longitudo]]: ' .. args['longitudo']
    end
end

return p