Module:Location map: Difference between revisions

Jump to navigation Jump to search
m (1 revision imported)
(Copy from sandbox per request on talk)
Line 1: Line 1:
require('Module:No globals')
require('strict')


local p = {}
local p = {}
Line 103: Line 103:
-- Finds a parameter in a transclusion of {{Coord}}.
-- Finds a parameter in a transclusion of {{Coord}}.
local function coord2text(para,coord) -- this should be changed for languages which do not use Arabic numerals or the degree sign
local function coord2text(para,coord) -- this should be changed for languages which do not use Arabic numerals or the degree sign
local lat, long = mw.ustring.match(coord,'<span class="p%-latitude latitude">([^<]+)</span><span class="p%-longitude longitude">([^<]+)</span>')
if lat then
return tonumber(para == 'longitude' and long or lat)
end
local result = mw.text.split(mw.ustring.match(coord,'%-?[%.%d]+°[NS] %-?[%.%d]+°[EW]') or '', '[ °]')
local result = mw.text.split(mw.ustring.match(coord,'%-?[%.%d]+°[NS] %-?[%.%d]+°[EW]') or '', '[ °]')
if para == 'longitude' then result = {result[3], result[4]} end
if para == 'longitude' then result = {result[3], result[4]} end
if not tonumber(result[1]) or not result[2] then return error('Malformed coordinates value', 2) end
if not tonumber(result[1]) or not result[2] then
mw.log('Malformed coordinates value')
mw.logObject(para, 'para')
mw.logObject(coord, 'coord')
return error('Malformed coordinates value', 2)
end
return tonumber(result[1]) * hemisphereMultipliers[para][result[2]]
return tonumber(result[1]) * hemisphereMultipliers[para][result[2]]
end
end
Line 126: Line 135:
if args.AlternativeMap then
if args.AlternativeMap then
return args.AlternativeMap
return args.AlternativeMap
elseif args.relief and map('image1') ~= '' then
elseif args.relief then
return map('image1')
        local digits = mw.ustring.match(args.relief,'^[1-9][0-9]?$') or '1' -- image1 to image99
else
        if map('image' .. digits) ~= '' then
return map('image')
            return map('image' .. digits)
        end
end
end
    return map('image')
end
end


Line 162: Line 173:
         end
         end
     end
     end
local retval = args.float == 'center' and '<div class="center">' or ''
local retval = frame:extensionTag{name = 'templatestyles', args = {src = 'Module:Location map/styles.css'}}
if args.float == 'center' then
retval = retval .. '<div class="center">'
end
if args.caption and args.caption ~= '' and args.border ~= 'infobox' then
if args.caption and args.caption ~= '' and args.border ~= 'infobox' then
retval = retval .. '<div class="noviewer thumb '
retval = retval .. '<div class="locmap noviewer noresize thumb '
if args.float == '"left"' or args.float == 'left' then
if args.float == '"left"' or args.float == 'left' then
retval = retval .. 'tleft'
retval = retval .. 'tleft'
Line 180: Line 194:
retval = retval .. '"><div style="position:relative;width:' .. width .. 'px' .. (args.border ~= 'none' and ';border:1px solid lightgray">' or '">')
retval = retval .. '"><div style="position:relative;width:' .. width .. 'px' .. (args.border ~= 'none' and ';border:1px solid lightgray">' or '">')
else
else
retval = retval .. '<div style="width:' .. width .. 'px;'
retval = retval .. '<div class="locmap" style="width:' .. width .. 'px;'
if args.float == '"left"' or args.float == 'left' then
if args.float == '"left"' or args.float == 'left' then
retval = retval .. 'float:left;clear:left'
retval = retval .. 'float:left;clear:left'
Line 193: Line 207:
end
end
local image = getContainerImage(args, map)
local image = getContainerImage(args, map)
local currentTitle = mw.title.getCurrentTitle()
retval = string.format(
retval = string.format(
'%s[[File:%s|%spx|%s%s]]',
'%s[[File:%s|%spx|%s%s|class=notpageimage]]',
retval,
retval,
image,
image,
width,
width,
args.alt or ((args.label or mw.title.getCurrentTitle().text) .. ' is located in ' .. map('name')),
args.alt or ((args.label or currentTitle.text) .. ' is located in ' .. map('name')),
args.maplink and ('|link=' .. args.maplink) or ''
args.maplink and ('|link=' .. args.maplink) or ''
)
)
if args.caption and args.caption ~= '' then
if args.caption and args.caption ~= '' then
if mw.ustring.find(args.caption, '##') then
if (currentTitle.namespace == 0) and mw.ustring.find(args.caption, '##') then
retval = retval .. '[[Category:Pages using location map with a double number sign in the caption]]'
retval = retval .. '[[Category:Pages using location map with a double number sign in the caption]]'
end
end
end
end
if args.overlay_image then
if args.overlay_image then
return retval .. '<div style="position:absolute;top:0;left:0">[[File:' .. args.overlay_image .. '|' .. width .. 'px]]</div>'
return retval .. '<div style="position:absolute;top:0;left:0">[[File:' .. args.overlay_image .. '|' .. width .. 'px|class=notpageimage]]</div>'
else
else
return retval
return retval
Line 221: Line 236:
end
end
local retval = '</div>'
local retval = '</div>'
local currentTitle = mw.title.getCurrentTitle()
if not args.caption or args.border == 'infobox' then
if not args.caption or args.border == 'infobox' then
if args.border then
if args.border then
retval = retval .. '<div>'
retval = retval .. '<div style="padding-top:0.2em">'
else
else
retval = retval .. '<div style="font-size:90%;padding-top:3px">'
retval = retval .. '<div style="font-size:91%;padding-top:3px">'
end
end
retval = retval
retval = retval
.. (args.caption or (args.label or mw.title.getCurrentTitle().text) .. ' (' .. map('name') .. ')')
.. (args.caption or (args.label or currentTitle.text) .. ' (' .. map('name') .. ')')
.. '</div>'
.. '</div>'
elseif args.caption ~= ''  then
elseif args.caption ~= ''  then
-- This is not the pipe trick. We're creating a link with no text on purpose, so that CSS can give us a nice image
-- This is not the pipe trick. We're creating a link with no text on purpose, so that CSS can give us a nice image
retval = retval .. '<div class="thumbcaption"><div class="magnify">[[:File:' .. getContainerImage(args, map) .. '| ]]</div>' .. args.caption .. '</div>'
retval = retval .. '<div class="thumbcaption"><div class="magnify">[[:File:' .. getContainerImage(args, map) .. '|class=notpageimage| ]]</div>' .. args.caption .. '</div>'
end
end


