Module:ConceptRelations7

From No Subject
Jump to navigation Jump to search

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

local p = {}

-- Normalize type strings into CSS-safe tokens
local function normalizeType(typeStr)
    if not typeStr then return "" end
    return typeStr:lower():gsub("%s+", "-"):gsub("[/,]", "-")
end

-- Create wikilink
local function makeLink(target)
    if not target or target == "" then return "" end
    local cleaned = target:gsub("%[%[", ""):gsub("%]%]", "")
    return "[[" .. cleaned .. "]]"
end

-- ================= CONTRIBUTORS =================

function p.contributors(frame)
    local args = frame:getParent().args
    local out = {}

    for i = 1, 20 do
        local name = args["contributor_" .. i .. "_name"]
        if not name or name == "" then break end

        local year = args["contributor_" .. i .. "_year"]
        local ctype = args["contributor_" .. i .. "_type"]
        local content = args["contributor_" .. i .. "_content"]

        local norm = normalizeType(ctype or "")

        table.insert(out, '<div class="contributor-block contributor-' .. norm .. '">')
        table.insert(out, '<div class="contributor-name">' .. makeLink(name) ..
            (ctype and ctype ~= "" and (' <span class="contributor-type-tag">' .. ctype .. '</span>') or '') ..
            '</div>')

        if year and year ~= "" then
            table.insert(out, '<div class="contributor-year">' .. year .. '</div>')
        end

        if content and content ~= "" then
            table.insert(out, '<div class="contributor-content">' .. content .. '</div>')
        end

        table.insert(out, '</div>')
    end

    return table.concat(out, "\n")
end

-- ================= RELATIONS (forward + tracking) =================

function p.relations(frame)
    local args = frame:getParent().args
    local out = {}
    local page = mw.title.getCurrentTitle().text

    for i = 1, 50 do
        local target = args["relation_" .. i .. "_target"]
        if not target or target == "" then break end

        local rtype = args["relation_" .. i .. "_type"]
        local content = args["relation_" .. i .. "_content"]
        local norm = normalizeType(rtype or "")

        local cleanTarget = target:gsub("%[%[", ""):gsub("%]%]", "")

        -- hidden tracking category for inverse lookup
        table.insert(out,
            '[[Category:Concept relations to ' .. cleanTarget .. '|' .. page .. ']]'
        )

        table.insert(out, '<div class="relation-block relation-' .. norm .. '">')
        table.insert(out, '<div class="relation-target">' .. makeLink(target) ..
            (rtype and rtype ~= "" and (' <span class="relation-type-tag">' .. rtype .. '</span>') or '') ..
            '</div>')

        if content and content ~= "" then
            table.insert(out, '<div class="relation-content">' .. content .. '</div>')
        end

        table.insert(out, '</div>')
    end

    return table.concat(out, "\n")
end

-- ================= BACKLINKS (inverse relations) =================
-- Uses CategoryTree to render members of:
-- Category:Concept relations to <CurrentPage>

function p.backlinks(frame)
    local title = mw.title.getCurrentTitle().text
    local cat = "Concept relations to " .. title

    local count = mw.site.stats.pagesInCategory(cat)
    if not count or count == 0 then
        return ""
    end

    return table.concat({
        '<div class="concept-backlinks">',
        '<div class="concept-backlinks-header">Referenced by</div>',
        '{{#categorytree:Category:' .. cat .. '|mode=pages|depth=1}}',
        '</div>'
    }, "\n")
end

return p