Difference between revisions of "Module:Geobox coor"
From ERPC Wiki Archive
(return nothing when nothing is passed in) |
m (1 revision imported) |
||
(2 intermediate revisions by 2 users not shown) | |||
Line 2: | Line 2: | ||
local geobox_coor = {} | local geobox_coor = {} | ||
+ | local coord_mod = require("Module:Coordinates") | ||
local function isnotempty(s) | local function isnotempty(s) | ||
Line 14: | Line 15: | ||
wrappers = 'Template:Geobox coor' | wrappers = 'Template:Geobox coor' | ||
}) | }) | ||
− | return geobox_coor[funcName](args | + | return geobox_coor[funcName](args) |
end | end | ||
− | |||
− | |||
− | |||
− | |||
end | end | ||
geobox_coor.coord = makeInvokeFunc('_coord') | geobox_coor.coord = makeInvokeFunc('_coord') | ||
− | function geobox_coor._coord(args | + | function geobox_coor._coord(args) |
local tstr = args['9'] or 'type:other' | local tstr = args['9'] or 'type:other' | ||
local dstr = isnotempty(args['title']) and 'inline,title' or 'inline' | local dstr = isnotempty(args['title']) and 'inline,title' or 'inline' | ||
Line 30: | Line 27: | ||
local prefix = args['prefix'] or '' | local prefix = args['prefix'] or '' | ||
local suffix = args['suffix'] or '' | local suffix = args['suffix'] or '' | ||
+ | local cname = args['name'] | ||
local innerprefix = args['innerprefix'] or '' | local innerprefix = args['innerprefix'] or '' | ||
local innersuffix = args['innersuffix'] or '' | local innersuffix = args['innersuffix'] or '' | ||
Line 36: | Line 34: | ||
if( isnotempty(args[3]) ) then | if( isnotempty(args[3]) ) then | ||
− | cstr = | + | cstr = coord_mod._coord( |
{args[1], args[2], args[3], lat_dir, args[5], args[6], args[7], lon_dir, | {args[1], args[2], args[3], lat_dir, args[5], args[6], args[7], lon_dir, | ||
− | tstr, format = args['format'] or 'dms', display = dstr} | + | tstr, format = args['format'] or 'dms', name = cname, display = dstr} |
) | ) | ||
elseif( isnotempty(args[2]) ) then | elseif( isnotempty(args[2]) ) then | ||
− | cstr = | + | cstr = coord_mod._coord( |
{args[1], args[2], lat_dir, args[5], args[6], lon_dir, | {args[1], args[2], lat_dir, args[5], args[6], lon_dir, | ||
− | tstr, format = args['format'] or 'dms', display = dstr} | + | tstr, format = args['format'] or 'dms', name = cname, display = dstr} |
− | + | ) | |
elseif( isnotempty(args[4]) ) then | elseif( isnotempty(args[4]) ) then | ||
− | cstr = | + | cstr = coord_mod._coord( |
{args[1], lat_dir, args[5], lon_dir, | {args[1], lat_dir, args[5], lon_dir, | ||
− | tstr, format = args['format'] or 'dec', display = dstr} | + | tstr, format = args['format'] or 'dec', name = cname, display = dstr} |
− | + | ) | |
elseif( isnotempty(args[1]) ) then | elseif( isnotempty(args[1]) ) then | ||
− | cstr = | + | cstr = coord_mod._coord( |
{args[1], args[5], | {args[1], args[5], | ||
− | tstr, format = args['format'] or 'dec', display = dstr} | + | tstr, format = args['format'] or 'dec', name = cname, display = dstr} |
− | + | ) | |
elseif( isnotempty(args['wikidata']) and mw.wikibase.getEntityObject()) then | elseif( isnotempty(args['wikidata']) and mw.wikibase.getEntityObject()) then | ||
local entity = mw.wikibase.getEntityObject() | local entity = mw.wikibase.getEntityObject() | ||
Line 68: | Line 66: | ||
longitude= math_mod._round(longitude,precision) | longitude= math_mod._round(longitude,precision) | ||
end | end | ||
− | cstr = | + | cstr = coord_mod._coord( |
− | {latitude, longitude, tstr, format = args['format'] or 'dms', display = dstr} | + | {latitude, longitude, tstr, format = args['format'] or 'dms', name = cname, display = dstr} |
) | ) | ||
end | end | ||
else | else | ||
− | return '' | + | return '' |
end | end | ||
Latest revision as of 17:28, 27 February 2017
Documentation for this module may be created at Module:Geobox coor/doc
-- this module implements [[template:geobox coor]] local geobox_coor = {} local coord_mod = require("Module:Coordinates") local function isnotempty(s) return s and s:match( '^%s*(.-)%s*$' ) ~= '' end -- Wrapper function to grab args (from Module:Coordinates), -- see Module:Arguments for this function's documentation. local function makeInvokeFunc(funcName) return function (frame) local args = require('Module:Arguments').getArgs(frame, { wrappers = 'Template:Geobox coor' }) return geobox_coor[funcName](args) end end geobox_coor.coord = makeInvokeFunc('_coord') function geobox_coor._coord(args) local tstr = args['9'] or 'type:other' local dstr = isnotempty(args['title']) and 'inline,title' or 'inline' local lat_dir = isnotempty(args[4]) and args[4] or 'N' local lon_dir = isnotempty(args[8]) and args[8] or 'E' local prefix = args['prefix'] or '' local suffix = args['suffix'] or '' local cname = args['name'] local innerprefix = args['innerprefix'] or '' local innersuffix = args['innersuffix'] or '' local cstr = '' if( isnotempty(args[3]) ) then cstr = coord_mod._coord( {args[1], args[2], args[3], lat_dir, args[5], args[6], args[7], lon_dir, tstr, format = args['format'] or 'dms', name = cname, display = dstr} ) elseif( isnotempty(args[2]) ) then cstr = coord_mod._coord( {args[1], args[2], lat_dir, args[5], args[6], lon_dir, tstr, format = args['format'] or 'dms', name = cname, display = dstr} ) elseif( isnotempty(args[4]) ) then cstr = coord_mod._coord( {args[1], lat_dir, args[5], lon_dir, tstr, format = args['format'] or 'dec', name = cname, display = dstr} ) elseif( isnotempty(args[1]) ) then cstr = coord_mod._coord( {args[1], args[5], tstr, format = args['format'] or 'dec', name = cname, display = dstr} ) elseif( isnotempty(args['wikidata']) and mw.wikibase.getEntityObject()) then local entity = mw.wikibase.getEntityObject() if(entity and entity.claims and entity.claims.P625 and entity.claims.P625[1].mainsnak.snaktype == 'value') then local math_mod = require("Module:Math") local precision = entity.claims.P625[1].mainsnak.datavalue.value.precision local latitude = entity.claims.P625[1].mainsnak.datavalue.value.latitude local longitude = entity.claims.P625[1].mainsnak.datavalue.value.longitude if precision then precision=-math_mod._round(math.log(precision)/math.log(10),0) latitude = math_mod._round(latitude,precision) longitude= math_mod._round(longitude,precision) end cstr = coord_mod._coord( {latitude, longitude, tstr, format = args['format'] or 'dms', name = cname, display = dstr} ) end else return '' end if(isnotempty(args['wrap'])) then return prefix .. innerprefix .. cstr .. innersuffix .. suffix else return prefix .. '<span style="white-space:nowrap">' .. innerprefix .. cstr .. innersuffix .. '</span>' .. suffix end end return geobox_coor