Module:Mapframe: Difference between revisions
Jump to navigation
Jump to search
>Evad37 (+) |
>Evad37 (+) |
||
Line 1: | Line 1: | ||
--Parameter for cleaned-up parent.args (whitespace trimmed, blanks removed) | --Parameter for cleaned-up parent.args (whitespace trimmed, blanks removed) | ||
local Args = {} | local Args = {} | ||
local defaults = {} | |||
function setCleanArgs(argsTable) | function setCleanArgs(argsTable) | ||
Line 17: | Line 19: | ||
end | end | ||
function | function makeContent(args) | ||
if args.raw then | |||
return args.raw | |||
end | end | ||
local content = {}; | local content = {}; | ||
local contentIndex = ''; | local contentIndex = ''; | ||
Line 58: | Line 59: | ||
end | end | ||
function makeTagAttribs(args, isTitle) | |||
local attribs = {} | |||
if args.zoom then | |||
attribs.zoom = args.zoom | |||
end | |||
-- todo: all the others | |||
return attribs | |||
end | |||
function makeTitleOutput(args, tagContent) | |||
local titleTag = mw.text.tag('maplink', makeTagAttribs(args, true), tagContent) | |||
local spanAttribs = { | |||
style = "font-size: small;", | |||
id = "coordinates" | |||
} | |||
return mw.text.tag('span', spanAttribs, titleTag) | |||
end | |||
function makeInlineOutput(args, tagContent) | |||
local tagName = 'maplink' | |||
if args.frame then | |||
tagName = 'mapframe' | |||
end | |||
return mw.text.tag(tagName, makeTagAttribs(args), tagContent) | |||
end | |||
local p = {} | local p = {} | ||
Line 67: | Line 95: | ||
local tagContent = makeContent(Args) | local tagContent = makeContent(Args) | ||
local display = mw.text.split(Args.display, '%s*,%s*') | |||
local displayInTitle = display[1] == 'title' or display[2] == 'title' | |||
local displayInline = display[1] == 'inline' or display[2] == 'inline' | |||
if displayInTitle and displayInline then | |||
return makeTitleOutput(args, tagContent) .. makeInlineOutput(args, tagContent) | |||
elseif displayInTitle then | |||
return makeTitleOutput(args, tagContent) | |||
elseif displayInline then | |||
return makeInlineOutput(args, tagContent) | |||
else | |||
error( 'Invalid display parameter') | |||
end | |||
end | end | ||
return p | return p |
Revision as of 00:53, 2 May 2018
Documentation for this module may be created at Module:Mapframe/doc
--Parameter for cleaned-up parent.args (whitespace trimmed, blanks removed) local Args = {} local defaults = {} function setCleanArgs(argsTable) local cleanArgs = {} for key, val in pairs(argsTable) do if type(val) == 'string' then val = val:match('^%s*(.-)%s*$') if val ~= '' then cleanArgs[key] = val end else cleanArgs[key] = val end end return cleanArgs end function makeContent(args) if args.raw then return args.raw end local content = {}; local contentIndex = ''; while Args['type'..contentIndex] do local contentArgs = {} contentArgs['type'] = Args['type'..contentIndex] --todo: Add other relevant args content[contentIndex or 1] = makeContentJson(contentArgs) contentIndex = (contentIndex or 1) + 1 end --Single item, no array needed if #content==1 then return content[0] end --Multiple items get placed in an array local contentArray = '[\n' + table.concat( content, ',\n') + '\n]' return contentArray end function makeContentJson(contentArgs) local data = {} if contentArgs.type == 'point' then data.type = "Feature" else data.type = "ExternalData" end --todo: fill out rest of data return mw.text.jsonEncode(data) end function makeTagAttribs(args, isTitle) local attribs = {} if args.zoom then attribs.zoom = args.zoom end -- todo: all the others return attribs end function makeTitleOutput(args, tagContent) local titleTag = mw.text.tag('maplink', makeTagAttribs(args, true), tagContent) local spanAttribs = { style = "font-size: small;", id = "coordinates" } return mw.text.tag('span', spanAttribs, titleTag) end function makeInlineOutput(args, tagContent) local tagName = 'maplink' if args.frame then tagName = 'mapframe' end return mw.text.tag(tagName, makeTagAttribs(args), tagContent) end local p = {} function p.main(frame) local parent = frame.getParent(frame) Args = setCleanArgs(parent.args) local tagContent = makeContent(Args) local display = mw.text.split(Args.display, '%s*,%s*') local displayInTitle = display[1] == 'title' or display[2] == 'title' local displayInline = display[1] == 'inline' or display[2] == 'inline' if displayInTitle and displayInline then return makeTitleOutput(args, tagContent) .. makeInlineOutput(args, tagContent) elseif displayInTitle then return makeTitleOutput(args, tagContent) elseif displayInline then return makeInlineOutput(args, tagContent) else error( 'Invalid display parameter') end end return p