Module:Sandbox/Chqaz/sandbox
Appearance
This is the module sandbox page for Module:Sandbox/Chqaz. |
Module documentation
[create]
--[[
Source script: https://it.wikivoyage.org/wiki/Modulo:Marker
Maintainer: Andyrom75
Lazy loads:
require('Module:WikidataIB').getSiteLink
]]
local coord2Dec = require('Module:Sandbox/Chqaz/Coordinates').toDec
local type2color = 'Module:TypeToColor'
local yesno = require( 'Module:Yesno' )
local function _isDefined(s)
return s ~= '' and s
end
local function _makeMarkerSymbol( args, frame )
local image4map = _isDefined(args.image) and ('[[File:' .. args.image .. '|300px]]') or ''
local tagArgs = {
zoom = tonumber( args.zoom ),
latitude = tonumber( args.lat ),
longitude = tonumber( args.long ),
show = args.show,
group = args.group,
description = (_isDefined(args.description) or '') .. image4map
}
if _isDefined( args.text ) then
tagArgs.text = args.text
if not args.useIcon then
tagArgs.class = 'no-icon'
end
end
local titleLink = ''
local details = args.details
local detailsLink = ''
if yesno(details, false) then
detailsLink = '<sup>[[#' .. args.name .. '|詳細]]</sup>'
end
if _isDefined( args.link ) then
if args.link == 'yes' then
titleLink = '[[' .. args.name .. ']]'
else
titleLink = '[[' .. args.link .. '|' .. args.name .. ']]'
end
else
titleLink = args.name
end
local title = titleLink .. detailsLink
local geoJson = {
type = 'Feature',
geometry = {
type = 'Point',
coordinates = { tagArgs.longitude, tagArgs.latitude }
},
properties = {
title = title,
description = tagArgs.description,
['marker-symbol'] = args.symbol,
['marker-color'] = args.color,
['marker-size'] = 'medium',
}
}
-- frame:extensionTag is expensive
return frame:extensionTag( 'maplink', mw.text.jsonEncode( geoJson ), tagArgs )
end
local function _Marker(frame)
local args = frame.args
local imageErrMsg = ''
--shortcut variable
local name = args.name or ''
local link = args.link or ''
local tag = ((args.islisting ~= 'yes') and mw.html.create( 'span' ):addClass( 'vcard' )) or mw.html.create()
local anchor = ''
if _isDefined( args.wikidata ) then
-- Se manca una delle due coordinate, provo a estrarle entrambe da Wikidata
if not _isDefined(args.lat) or not _isDefined(args.long) then
local coords = mw.wikibase.getBestStatements( args.wikidata, 'P625')
if coords[1] then
args.lat = coords[1].mainsnak.datavalue.value.latitude
args.long = coords[1].mainsnak.datavalue.value.longitude
end
end
-- Se non è stata fornita l'immagine, provo a estrarla da Wikidata
if not _isDefined(args.image) then
local image = mw.wikibase.getBestStatements( args.wikidata, 'P18')
args.image = image[1] and image[1].mainsnak.datavalue.value
end
end
-- Se sono presenti le coordinate creo il marker tag
if args.lat ~= 'NA' and args.long ~= 'NA' then
if _isDefined(args.lat) and _isDefined(args.long) then
-- arguments setup
args.lat = coord2Dec(args.lat or 0, '', 6).dec
args.long = coord2Dec(args.long or 0, '', 6).dec
args.type = _isDefined(args.type) or 'listing'
args.zoom = _isDefined(args.zoom) or '17'
args.group = _isDefined(args.group) or args.type
args.symbol = '-number-' .. ( args.counter or args.type or 'listing' )
args.color = ''
args.show = args.show or 'mask,around,buy,city,do,drink,eat,go,listing,other,see,sleep,vicinity,view'
local mapTagOutput = _makeMarkerSymbol( args, frame )
tag:tag( 'span' )
:addClass( 'noprint listing-coordinates' )
:css('display','none')
:tag( 'span' )
:addClass( 'geo' )
:tag( 'abbr' )
:addClass( 'latitude' )
:wikitext( args.lat )
:done()
:tag( 'abbr' )
:addClass( 'longitude' )
:wikitext( args.long )
:allDone()
:tag( 'span' )
:attr( 'title', 'See this element on the map' )
:wikitext( mapTagOutput )
else
local listingTypes = {see=true, eat=true, buy=true, drink=true, sleep=true, ['do']=true, go=true, city=true, vicinity=true}
if mw.title.getCurrentTitle().namespace == 0 and args.type and listingTypes[string.lower(args.type)] then
anchor = anchor .. "[[Category:座標が無いListing]]"
end
end
end
-- costruisco l'ancora sul name ed eventualmente gli associo il sitoweb
if _isDefined( args.wikidata ) then
local flagImage = mw.wikibase.getBestStatements( args.wikidata, 'P41' )
if flagImage[1] then
local flag = '[[ファイル:' .. flagImage[1].mainsnak.datavalue.value .. '|30px]]'
anchor = anchor .. flag
end
end
if name ~= '' then
local anchor2 = ''
if link ~= '' then
anchor2 = string.match(link, 'yes')
and '<span id="' .. mw.uri.anchorEncode( name ) .. '" class="fn org listing-name" style="font-weight:bold;">[[' .. name .. ']]</span>'
or '<span id="' .. mw.uri.anchorEncode( name ) .. '" class="fn org listing-name" style="font-weight:bold;">[[' .. link .. '|' .. name .. ']]</span>'
else
anchor2 = '<span id="' .. mw.uri.anchorEncode( name ) .. '" class="fn org listing-name" style="font-weight:bold;">' .. name .. '</span>'
end
-- se includo un sito web il name deve essere puramente testuale
anchor = anchor .. anchor2
end
if mw.title.getCurrentTitle().namespace == 0 then
anchor = anchor .. '[[Category:マップマーカーあり]]'
end
if _isDefined( args.url ) then
local url = ' <span class="fn org listing-link">[[ファイル:Blue globe icon.svg|15px|link=' .. args.url .. '|外部リンク|alt=外部リンク|class=listing-url]]</span>'
anchor = anchor .. url
end
-- Se l'immagine fornita non esiste genero un messaggio di errore
if _isDefined( args.image ) then
local image = 'ファイル:' .. args.image
if mw.title.new( image ) and mw.title.new( image ).exists == true then
args.description = '[[' .. image .. '|100x100px|' .. name .. ']]'
else
imageErrMsg = mw.html.create( 'span' )
:addClass('imageinfo debuginfo')
:css('display','none')
:wikitext( 'NOT EXISTING LISTING IMAGE' )
end
end
if (args.islisting ~= 'yes') and _isDefined( args.wikidata ) then
local getSiteLink = require('Module:WikidataIB').getSiteLink
if _isDefined( getSiteLink{args={wiki='jawikivoyage', qid=args.wikidata}} ) then
anchor = anchor .. (string.match(name, '%[%[') and '' or '[[Category:記事がありウィキ間リンクが無いマーカー]]')
else
local wpLink = _isDefined( getSiteLink{args={wiki='jawiki', qid=args.wikidata}} )
anchor = anchor .. (wpLink and '<span class="listing-sister-icons"> [[File:Antu wikipedia.svg|15px|class=listing-sister|link=w:' .. wpLink .. '|Wikipedia上の<span>' .. wpLink .. '</span>]]</span>' or '')
end
end
return tostring(tag:node(imageErrMsg):wikitext(' ' .. anchor))
end
--Interfacce del modulo
local p = {}
function p.MarkerTemplate(frame)
return _Marker(frame:getParent())
end
function p.MarkerInvoke(frame)
return _Marker(frame)
end
function p.MarkerModule(frame)
local Cframe = mw.getCurrentFrame()
Cframe.args = frame.args
return _Marker(Cframe)
end
return p