Module:Mapframe: Difference between revisions
Jump to navigation
Jump to search
>Evad37 (plain output for testing) |
>Evad37 (rest of data for json) |
||
Line 52: | Line 52: | ||
end | end | ||
function makeContentJson( | function makeCoordsText(args) | ||
local coords | |||
local frame = mw.getCurrentFrame() | |||
if args.coord then | |||
coords = args.coord | |||
else | |||
coords = frame:preprocess('{{WikidataCoord|display=|'..(args.id or args.ids or mw.wikibase.getEntityIdForCurrentPage())..'}}') | |||
end | |||
return frame:preprocess('[{{#invoke:coordinates|coord2text|'..coords..'|long}}, {{#invoke:coordinates|coord2text|'..coords..'|lat}}]') | |||
end | |||
function makeContentJson(args) | |||
local data = {} | local data = {} | ||
if | |||
if args.type == 'point' then | |||
data.type = "Feature" | data.type = "Feature" | ||
data.geometry = { | |||
type = "Point", | |||
coordinates = makeCoordsText(args) | |||
} | |||
data.properties = { | |||
title = args.title or mw.getCurrentFrame():getParent():getTitle(), | |||
["marker-symbol"] = args.marker or "marker", | |||
["marker-color"] = "5E74F3" | |||
} | |||
else | else | ||
data.type = "ExternalData" | data.type = "ExternalData" | ||
if args.type == "data" or args.from then | |||
data.service = "page" | |||
elseif args.type == "line" then | |||
data.service = "geoline" | |||
elseif args.type == "shape" then | |||
data.service = "geoshape" | |||
elseif args.type == "shape-inverse" then | |||
data.service = "geomask" | |||
end | |||
if args.id or args.ids or (not (args.from) and mw.wikibase.getEntityIdForCurrentPage()) then | |||
data.ids = args.id or args.ids or mw.wikibase.getEntityIdForCurrentPage() | |||
else | |||
data.title = args.from | |||
end | |||
data.properties = { | |||
stroke = args["stroke-color"] or args["stroke-colour"] or "#ff0000", | |||
["stroke-width"] = args["stroke-width"] or "6" | |||
} | |||
end | end | ||
data.properties.title = args.title or mw.getCurrentFrame():getParent():getTitle() | |||
if args.description then | |||
data.properties.description = args.description | |||
end | |||
return mw.text.jsonEncode(data) | return mw.text.jsonEncode(data) |
Revision as of 09:48, 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 = { display = 'inline' } 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 if contentIndex == '' then contentIndex = 1 end content[contentIndex] = makeContentJson(contentArgs) contentIndex = contentIndex + 1 end --Single item, no array needed if #content==1 then return content[1] end --Multiple items get placed in a FeatureCollection local contentArray = '{"type": "FeatureCollection", "features": [\n' .. table.concat( content, ',\n') .. '\n] }' return contentArray end function makeCoordsText(args) local coords local frame = mw.getCurrentFrame() if args.coord then coords = args.coord else coords = frame:preprocess('{{WikidataCoord|display=|'..(args.id or args.ids or mw.wikibase.getEntityIdForCurrentPage())..'}}') end return frame:preprocess('[{{#invoke:coordinates|coord2text|'..coords..'|long}}, {{#invoke:coordinates|coord2text|'..coords..'|lat}}]') end function makeContentJson(args) local data = {} if args.type == 'point' then data.type = "Feature" data.geometry = { type = "Point", coordinates = makeCoordsText(args) } data.properties = { title = args.title or mw.getCurrentFrame():getParent():getTitle(), ["marker-symbol"] = args.marker or "marker", ["marker-color"] = "5E74F3" } else data.type = "ExternalData" if args.type == "data" or args.from then data.service = "page" elseif args.type == "line" then data.service = "geoline" elseif args.type == "shape" then data.service = "geoshape" elseif args.type == "shape-inverse" then data.service = "geomask" end if args.id or args.ids or (not (args.from) and mw.wikibase.getEntityIdForCurrentPage()) then data.ids = args.id or args.ids or mw.wikibase.getEntityIdForCurrentPage() else data.title = args.from end data.properties = { stroke = args["stroke-color"] or args["stroke-colour"] or "#ff0000", ["stroke-width"] = args["stroke-width"] or "6" } end data.properties.title = args.title or mw.getCurrentFrame():getParent():getTitle() if args.description then data.properties.description = args.description end 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 or defaults.display, '%s*,%s*') local displayInTitle = display[1] == 'title' or display[2] == 'title' local displayInline = display[1] == 'inline' or display[2] == 'inline' local output if displayInTitle and displayInline then output = makeTitleOutput(Args, tagContent) .. makeInlineOutput(Args, tagContent) elseif displayInTitle then output = makeTitleOutput(Args, tagContent) elseif displayInline then output = makeInlineOutput(Args, tagContent) else error( 'Invalid display parameter') end return output -- temporary for testing, to see the JSON being produce -- return frame:preprocess(output) end return p