Module:Titled coords: Difference between revisions

Content deleted Content added
fixed pattern matching on seconds to allow tenths and hundreds (copied from /sandbox)
v1.2:Calls Module:Coordinates directly instead of via {{Coord}} (updated from sandbox)
 
Line 1:
-- *** Version 1.12 (AlphaBeta) ***
local p = {}
 
-- Module entry point
function p.main(frame)
function p.build(rawCoords, title, options)
local raw_coords = frame.args[1]
local title has_seconds = frame.args[2] or ""true
local options output = frame.args[3] or ""
 
localif has_secondsnot title then title = 1"" end
if not options then options = "" end
local output = "", coords, sidx, eidx
local lat_d, lat_m, lat_s, lat_NS, lon_d, lon_m, lon_s, lon_WE
 
-- ** Display the raw coordinates obtained from {{wikidata}} **
if string.find(options,"raw") ~= nil then
output = output .. "raw_coordsrawCoords:" .. raw_coordsrawCoords
end
 
-- ** Extract the individual fields from the raw coordinates **
-- ** See https://www.lua.org/manual/5.3/manual.html#6.4.1 **
local sidx, _, lat_d, lat_m, lat_s, lat_NS, lon_d, lon_m, lon_s, lon_WE =
string.find(raw_coordsrawCoords, "(%d+)/(%d+)/(%d+%.?%d*)/(%a)/(%d+)/(%d+)/(%d+%.?%d*)/(%a)")
 
if sidx == nil then -- extract without seconds
local eidx
sidx, eidx, lat_d, lat_m, lat_NS, lon_d, lon_m, lon_WE =
string.find(raw_coordsrawCoords, "(%d+)/(%d+)/(%a)/(%d+)/(%d+)/(%a)")
if sidx == nil then
return output .. "<br/>Error: Failed to process raw_coordsrawCoords: " .. raw_coordsrawCoords
end
if string.find(options,"idx") ~= nil then
Line 31:
return output
end
has_seconds = 0false
end
 
if string.find(options,"debug") ~= nil then
if has_seconds == 1 then
output = output .. "<br/>raw_coordsrawCoords:" .. raw_coordsrawCoords .. "<br/>lat dms = " .. lat_d .. "|" .. lat_m .. "|" .. lat_s .. "|" .. lat_NS ..
"<br/>lon dms = " .. lon_d .. "|" .. lon_m .. "|" .. lon_s .. "|" .. lon_WE .. "\n"
else
output = output .. "<br/>raw_coordsrawCoords:" .. raw_coordsrawCoords .. "<br/>lat dm = " .. lat_d .. "|" .. lat_m .. "|" .. lat_NS ..
"<br/>lon dms = " .. lon_d .. "|" .. lon_m .. "|" .. lon_WE .. "\n"
end
end
Line 47:
local enc_title = title:gsub(" ", "+")
 
-- ** Call {{coord}}Coordinates passing individual fields and setting the title **
local coord = require('Module:Coordinates')._coord
local type_title = "type:mountain&title=" .. enc_title
local coordsTxt -- declared here or lua strict complains
if has_seconds == 1 then
coords = frame:expandTemplate{title='coord', args= { lat_d, lat_m, lat_s, lat_NS, lon_d, lon_m, lon_s, lon_WE, type_title}}
local coordsTxt = coord{lat_d, lat_m, lat_s, lat_NS, lon_d, lon_m, lon_s, lon_WE, type_title}
else
coordscoordsTxt = frame:expandTemplate{title='coord', args= { lat_d, lat_m, lat_NS, lon_d, lon_m, lon_WE, type_title}}
-- ** Doesn't like me adding these args **
--"type:mountain","format=dms","name=" .. name}}
Line 58 ⟶ 60:
 
if output ~= "" then
return output .. "<br/>" .. coordscoordsTxt
end
 
return coordscoordsTxt
end
 
-- Template entry point
function p.main(frame)
local raw_coords = frame.args[1]
local title = frame.args[2] or ""
local options = frame.args[3] or ""
 
return p.build(raw_coords, title, options)
end
 
return p
 
-- LUA debug console tests
-- print(p.build("50/27/40/N/122/17/42/W","test title",""))
-- print(p.build("50/27/N/122/17/W","title no seconds",""))
 
--[[ Tried to invoke {{wikidata}} directly but not working: