Module:SemanticAnnotations: Difference between revisions

// via Wikitext Extension for VSCode
// via Wikitext Extension for VSCode
Line 368: Line 368:
                       2. Object: {["Property"] = {param = "param_name"}}
                       2. Object: {["Property"] = {param = "param_name"}}
                       3. Complex: {["Property"] = {mappings = {{param = "p1", metadata = {...}}, ...}}}
                       3. Complex: {["Property"] = {mappings = {{param = "p1", metadata = {...}}, ...}}}
                      4. Subobject: {["Property"] = {is_subobject = true, properties = {property map}, id_prefix = "optional_prefix"}}
     @param options  - Configuration options (same as generateAnnotations)
     @param options  - Configuration options (same as generateAnnotations)
      
      
Line 386: Line 387:
     -- Build the property table for mw.smw.set
     -- Build the property table for mw.smw.set
     local properties = {}
     local properties = {}
    local semanticOutput = ""
      
      
     -- Process all mappings
     -- Process all mappings
Line 396: Line 398:
             processSimpleMapping(properties, fullPropertyName, args[mapping], transform[property], default[property])
             processSimpleMapping(properties, fullPropertyName, args[mapping], transform[property], default[property])
         elseif type(mapping) == "table" then
         elseif type(mapping) == "table" then
             if mapping.param then
             if mapping.is_subobject then
                -- This is a special subobject definition
               
                -- Get the subobject properties map
                local subobjectProperties = mapping.properties or {}
               
                -- Create the actual properties map by processing each property value
                local actualProperties = {}
               
                -- Process each property definition in the subobject
                for subPropName, subPropValue in pairs(subobjectProperties) do
                    if type(subPropValue) == "table" and subPropValue.param then
                        -- Object with param reference
                        local paramName = subPropValue.param
                       
                        -- Get the value from args
                        if args[paramName] and args[paramName] ~= "" then
                            local value = args[paramName]
                           
                            -- Apply transform if one exists for this property
                            if subPropValue.transform and type(subPropValue.transform) == "function" then
                                value = subPropValue.transform(value)
                            end
                           
                            -- Set the property
                            actualProperties[subPropName] = value
                        end
                    elseif type(subPropValue) == "string" then
                        -- Simple string mapping or static value
                        if args[subPropValue] and args[subPropValue] ~= "" then
                            -- It's a parameter reference
                            actualProperties[subPropName] = args[subPropValue]
                        else
                            -- It's a static value
                            actualProperties[subPropName] = subPropValue
                        end
                    end
                end
               
                -- If we have at least one property to set
                if next(actualProperties) then
                    -- Generate a reasonably unique ID for the subobject
                    local idPrefix = mapping.id_prefix or "subobj"
                    local idValue = ""
                   
                    -- Try to use primary property value as part of the ID
                    local primaryProp = mapping.primary_property
                    if primaryProp and actualProperties[primaryProp] then
                        -- Clean the value to use in an ID
                        idValue = tostring(actualProperties[primaryProp]):gsub("[^%w]", "_")
                    else
                        -- Just use an incremental number if no primary property
                        idValue = tostring(os.time() % 10000) .. "_" .. math.random(1000, 9999)
                    end
                   
                    -- Create the full ID
                    local subobjectId = idPrefix .. "_" .. idValue
                   
                    -- Create the subobject
                    if mw.smw then
                        -- Use native SMW API
                        local subobjectResult = mw.smw.subobject({
                            id = subobjectId,      -- Use our generated ID
                            properties = actualProperties
                        })
                       
                        -- If there was an error, append it to output for debugging
                        if type(subobjectResult) == "table" and subobjectResult.error then
                            semanticOutput = semanticOutput .. "\n<!-- SMW Subobject Error: " ..
                                            tostring(subobjectResult.error) .. " -->"
                        end
                    else
                        -- Parser function fallback
                        semanticOutput = semanticOutput .. "\n" ..
                                        generateSmwSubobjectFragment(actualProperties, subobjectId)
                    end
                end
            elseif mapping.param then
                 -- Single mapping with object structure
                 -- Single mapping with object structure
                 processSimpleMapping(properties, fullPropertyName, args[mapping.param], transform[property], default[property])
                 processSimpleMapping(properties, fullPropertyName, args[mapping.param], transform[property], default[property])
Line 413: Line 492:
         end)
         end)
          
          
         -- If successful, return empty string (properties are set behind the scenes)
         -- If successful, return semanticOutput (which might contain subobject results)
         -- If failed, fall back to parser function approach
         -- If failed, fall back to parser function approach
         if success then
         if success then
             return ""
             return semanticOutput
         else
         else
             return p.generateEnhancedAnnotations(args, mappings, options)
             return p.generateEnhancedAnnotations(args, mappings, options) .. semanticOutput
         end
         end
     end
     end
      
      
     return ""
     return semanticOutput
end
end
-- Helper function to generate #subobject parser function with an ID
function p.generateSmwSubobjectFragment(properties, id)
    local result = '<div style="display:none;">\n  {{#subobject:'
   
    -- Set the ID if available
    if id and id ~= "" then
        result = result .. "|@" .. id
    end
   
    for propName, propValue in pairs(properties) do
        if propValue and propValue ~= "" then
            result = result .. "\n    |" .. propName .. "=" .. propValue
        end
    end
   
    result = result .. "\n  }}\n</div>"
    return result
end
-- For backward compatibility with local reference
generateSmwSubobjectFragment = p.generateSmwSubobjectFragment


return p
return p