Module:ThinkerRelations

From No Subject
Jump to navigation Jump to search

Documentation for this module may be created at Module:ThinkerRelations/doc

local p = {}
local getArgs = require('Module:Arguments').getArgs

function p.main(frame)
    local args = getArgs(frame)
    local root = mw.html.create('div')
    
    -- CONFIG: CSS Classes
    local container_class = 'thinker-relations-container'
    local block_class = 'relation-block'
    local header_class = 'relation-header'
    local type_class = 'relation-type-tag'
    local content_class = 'relation-content'

    -- 1. Check if we have any relations to display
    local has_relations = false
    for k, v in pairs(args) do
        if string.find(k, '^relation_%d+_target$') then
            has_relations = true
            break
        end
    end

    if not has_relations then return '' end

    -- 2. Add Section Header
    root:tag('div')
        :addClass('thinker-section-header')
        :wikitext('Critical Network')

    -- 3. Loop through relations (1 to 50)
    for i = 1, 50 do
        local target = args['relation_' .. i .. '_target']
        local content = args['relation_' .. i .. '_content']
        local rel_type = args['relation_' .. i .. '_type']
        
        if target then
            -- SUGGESTION 1: Clean brackets from target input to prevent [[[[Links]]]]
            local clean_target = target:gsub('%[%[', ''):gsub('%]%]', '')
            
            -- BUILD THE HTML CARD
            local block = root:tag('div'):addClass(block_class)
            
            -- Header (Target + Type Tag)
            local header = block:tag('div'):addClass(header_class)
            header:wikitext('To [[' .. clean_target .. ']]')
            
            if rel_type then
                header:tag('span')
                    :addClass(type_class)
                    :wikitext(rel_type)
                    
                -- SUGGESTION 2: Case-insensitive color logic
                local type_lower = rel_type:lower()
                if type_lower == 'critique' then 
                    block:css('border-left-color', '#e74c3c') -- Red
                elseif type_lower == 'influence' then 
                    block:css('border-left-color', '#2ecc71') -- Green
                elseif type_lower == 'clinical' then
                    block:css('border-left-color', '#3498db') -- Blue
                end
            end
            
            -- Content
            if content then
                block:tag('div')
                    :addClass(content_class)
                    :wikitext(content)
            end

            -- STORE THE SEMANTIC DATA (Uncomment if SMW is installed)
            -- local subobject_data = {
            --    ['Has relation to'] = clean_target,
            --    ['Has relation type'] = rel_type or 'General',
            --    ['Has description'] = content
            -- }
            -- mw.smw.subobject(subobject_data)
        end
    end

    return tostring(root)
end

return p