Jump to content

Module:Glossary: Difference between revisions

From Insurer Brain
Content deleted Content added
No edit summary
No edit summary
Line 16: Line 16:
seen[title] = true
seen[title] = true
-- only prepend if not already in Definition: namespace
local defTitle
local defTitle
if title:match("^Definition:") then
if title:match("^Definition:") then
Line 24: Line 23:
end
end
-- clean display name (strip namespace prefix)
local displayName = title:gsub("^Definition:", "")
local displayName = title:gsub("^Definition:", "")
Line 48: Line 46:
end)
end)
-- render: just the sentence, no repeated title
local out = {}
local out = {}
for _, e in ipairs(entries) do
for _, e in ipairs(entries) do
table.insert(out, string.format(
table.insert(out, e.summary)
"'''%s''': %s", e.title, e.summary
))
end
end

Revision as of 20:59, 12 April 2026

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

-- Module:AutoGlossary
local p = {}

function p.generate(frame)
    local current = mw.title.getCurrentTitle()
    local content = current:getContent() or ""
    
    local seen = {}
    local entries = {}
    
    for link in content:gmatch("%[%[([^%]|]+)") do
        local title = link:match("^(.-)#") or link
        title = mw.text.trim(title)
        
        if not seen[title] then
            seen[title] = true
            
            local defTitle
            if title:match("^Definition:") then
                defTitle = title
            else
                defTitle = "Definition:" .. title
            end
            
            local displayName = title:gsub("^Definition:", "")
            
            local page = mw.title.new(defTitle)
            
            if page and page.exists then
                local text = page:getContent() or ""
                text = text:gsub("%b{}", "")
                text = text:gsub("^%s+", "")
                local sentence = text:match("^(.-%.)")
                if sentence then
                    table.insert(entries, {
                        title = displayName,
                        summary = sentence
                    })
                end
            end
        end
    end
    
    table.sort(entries, function(a, b) 
        return a.title < b.title 
    end)
    
    -- render: just the sentence, no repeated title
    local out = {}
    for _, e in ipairs(entries) do
        table.insert(out, e.summary)
    end
    
    return table.concat(out, "\n\n")
end

return p