Jump to content

Module:Glossary

From Insurer Brain
Revision as of 20:56, 12 April 2026 by Wikilah admin (talk | contribs)

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 = {}
    local debug = {}
    
    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 = "Definition:" .. title
            local page = mw.title.new(defTitle)
            
            if not page then
                table.insert(debug, "FAIL mw.title.new: " .. defTitle)
            elseif not page.exists then
                table.insert(debug, "NOT FOUND: " .. defTitle)
            else
                local text = page:getContent()
                if not text then
                    table.insert(debug, "NO CONTENT: " .. defTitle)
                else
                    text = text:gsub("%b{}", "")
                    text = text:gsub("^%s+", "")
                    local sentence = text:match("^(.-%.)")
                    if not sentence then
                        table.insert(debug, "NO SENTENCE: " .. defTitle)
                    else
                        table.insert(debug, "OK: " .. defTitle)
                        table.insert(entries, {
                            title = title,
                            summary = sentence
                        })
                    end
                end
            end
        end
    end
    
    table.sort(entries, function(a, b) 
        return a.title < b.title 
    end)
    
    local out = {}
    
    -- debug output (remove once working)
    table.insert(out, "=== DEBUG ===")
    for _, d in ipairs(debug) do
        table.insert(out, d)
    end
    table.insert(out, "=== END DEBUG ===")
    
    for _, e in ipairs(entries) do
        table.insert(out, string.format(
            "'''%s''': %s", e.title, e.summary
        ))
    end
    
    return table.concat(out, "\n\n")
end

return p