Line 250: Line 265:
end
end
mw.logObject(args, 'args')
mw.logObject(args, 'args')
retval = retval .. '[[Category:Location maps with removed parameters|caption_undefined]]'
if currentTitle.namespace == 0 then
    retval = retval .. '[[Category:Location maps with removed parameters|caption_undefined]]'
end
end
end
if map('skew') ~= '' or map('lat_skew') ~= '' or map('crosses180') ~= '' or map('type') ~= '' then
if map('skew') ~= '' or map('lat_skew') ~= '' or map('crosses180') ~= '' or map('type') ~= '' then
mw.log('Removed parameter used in map definition ' .. map())
mw.log('Removed parameter used in map definition ' .. map())
local key = (map('skew') ~= '' and 'skew' or '') ..
if currentTitle.namespace == 0 then
    local key = (map('skew') ~= '' and 'skew' or '') ..
(map('lat_skew') ~= '' and 'lat_skew' or '') ..
(map('lat_skew') ~= '' and 'lat_skew' or '') ..
(map('crosses180') ~= '' and 'crosses180' or '') ..
(map('crosses180') ~= '' and 'crosses180' or '') ..
(map('type') ~= '' and 'type' or '')
(map('type') ~= '' and 'type' or '')
retval = retval .. '[[Category:Location maps with removed parameters|' .. key .. ' ]]'
    retval = retval .. '[[Category:Location maps with removed parameters|' .. key .. ' ]]'
end
end
end
if string.find(map('name'), '|', 1, true) then
if string.find(map('name'), '|', 1, true) then
mw.log('Pipe used in name of map definition ' .. map())
mw.log('Pipe used in name of map definition ' .. map())
retval = retval .. '[[Category:Location maps with a name containing a pipe]]'
if currentTitle.namespace == 0 then
  retval = retval .. '[[Category:Location maps with a name containing a pipe]]'
