Module:City: Difference between revisions
From Neodyland Wiki
More actions
define default link to Wikipedia instead of Commons for empty strings |
expand |
||
| Line 1: | Line 1: | ||
local p = {} | local p = {} | ||
function p._city(place, lang, link) | function p._city(place, lang, link) | ||
-- === STEP 1: if "place" is empty than do nothing ============== | |||
if (not place) or (place == "") then | if (not place) or (place == "") then | ||
return | 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 | end | ||
if string.match(place, "Q%d+") then | -- === 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 | else | ||
item = | local LookupTable = require("Module:City/data") | ||
local item = LookupTable[mw.ustring.lower(place)] | |||
if item then | |||
return wikidata._getLabel(item, {lang = lang, link = link}) | |||
end | |||
end | end | ||
if | |||
return place | -- === 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 mw.title.new( place, '' ).exists then | |||
return "[[" .. place .. "]]" | |||
elseif mw.title.new( place, 'category' ).exists then | |||
return string.format("[[:Category:%s|%s]]", place,place) | |||
end | |||
end | end | ||
-- return as is | |||
return place | |||
return | |||
end | end | ||
function p.city(frame) | function p.city(frame) | ||
local args = frame.args | local args = frame.args | ||
return p._city(args | args.place = mw.text.trim(args.place) | ||
if not (args.lang and mw.language.isSupportedLanguage(args.lang)) then | |||
args.lang = mw.message.new( "lang" ):plain() -- get user's chosen language | |||
end | |||
if (not args.link) or (mw.text.trim(args.link) == "") then | |||
args.link = "wikipedia" | |||
end | |||
return p._city(args.place, args.lang, args.link) | |||
end | end | ||
return p | return p | ||
Revision as of 03:54, 12 September 2016
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
local LookupTable = require("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 mw.title.new( place, '' ).exists then
return "[[" .. place .. "]]"
elseif mw.title.new( place, 'category' ).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
args.place = mw.text.trim(args.place)
if not (args.lang and mw.language.isSupportedLanguage(args.lang)) then
args.lang = mw.message.new( "lang" ):plain() -- get user's chosen language
end
if (not args.link) or (mw.text.trim(args.link) == "") then
args.link = "wikipedia"
end
return p._city(args.place, args.lang, args.link)
end
return p