Module:Type in 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 = plaintext(text)
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-),', '%1') --fix possible blank seperated commas from previous cleanup
:gsub('%s%s', ' ') --fix possible extra spaces 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..',' --comma at the end for convenient splitting with gmatch
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
--validate type parameter
function p.validateTyp (typ, args)
checkpatterns = args['check-patterns']
invalidadd = args.invalidadd
if checkpatterns then
for k in (checkpatterns..';'):gmatch('([^;]*);') do --split checkpatterns by ;, check if one of the patterns is in type
if typ:match(k) then return typ end
end
if invalidadd then --if invalid, add to make it valid
return typ..' '..invalidadd
end
else
return typ
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 = args[1]
if typ then typ = plaintext(args[1]) end
if not typ then return end --check after plaintexting if typ exists
sep = ((args.sep == 'no') and '') or args.sep or ' in ' --if args.sep set to no, nothing between type and loc, if it has other value put that
loc = args[2]
if args['full-loc'] then func = '_prepareLoc' else func = '_generalLoc' end
loc = loc and (sep..p[func](args[2])) or ''
typ = p.validateTyp (typ, args)
if typ then return frame:expandTemplate {title = 'Short description', args = {typ..loc, 'noreplace'}} end
end
return p
Content Disclaimer
Informasi ini disarikan dari Wikipedia dan disajikan kembali untuk tujuan edukasi. Konten tersedia di bawah lisensi CC BY-SA 3.0. Kami tidak bertanggung jawab atas ketidakakuratan data yang bersumber dari kontribusi publik tersebut.
- The information displayed on this website is sourced in part or in whole from Wikipedia and has been adapted for the purpose of restating it. We strive to provide accurate and relevant information, however:
- There is no guarantee of absolute accuracy. Wikipedia is an open, collaborative project that can be edited by anyone, so information is subject to change.
- It is not intended to constitute professional advice. The content displayed is for informational and educational purposes only. For important decisions (e.g., medical, legal, or financial), please consult a professional.
- Content copyright. Wikipedia is licensed under the Creative Commons Attribution-ShareAlike License (CC BY-SA). This means that content may be reused with appropriate attribution and shared under a similar license.
- Responsible use. Any risk arising from the use of information from this website is entirely the responsibility of the user.