Module:Coordinates: Difference between revisions

Content deleted Content added
use templatestyles
Tomartino (talk | contribs)
m 166 revisions imported from wikipedia:Module:Coordinates
 
(18 intermediate revisions by 9 users not shown)
Line 16:
]]
 
require('Module:No globalsstrict')
 
local math_mod = require("Module:Math")
local coordinates = {};
local isSandbox = mw.getCurrentFrame():getTitle():find('sandbox', 1, true);
 
local current_page = mw.title.getCurrentTitle()
local page_name = mw.uri.encode( current_page.prefixedText, 'WIKI' );
local coord_link = 'https://geohack.toolforge.org/geohack.php?pagename=' .. page_name .. '&params='
local templatestyles = 'Module:Coordinates/styles.css'
 
--[[ Helper function, replacement for {{coord/display/title}} ]]
local function displaytitle(s, notescoords)
return mw.getCurrentFrame():extensionTag{ name = 'templatestyles', args = { src = templatestyles} }
local l = "[[Geographic coordinate system|Coordinates]]: " .. s
name = 'indicator',
local co = '<span id="coordinates">' .. l .. notes .. '</span>';
args = { name = 'coordinates' },
return '<span style="font-size: small;">' .. co .. '</span>';
local l content = '<span id="coordinates">[[Geographic coordinate system|Coordinates]]: "' .. coords .. s'</span>'
end
}
 
--[[ Helper function, Replacement for {{coord/display/inline}} ]]
local function displayinline(s, notes)
return s .. notes
end
 
Line 69 ⟶ 66:
local result = ""
for i,v in ipairs(errors) do
local errorHTMLresult = result .. '<strong class="error">Coordinates: ' .. v[2] .. '</strong><br />'
result = result .. errorHTML .. "<br />"
end
return result
Line 157 ⟶ 153:
end
 
local stylesheetLink = 'Module:Coordinates' .. ( isSandbox and '/sandbox' or '' ) .. '/styles.css'
return mw.getCurrentFrame():extensionTag{ name = 'templatestyles', args = { src = templatestyles} }
return mw.getCurrentFrame():extensionTag{
.. '<span class="plainlinks nourlexpansion">'
name = 'templatestyles', args = { src = stylesheetLink }
.. '[' .. coord_link .. uriComponents .. ' ' .. inner .. ']'
} .. '<span class="plainlinks nourlexpansion">[' .. coord_link .. uriComponents ..
.. '</span>'
' ' .. inner .. ']</span>' .. '[[Category:Pages using gadget WikiMiniAtlas]]'
end
 
Line 467 ⟶ 464:
else
-- Error
return errorPrinter({{"formatTest", "Unknown argument format"}}) .. '[[Category:Pages with malformed coordinate tags]]'
end
result.name = args.name
Line 490 ⟶ 487:
local function makeWikidataCategories(qid)
local ret
local qid = qid or mw.wikibase.getEntityIdForCurrentPage()
if mw.wikibase and current_page.namespace == 0 then
localif entityqid =and mw.wikibase.entityExists(qid) and mw.wikibase.getEntityObjectgetBestStatements(qid, "P625") orand mw.wikibase.getEntityObjectgetBestStatements(qid, "P625")[1] then
local snaktype = entitymw.claimswikibase.getBestStatements(qid, "P625")[1].mainsnak.snaktype
if entity and entity.claims and entity.claims.P625 and entity.claims.P625[1] then
local snaktype = entity.claims.P625[1].mainsnak.snaktype
if snaktype == 'value' then
-- coordinates exist both here and on Wikidata, and can be compared.
Line 633 ⟶ 630:
local Display = args.display and args.display:lower() or 'inline'
 
-- it and ti are short for inline,title and title,inline
local function isInline(s)
-- Finds whether coordinates are displayed inline.
Line 649 ⟶ 647:
local text = ''
if isInline(Display) then
text = text .. displayinline('<span class="geo-inline">' .. contents, .. Notes) .. '</span>'
end
if isInTitle(Display) then
-- Add to output since indicator content is invisible to Lua later on
text = text
if not isInline(Display) then
.. displaytitle(contents, Notes)
text = text .. '<span class="geo-inline-hidden noexcerpt">' .. contents .. Notes .. '</span>'
.. makeWikidataCategories(args.qid)
end
text = text .. displaytitle(contents .. Notes) .. makeWikidataCategories(args.qid)
end
if not args.nosave then
Line 687:
 
]]
function coordinates.coord2text_coord2text(framecoord,type)
if frame.args[1]coord == '' or frame.args[2]type == '' or not frame.args[2]type then return nil end
frame.args[2]type = mw.text.trim(frame.args[2]type)
if frame.args[2]type == 'lat' or frame.args[2]type == 'long' then
local result, negative = mw.text.split((mw.ustring.match(frame.args[1]coord,'[%.%d]+°[NS] [%.%d]+°[EW]') or ''), ' ')
if frame.args[2]type == 'lat' then
result, negative = result[1], 'S'
else
Line 701:
return result[1]
else
return mw.ustring.match(frame.args[1]coord, 'params=.-_' ..frame.args[2] type .. ':(.-)[ _]')
end
end
 
function coordinates.coord2text(frame)
return coordinates._coord2text(frame.args[1],frame.args[2])
end
 
Line 719 ⟶ 723:
]]
function coordinates.coordinsert(frame)
-- for the 2nd or later integer parameter (the first is the coord template, as above)
for i, v in ipairs(frame.args) do
if i ~= 1 then
-- if we cannot find in the coord_template the i_th coordinsert parameter e.g. region
if not mw.ustring.find(frame.args[1], (mw.ustring.match(frame.args[i], '^(.-:)') or '')) then
-- find from the params= up to the first possibly-present underscore
-- and append the i_th coordinsert parameter and a space
-- IDK why we're adding a space but it does seem somewhat convenient
frame.args[1] = mw.ustring.gsub(frame.args[1], '(params=.-)_? ', '%1_'..frame.args[i]..' ')
end
Line 727 ⟶ 736:
end
if frame.args.name then
-- if we can't find the vcard class
if not mw.ustring.find(frame.args[1], '<span class="vcard">') then
-- take something that looks like a coord template and add the vcard span with class and fn org class
local namestr = frame.args.name
frame.args[1] = mw.ustring.gsub(frame.args[1],
frame.args[1],
'(<span class="geo%-default">)(<span[^<>]*>[^<>]*</span><span[^<>]*>[^<>]*<span[^<>]*>[^<>]*</span></span>)(</span>)',
'%1<span class="vcard">%2<span style="display:none">&#xfeff; (<span class="fn org">' .. namestr .. '</span>)</span></span>%3')
)
frame.args[1] = mw.ustring.gsub(frame.args[1], '(&params=[^&"<>%[%] ]*) ', '%1&title=' .. mw.uri.encode(namestr) .. ' ')
-- then find anything from coordinates parameters to the 'end' and attach the title parameter
frame.args[1] = mw.ustring.gsub(
frame.args[1],
'(&params=[^&"<>%[%] ]*) ',
'%1&title=' .. mw.uri.encode(namestr) .. ' '
)
end
end
-- replace the existing indicator with a new indicator using the modified content
frame.args[1] = mw.ustring.gsub(
frame.args[1],
'(<span class="geo%-inline[^"]*">(.+)</span>)\127[^\127]*UNIQ%-%-indicator%-%x+%-%-?QINU[^\127]*\127',
function (inline, coord) return inline .. displaytitle(coord) end
)
 
return frame.args[1]
end