Module:TemplateHelpers: Difference between revisions

// via Wikitext Extension for VSCode
// via Wikitext Extension for VSCode
Line 376: Line 376:
         semanticOutput
         semanticOutput
     )
     )
end
-- Helper function to process additional properties with multi-value support
-- This standardizes how additional properties are handled across templates
function p.processAdditionalProperties(args, semanticConfig, semanticOutput, skipProperties)
    if not semanticConfig or not semanticConfig.additionalProperties then
        return semanticOutput
    end
   
    skipProperties = skipProperties or {}
   
    for property, sourceFields in pairs(semanticConfig.additionalProperties) do
        -- Skip properties that are handled separately
        if not skipProperties[property] then
            for _, fieldName in ipairs(sourceFields) do
                if args[fieldName] and args[fieldName] ~= "" then
                    local value = args[fieldName]
                   
                    -- Apply transformation if available
                    if semanticConfig.transforms and semanticConfig.transforms[property] then
                        value = semanticConfig.transforms[property](value)
                    end
                   
                    -- Check if this is a multi-value field that needs to be split
                    if p.isMultiValueField(value) then
                        -- Use the generic multi-value function
                        semanticOutput = p.addMultiValueSemanticProperties(
                            value,
                            property,
                            semanticConfig.transforms and semanticConfig.transforms[property],
                            semanticOutput
                        )
                    else
                        -- Single value property
                        if mw.smw then
                            mw.smw.set({[property] = value})
                        else
                            semanticOutput = semanticOutput .. "\n" ..
                                '<div style="display:none;">\n  {{#set: ' .. property .. '=' ..
                                value .. ' }}\n</div>'
                        end
                    end
                end
            end
        end
    end
   
    return semanticOutput
end
-- Helper function to check if a field contains multiple values
function p.isMultiValueField(value)
    if not value or value == "" then return false end
   
    -- Check for common multi-value delimiters
    return value:match(";") or value:match("%s+and%s+")
end
end


Line 412: Line 468:
     end
     end
      
      
     -- Handle multiple values
     -- Process additional properties with multi-value support
     if semanticConfig.additionalProperties then
     local skipProperties = options.skipProperties or {}
        for property, sourceFields in pairs(semanticConfig.additionalProperties) do
    semanticOutput = p.processAdditionalProperties(args, semanticConfig, semanticOutput, skipProperties)
            for _, fieldName in ipairs(sourceFields) do
                if args[fieldName] and args[fieldName] ~= "" then
                    local value = args[fieldName]
                   
                    -- Apply transformation if available
                    if semanticConfig.transforms and semanticConfig.transforms[property] then
                        value = semanticConfig.transforms[property](value)
                    end
                   
                    if mw.smw then
                        mw.smw.set({[property] = value})
                    else
                        semanticOutput = semanticOutput .. "\n" ..
                            '<div style="display:none;">\n  {{#set: ' .. property .. '=' ..
                            value .. ' }}\n</div>'
                    end
                end
            end
        end
    end
      
      
     -- Handle multi-value fields that need to be split
     -- Handle multi-value fields that need to be split