Module:ConceptRelations3

From No Subject
Jump to navigation Jump to search

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

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

function p.main(frame)
    local args = getArgs(frame)
    local root = mw.html.create('div')
    
    -- Check if there are any relationships
    local has_relations = false
    for i = 1, 50 do
        if args['relation_' .. i .. '_target'] then
            has_relations = true
            break
        end
    end
    if not has_relations then return '' end

    -- Relationship type to color mapping
    local type_colors = {
        presupposes = '#3498db',           -- Blue
        ['contrasts with'] = '#e74c3c',    -- Red
        ['manifests in'] = '#2ecc71',      -- Green
        ['transformed by'] = '#9b59b6',    -- Purple
        ['related to'] = '#95a5a6',        -- Gray
        enables = '#1abc9c',               -- Teal
        derived = '#3498db'                -- Blue (same as presupposes)
    }

    -- Count total relationships for display management
    local total_relations = 0
    for i = 1, 50 do
        if args['relation_' .. i .. '_target'] then
            total_relations = total_relations + 1
        else
            break
        end
    end

    -- Build relationship blocks
    local relations_shown = 0
    local max_initial_display = 5  -- Show first 5, collapse rest
    
    for i = 1, 50 do
        local target = args['relation_' .. i .. '_target']
        if not target then break end

        local rel_type = args['relation_' .. i .. '_type']
        local content = args['relation_' .. i .. '_content']
        
        -- Create main block
        local block = root:tag('div'):addClass('relation-block')
        
        -- Add collapse class if beyond initial display limit
        relations_shown = relations_shown + 1
        if relations_shown > max_initial_display then
            block:addClass('relation-collapsed')
        end
        
        -- Determine border color based on relationship type
        if rel_type then
            local type_lower = mw.ustring.lower(rel_type)
            local color_found = false
            
            for keyword, color in pairs(type_colors) do
                if mw.ustring.find(type_lower, keyword, 1, true) then
                    block:css('border-left-color', color)
                    color_found = true
                    break
                end
            end
            
            -- Default color if no match
            if not color_found then
                block:css('border-left-color', '#95a5a6')
            end
        end
        
        -- Clean target (remove wikilink brackets if present)
        local clean_target = mw.ustring.gsub(target, '%[%[', '')
        clean_target = mw.ustring.gsub(clean_target, '%]%]', '')
        
        -- Header with relationship type and target
        local header = block:tag('div')
            :addClass('relation-header')
            :attr('role', 'heading')
            :attr('aria-level', '3')
        
        if rel_type then
            header:tag('span')
                :addClass('relation-type')
                :wikitext(rel_type .. ': ')
        end
        
        header:wikitext('[[' .. clean_target .. ']]')
        
        -- Content/explanation
        if content then
            block:tag('div')
                :addClass('relation-content')
                :wikitext(content)
        end
        
        -- Set semantic properties for queryable relationships
        if rel_type then
            local type_lower = mw.ustring.lower(rel_type)
            if mw.ustring.find(type_lower, 'presupposes') then
                block:wikitext('[[Presupposes concept::' .. clean_target .. ']]')
            elseif mw.ustring.find(type_lower, 'contrasts') then
                block:wikitext('[[Contrasts with concept::' .. clean_target .. ']]')
            elseif mw.ustring.find(type_lower, 'manifests') then
                block:wikitext('[[Manifests in::' .. content .. ']]')
            end
        end
    end
    
    -- Add "Show more relationships" toggle if needed
    if total_relations > max_initial_display then
        local toggle = root:tag('div')
            :addClass('relation-toggle')
            :attr('data-toggle', 'relations')
        toggle:tag('span')
            :addClass('toggle-text')
            :wikitext('[+' .. (total_relations - max_initial_display) .. ' more relationships]')
        toggle:tag('span')
            :addClass('toggle-icon')
            :wikitext('▼')
    end

    return tostring(root)
end

return p