end
end
end
if args.float == 'center' then
if args.float == 'center' then
Line 272: Line 293:
local function markOuterDiv(x, y, imageDiv, labelDiv)
local function markOuterDiv(x, y, imageDiv, labelDiv)
return mw.html.create('div')
return mw.html.create('div')
:cssText('position:absolute;top:' .. round(y, 3) .. '%;left:' .. round(x, 3) .. '%')
:addClass('od')
:cssText('top:' .. round(y, 3) .. '%;left:' .. round(x, 3) .. '%')
:node(imageDiv)
:node(imageDiv)
:node(labelDiv)
:node(labelDiv)
Line 279: Line 301:
local function markImageDiv(mark, marksize, label, link, alt, title)
local function markImageDiv(mark, marksize, label, link, alt, title)
local builder = mw.html.create('div')
local builder = mw.html.create('div')
:cssText('position:absolute;left:-' .. round(marksize / 2) .. 'px;top:-' .. round(marksize / 2) .. 'px;line-height:0')
:addClass('id')
:cssText('left:-' .. round(marksize / 2) .. 'px;top:-' .. round(marksize / 2) .. 'px')
:attr('title', title)
:attr('title', title)
if marksize ~= 0 then
if marksize ~= 0 then
builder:wikitext(string.format(
builder:wikitext(string.format(
'[[File:%s|%dx%dpx|%s|link=%s%s]]',
'[[File:%s|%dx%dpx|%s|link=%s%s|class=notpageimage]]',
mark,
mark,
marksize,
marksize,
Line 297: Line 320:
local function markLabelDiv(label, label_size, label_width, position, background, x, marksize)
local function markLabelDiv(label, label_size, label_width, position, background, x, marksize)
if tonumber(label_size) == 0 then
if tonumber(label_size) == 0 then
return mw.html.create('div'):cssText('font-size:0%;position:absolute'):wikitext(label)
return mw.html.create('div'):addClass('l0'):wikitext(label)
end
end
local builder = mw.html.create('div')
local builder = mw.html.create('div')
:cssText('font-size:' .. label_size .. '%;line-height:110%;position:absolute;width:' .. label_width .. 'em')
:cssText('font-size:' .. label_size .. '%;width:' .. label_width .. 'em')
local distance = round(marksize / 2 + 1)
local distance = round(marksize / 2 + 1)
local spanCss
if position == 'top' then -- specified top
if position == 'top' then -- specified top
builder:cssText('bottom:' .. distance .. 'px;left:' .. (-label_width / 2) .. 'em;text-align:center')
builder:addClass('pv'):cssText('bottom:' .. distance .. 'px;left:' .. (-label_width / 2) .. 'em')
elseif position == 'bottom' then -- specified bottom
elseif position == 'bottom' then -- specified bottom
builder:cssText('top:' .. distance .. 'px;left:' .. (-label_width / 2) .. 'em;text-align:center')
builder:addClass('pv'):cssText('top:' .. distance .. 'px;left:' .. (-label_width / 2) .. 'em')
elseif position == 'left' or (tonumber(x) > 70 and position ~= 'right') then -- specified left or autodetected to left
elseif position == 'left' or (tonumber(x) > 70 and position ~= 'right') then -- specified left or autodetected to left
builder:cssText('top:-0.75em;right:' .. distance .. 'px;text-align:right')
builder:addClass('pl'):cssText('right:' .. distance .. 'px')
spanCss = 'float:right'
else -- specified right or autodetected to right
else -- specified right or autodetected to right
builder:cssText('top:-0.75em;left:' .. distance .. 'px;text-align:left')
builder:addClass('pr'):cssText('left:' .. distance .. 'px')
spanCss = 'float:left'
end
end
builder = builder:tag('div')
builder = builder:tag('div')
:css('display', 'inline')
:cssText('padding:1px')
:cssText(spanCss)
:wikitext(label)
:wikitext(label)
if background then
if background then
Line 384: Line 401:
end
end
local builder = mw.html.create()
local builder = mw.html.create()
local currentTitle = mw.title.getCurrentTitle()
if args.coordinates then
if args.coordinates then
-- Temporarily removed to facilitate infobox conversion. See [[Wikipedia:Coordinates in infoboxes]]
-- Temporarily removed to facilitate infobox conversion. See [[Wikipedia:Coordinates in infoboxes]]
Line 399: Line 417:
longitude, latitude = value.longitude, value.latitude
longitude, latitude = value.longitude, value.latitude
end
end
if args.link then
if args.link and (currentTitle.namespace == 0) then
builder:wikitext('[[Category:Location maps with linked markers with coordinates from Wikidata]]')
builder:wikitext('[[Category:Location maps with linked markers with coordinates from Wikidata]]')
end
end
Line 408: Line 426:
error('No value was provided for latitude')
error('No value was provided for latitude')
end
end
if (not args.lon_deg) ~= (not args.lat_deg) then
if currentTitle.namespace > 0 then
builder:wikitext('[[Category:Location maps with different longitude and latitude precisions|Degrees]]')
if (not args.lon_deg) ~= (not args.lat_deg) then
elseif (not args.lon_min) ~= (not args.lat_min) then
builder:wikitext('[[Category:Location maps with different longitude and latitude precisions|Degrees]]')
builder:wikitext('[[Category:Location maps with different longitude and latitude precisions|Minutes]]')
elseif (not args.lon_min) ~= (not args.lat_min) then
elseif (not args.lon_sec) ~= (not args.lat_sec) then
builder:wikitext('[[Category:Location maps with different longitude and latitude precisions|Minutes]]')
builder:wikitext('[[Category:Location maps with different longitude and latitude precisions|Seconds]]')
elseif (not args.lon_sec) ~= (not args.lat_sec) then
elseif (not args.lon_dir) ~= (not args.lat_dir) then
builder:wikitext('[[Category:Location maps with different longitude and latitude precisions|Seconds]]')
builder:wikitext('[[Category:Location maps with different longitude and latitude precisions|Hemisphere]]')
elseif (not args.lon_dir) ~= (not args.lat_dir) then
elseif (not args.long) ~= (not args.lat) then
builder:wikitext('[[Category:Location maps with different longitude and latitude precisions|Hemisphere]]')
builder:wikitext('[[Category:Location maps with different longitude and latitude precisions|Decimal]]')
elseif (not args.long) ~= (not args.lat) then
builder:wikitext('[[Category:Location maps with different longitude and latitude precisions|Decimal]]')
end
end
if ((tonumber(args.lat_deg) or 0) < 0) and ((tonumber(args.lat_min) or 0) ~= 0 or (tonumber(args.lat_sec) or 0) ~= 0 or (args.lat_dir and args.lat_dir ~='')) then
builder:wikitext('[[Category:Location maps with negative degrees and minutes or seconds]]')
end
if ((tonumber(args.lon_deg) or 0) < 0) and ((tonumber(args.lon_min) or 0) ~= 0 or (tonumber(args.lon_sec) or 0) ~= 0 or (args.lon_dir and args.lon_dir ~= '')) then
builder:wikitext('[[Category:Location maps with negative degrees and minutes or seconds]]')
end
if (((tonumber(args.lat_min) or 0) < 0) or ((tonumber(args.lat_sec) or 0) < 0)) then
builder:wikitext('[[Category:Location maps with negative degrees and minutes or seconds]]')
end
if (((tonumber(args.lon_min) or 0) < 0) or ((tonumber(args.lon_sec) or 0) < 0)) then
builder:wikitext('[[Category:Location maps with negative degrees and minutes or seconds]]')
end
end
if args.skew or args.lon_shift or args.markhigh then
if args.skew or args.lon_shift or args.markhigh then
Line 426: Line 458:
end
end
mw.logObject(args, 'args')
mw.logObject(args, 'args')
local key = (args.skew and 'skew' or '') ..
if currentTitle.namespace == 0 then
(args.lon_shift and 'lon_shift' or '') ..
local key = (args.skew and 'skew' or '') ..
(args.markhigh and 'markhigh' or '')
(args.lon_shift and 'lon_shift' or '') ..
builder:wikitext('[[Category:Location maps with removed parameters|' .. key ..' ]]')
(args.markhigh and 'markhigh' or '')
builder:wikitext('[[Category:Location maps with removed parameters|' .. key ..' ]]')
end
end
end
if map('x') ~= '' then
if map('x') ~= '' then
Line 448: Line 482:
end
end
mw.logObject(args, 'args')
mw.logObject(args, 'args')
local key = frame:preprocess('{{FULLPAGENAME}}')
if currentTitle.namespace == 0 then
builder:wikitext('[[Category:Location maps with marks outside map and outside parameter not set|' .. key .. ' ]]')
local key = currentTitle.prefixedText
builder:wikitext('[[Category:Location maps with marks outside map and outside parameter not set|' .. key .. ' ]]')
end
end
end
local mark = args.mark or map('mark')
local mark = args.mark or map('mark')
Line 459: Line 495:
local labelDiv
local labelDiv
if args.label and args.position ~= 'none' then
if args.label and args.position ~= 'none' then
labelDiv = markLabelDiv(args.label, args.label_size or 90, args.label_width or 6, args.position, args.background, x, marksize)
labelDiv = markLabelDiv(args.label, args.label_size or 91, args.label_width or 6, args.position, args.background, x, marksize)
end
end
return builder:node(markOuterDiv(x, y, imageDiv, labelDiv))
return builder:node(markOuterDiv(x, y, imageDiv, labelDiv))