Module:Type in location: Difference between revisions
fix |
fix |
||
Line 10: | Line 10: | ||
text = text:gsub('<br/?>', ', ') --<br> or <br/> with commas |
text = text:gsub('<br/?>', ', ') --<br> or <br/> with commas |
||
text = plaintext(text) --plain textify to reduce strange stuff |
text = plaintext(text) --plain textify to reduce strange stuff |
||
text = text..',' --comma at the end makes things convenient |
|||
text = text:gsub('%b()', ', ') --remove things in brackets as etxtraneous information |
text = text:gsub('%b()', ', ') --remove things in brackets as etxtraneous information |
||
:gsub('[%s,](.-%d.-)[%s,]', '') --remove things with digits as generally being unecessary postal codes/road numbers etc |
:gsub('[%s,](.-%d.-)[%s,]', '') --remove things with digits as generally being unecessary postal codes/road numbers etc |
Revision as of 04:14, 19 May 2018
This Lua module is used on approximately 151,000 pages. To avoid major disruption and server load, any changes should be tested in the module's /sandbox or /testcases subpages, or in your own module sandbox. The tested changes can be added to this page in a single edit. Consider discussing changes on the talk page before implementing them. |
This module depends on the following other modules: |
Usage
For generating short descriptions for infoboxes of structures/small places such as dams and museums, which are often in the format of type in location. Example:
{{#invoke:Type in location|main|{{{type|}}}|{{{location|}}}|check-patterns = [Mm]useum;foo;bar}}
Description only shows if check-pattern is matched in type.
Multiple check-patterns can be specified as "pattern1;pattern2;pattern3" and so on.
Multiple infobox location parameters can be specified using {{comma separated entries}}; see {{Infobox television station}} and {{Infobox hospital}} for complex examples.
Also has helper functions of prepareLoc (for removing extraneous from addresses) and generalLoc (for getting more general location from specific address) which can be used seperately.
What is specified in the |invalidadd=
parameter will be added with a space if the check-patterns fail.
Setting |full-loc=
gives the full of the location (with slight processing) instead of the more general location.
local p = {}
local plaintext = require("Module:Plain text")._main
--Cleanup/format location for use in short descriptions
function p.prepareLoc (frame)
return p._prepareLoc (frame.args[1])
end
function p._prepareLoc (text)
text = text:gsub('<br/?>', ', ') --<br> or <br/> with commas
text = plaintext(text) --plain textify to reduce strange stuff
text = text..',' --comma at the end makes things convenient
text = text:gsub('%b()', ', ') --remove things in brackets as etxtraneous information
:gsub('[%s,](.-%d.-)[%s,]', '') --remove things with digits as generally being unecessary postal codes/road numbers etc
:gsub('%s%s', '') --remove possible double spaces from previous cleanup
:gsub('(,%s-),', '') --remove possible blank seperated commas from previous cleanup
:gsub('^[%s,]*', '') --trim commas and spaces from beginning
:gsub('[%s,]*$', '') --trim commas and spaces from end
return text
end
--Gets general location from more specific one for short descriptions
--i.e if a location is specified to be "P. Sherman 42 Wallaby Way Sydney, Australia", return "Sydney, Australia"
--splits by commas and returns last two entries
function p.generalLoc (frame)
return p._generalLoc (frame.args[1])
end
function p._generalLoc (loc)
loc = p._prepareLoc(loc)
split = {}
num = 0
loc = loc..','
for k in loc:gmatch('([^,]*),') do --split by commmas
table.insert(split, k)
num = num + 1
end
if num == 1 then --if only comma was the one at the end return the whole thing
return split[1]
else
return split[num-1]..','..split[num] --return last two entries seperated by commas
end
end
--generates type in location
function p.main(frame)
args = require('Module:Arguments').getArgs (frame, {frameOnly = true})
return p._main(args, frame)
end
function p._main (args, frame)
typ = plaintext(args[1])
loc = args[2]
loc = loc and (' in '..p._generalLoc(args[2])) or ''
checkpattern = args['check-pattern']
if typ then
if (not checkpattern) or typ:match(checkpattern) then --check checkpattern exists; if it doesn't, true; if it exists and matches, true
return frame:expandTemplate {title = 'Short description', args = {typ..loc, 'noreplace'}}
end
end
end
return p