モジュール:Geobox map

-- this module implements template:geobox2 map
local getArgs = require('Module:Arguments').getArgs
local data_module = "Module:Geobox map/data"
local p = {}
local function rnd2(n)
	return math.floor(n * 100 + 0.5) / 100
end
local function aspect_ratio(s)
	local f = mw.title.new(s).file
	if f ~= nil and f ~= false then
		return f'width' / f'height'
	else
		return 1
	end
end
-- Finds a parameter in a transclusion of .
local function coord2text(para,coord) -- this should be changed for languages which do not use Arabic numerals or the degree sign
	local hemisphereMultipliers = {
	longitude = { W = -1, w = -1, E = 1, e = 1 },
	latitude = { S = -1, s = -1, N = 1, n = 1 }
	}
	local i18nmap = {'東' = 'E', '南' = 'S', '西' = 'W', '北' = 'N'}
	local result = mw.text.split(mw.ustring.match(coord,'%-?%.%d+°NS %-?%.%d+°EW') or , ' °')
	if table.maxn(result) == 1 then
		result = mw.text.split(mw.ustring.match(coord,'南北緯%-?%.%d+度 東西経%-?%.%d+度') or , '緯経度 ')
		if table.maxn(result) ~= 1 then
			result = {result2, i18nmapresult1, result5, i18nmapresult4}
		end
	end
	if para == 'longitude' then result = {result3, result4} end
	if not tonumber(result1) or not result2 then return error('不正な座標指定です。', 2) end
	return tonumber(result1) * hemisphereMultiplierspararesult2
end
function p.main(frame)
	local tracking_cats = 
	local args = getArgs(frame)
	local root = mw.html.create('div')
	root:css('width', (args'width' or '256') .. 'px')
		:css('margin-left', 'auto')
		:css('margin-right', 'auto')
	local div = root:tag('div'):css('position', 'relative')
	if args'background' then
		local im = 'File:' .. args'background'
		im = im .. '|' .. (args'width' or '256') .. 'px'
		if args'map' then
			im = im .. '|link='
		else
			im = im .. '|alt=' .. (args'alt' or )
		end
		im = im .. '|' .. (args'caption' or ) .. 
		div:tag('div')
			:css('position', 'absolute')
			:wikitext(im)
	end
	if args'map' then
		local im = 'File:' .. args'map'
		im = im .. '|' .. (args'width' or '256') .. 'px'
		im = im .. '|alt=' .. (args'alt' or )
		im = im .. '|' .. (args'caption' or ) .. 
		div:tag('div')
			:css('position', 'relative')
			:wikitext(im)
	end
	if (args'x' and args'y') or (args'coordinates' and args'locator') then
		local x, y = 0, 0
		if (args'x' and args'y') then
			x = tonumber(args'x' or '0') or 0
			y = tonumber(args'y' or '0') or 0
		elseif args'locator' then
			x = coord2text('longitude', args'coordinates')
			y = coord2text('latitude', args'coordinates')
		end
		local lpct, tpct = 0, 0
		if args'locator' then
			-- local tname = 'Geobox locator ' .. args'locator'
			-- left = tonumber(frame:expandTemplate{title = tname, args = { 'left' }} or left) or left
			-- right = tonumber(frame:expandTemplate{title = tname, args = { 'right' }} or right) or right
			-- top = tonumber(frame:expandTemplate{title = tname, args = { 'top' }} or top) or top
			-- bottom = tonumber(frame:expandTemplate{title = tname, args = { 'bottom' }} or bottom) or bottom
			local all_maps = mw.loadData(data_module)
			local locator = mw.ustring.gsub(args'locator', "_ _ *", ' ')
			local map_data = all_mapslocator
			if map_data and type(map_data) == 'table' then
				tracking_cats = tracking_cats .. 
				left = map_data'left'
				right = map_data'right'
				top = map_data'top'
				bottom = map_data'bottom'
				lpct = rnd2( (x - left)/(right - left)*100 )
				tpct = rnd2( (top - y) / (top - bottom)*100 )
			else
				tracking_cats = tracking_cats .. 
			end
		else
			local scale = aspect_ratio('Image:' .. (args'map' or ))
			lpct = rnd2(x)
			tpct = rnd2(y*scale)
			tracking_cats = tracking_cats .. 
		end
		div:tag('div')
			:css('position', 'absolute')
			:css('left', lpct .. '%')
			:css('top', tpct .. '%')
			:css('padding', '0')
			:css('line-height', '0')
			:tag('div')
				:css('position', 'absolute')
				:css('text-align', 'center')
				:css('left', '-4px')
				:css('top', '-4px')
				:css('width', '8px')
				:css('font-size', '8px')
				:css('line-height', '0')
				:wikitext('alt=')
	end
	if args'caption' then
		root:tag('div')
			:css('padding-top', '0.4em')
			:css('text-align', 'center')
			:wikitext(args'caption')
	end
	if args'table' then
		return '\n' .. tostring(root) .. tracking_cats .. '\n'
	else
		return tostring(root) .. tracking_cats
	end
end
return p