Module:SemanticAnnotations: Difference between revisions
// via Wikitext Extension for VSCode |
// via Wikitext Extension for VSCode |
||
| Line 13: | Line 13: | ||
--[[ | --[[ | ||
Generates semantic annotations using SMW's #set parser function. | Generates semantic annotations using SMW's #set parser function. | ||
This function now handles both legacy string mappings and new complex mappings. | |||
@param args - Template parameters table | @param args - Template parameters table | ||
@param mappings - Property to parameter mappings: {["Property"] = "param_name", ...} | @param mappings - Property to parameter mappings in any of these formats: | ||
1. Simple string: {["Property"] = "param_name"} | |||
2. Object with param: {["Property"] = {param = "param_name"}} | |||
3. Object with multiple mappings: {["Property"] = {mappings = [{param = "p1", metadata = {...}}, ...]}} | |||
@param options - Configuration options: | @param options - Configuration options: | ||
{ | { | ||
| Line 28: | Line 32: | ||
]] | ]] | ||
function p.generateAnnotations(args, mappings, options) | function p.generateAnnotations(args, mappings, options) | ||
-- For complex mappings, just delegate to generateEnhancedAnnotations | |||
if mappings and type(mappings) == "table" then | |||
for _, mapping in pairs(mappings) do | |||
if type(mapping) == "table" then | |||
-- Found at least one complex mapping, use the enhanced function | |||
return p.generateEnhancedAnnotations(args, mappings, options) | |||
end | |||
end | |||
end | |||
-- If we got here, all mappings are simple string mappings | |||
args = args or {} | args = args or {} | ||
mappings = mappings or {} | mappings = mappings or {} | ||
| Line 53: | Line 68: | ||
local propertyCount = 0 | local propertyCount = 0 | ||
-- Handle regular property mappings | -- Handle regular property mappings (legacy string-to-string format) | ||
for property, param in pairs(mappings) do | for property, param in pairs(mappings) do | ||
local fullPropertyName = prefix .. property | -- Only process string params (skip tables which are handled by enhanced function) | ||
if type(param) == "string" then | |||
local fullPropertyName = prefix .. property | |||
local value = args[param] | |||
-- Apply transform if one exists for this property | |||
if value and transform[property] then | |||
value = transform[property](value) | |||
end | |||
-- Use the value if it exists, otherwise use default if provided | |||
if value and value ~= "" then | |||
table.insert(result, string.format(' |%s=%s', fullPropertyName, value)) | |||
propertyCount = propertyCount + 1 | |||
elseif default[property] then | |||
table.insert(result, string.format(' |%s=%s', fullPropertyName, default[property])) | |||
propertyCount = propertyCount + 1 | |||
end | |||
end | end | ||
-- No need for goto continue - just continue the loop naturally | |||
end | end | ||