Modulo:Coordinates2

Dokumentado Dokumentado


Ŝablona programado Diskutoj Lua Testoj Subpaĝoj
Modulo Esperanto English

Modulo: Dokumentado


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.)
-- Lua-module for {{Koordinato}} - 2013-07-29
-- 
local p = {}  
local Coor = require('Modulo:Coordinates')
-- local args 
local marco
local formates = {
    "DMS",
    "DM",
    "D",
    "DEC",
    "CH1903",
    "UTM",
}

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 = ""
    -- 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}}]]"
	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(type)
    -- analyzes the value of type
    local errors = {}
    if type ~= "" and type ~= nil then
       if type == "country" or type == "state" or type == "adm1st" or type == "adm2nd" 	
       or type == "city" or type == "isle" or type == "airport" or type == "mountain"
       or type == "waterbody" or type == "forest" or type == "landmark" or type == "event"
       or type == "example" then type = "type:" .. type  
           return type, errors  --correct value in type
       else if ns0 == true then table.insert(errors, {"testtype", errormessage(3,type) }) end  --wrong value in type
           type = ""
           return type, errors
       end    
     else if ns0 == true then table.insert(errors, {"testtype", errormessage(3,"") }) end --empty value in type
          type = ""
     end  
     return type, 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 p.coordinates(frame)
-- Creates the coordinates given in the parametres latd, lat, longd ... with the help of Modulo:Coordenas. 
-- 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 globe = mw.ustring.lower(args['globe'] or "")
    local type = args['type'] or ""
    local typetest = args['type'] or ""    
    local dim = 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 
    -- 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>'
        d = "title"
        ft = "dms"
    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"
        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 type
        type, errors=testtype(type)
        -- test for dim
        dim, errors = testdim(dim)
        
        -- 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
                 type = type .. '(' .. 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
                    type = type .. '(' .. s .. ')'
                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 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
        globe = "globe:" .. globe 
        type = ""
        dim = ""
        region = ""
    -- if there is a value in Esperanto, it has to be changed into English one    
    elseif globe == "marso" then
        globe = "globe:mars"
        type = ""
        dim = ""
        region = ""
    elseif globe == "luno" then
        globe = "globe:moon"
        type = ""
        dim = ""
        region = ""
    elseif globe == "venuso" then
        globe = "globe:venus"
        type = ""
        dim = ""
        region = ""
    elseif globe == "merkurio" then
        globe = "globe:mercury"
        type = ""
        dim = ""
        region = ""        
    else -- if globe isn't empty or not "earth" and has an invalid value
        globe = "" 
        type = ""    
        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 .. type .. globe, format=f, format2=f2, format3=f3, format4=f4, formatitle=ft, formatitle2=ft2, word=w, name=n, category=cat, sortkey=k, 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 .. type .. globe, format=f, format2=f2, format3=f3, format4=f4, formatitle=ft, formatitle2=ft2, word=w, name=n, category=cat, sortkey=k, gf=frame--gf=marco
                  }
        return  prefix .. Coor.coord2(co) .. suffix .. errorPrinter(errors)
    elseif args['coord'] then -- If you give by coord, it returns word-for-word the value
        return prefix2 .. args['coord'] .. suffix2
    elseif args['koordinatoj'] then -- If you give by coord, it returns word-for-word the value
        return prefix2 .. args['koordinatoj'] .. suffix2
    elseif args['Koordinatoj'] then -- If you give by coord, it returns word-for-word the value
        return prefix2 .. args['Koordinatoj'] .. suffix2        
    elseif args['latitudo'] and args['longitudo'] then
        return
            '&bull;&nbsp;[[Latitudo]]: ' .. args['latitudo'] ..'<br>&bull;&nbsp;[[Longitudo]]: ' .. args['longitudo']
    end
end

return p