Module:City
From Neodyland Wiki
More actions
This documentation is transcluded from Module:City/doc.
local p = {}
function p._city(place, lang, link)
-- === STEP 1: if "place" is empty than do nothing ==============
if (not place) or (place == "") then
return ""
end
--=== STEP 2: Check if "place" already has a link and if so than skip the rest of the template ==============
if string.match(place, "[%{%{|%[%[].+[%}%}|%]%]]") then
return place
end
-- === STEP 3: Check if "place" holds a q-code or matches any of the hardwired names ==============
local wikidata = require("Module:Wikidata")
if string.match(place, "^Q%d+$") then
return wikidata._getLabel(place, {lang = lang, link = link})
else
-- if multiple calls to {{City}} from a single file, than mw.loadData should load [[Module:City/data]] only once
local LookupTable = mw.loadData("Module:City/data")
local item = LookupTable[mw.ustring.lower(place)]
if item then
return wikidata._getLabel(item, {lang = lang, link = link})
end
end
-- === STEP 4: Check if {{{1}}} matches existing template, gallery or category and if so provide the link ===
if #place>=3 and #place<40 then
local page = mw.title.new( place, '' )
if page and page.exists then
return "[[" .. place .. "]]"
end
page = mw.title.new( place, 'category' )
if page and page.exists then
return string.format("[[:Category:%s|%s]]", place,place)
end
end
-- return as is
return place
end
function p.city(frame)
local args = frame.args
if not (args.lang and mw.language.isSupportedLanguage(args.lang)) then
args.lang = frame:callParserFunction( "int", "lang" ) -- get user's chosen language
end
if (not args.link) or (mw.text.trim(args.link) == "") then
args.link = "wikipedia"
end
args.place = mw.text.trim(args.place or '')
return p._city(args.place, args.lang, args.link)
end
return p