Jump to content

Module:Glossary

From Insurer Brain
Revision as of 20:49, 12 April 2026 by Wikilah admin (talk | contribs) (Created page with "-- Module:AutoGlossary local p = {} function p.generate(frame) local current = mw.title.getCurrentTitle() local content = current:getContent() or "" -- find all internal links 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 p...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

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 ""
    
    -- find all [[internal links]]
    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 page = mw.title.new(title)
            if page and page.exists then
                local text = page:getContent() or ""
                -- strip templates and clean up
                text = text:gsub("%b{}", "")
                text = text:gsub("^%s+", "")
                
                -- collect up to 3 sentences
                local sentences = {}
                local count = 0
                for sentence in text:gmatch("([^%.]+%.)") do
                    sentence = mw.text.trim(sentence)
                    if sentence ~= "" then
                        count = count + 1
                        table.insert(sentences, sentence)
                        if count >= 3 then
                            break
                        end
                    end
                end
                
                if #sentences > 0 then
                    table.insert(entries, {
                        title = title,
                        summary = table.concat(sentences, " ")
                    })
                end
            end
        end
    end
    
    -- sort alphabetically
    table.sort(entries, function(a, b) 
        return a.title < b.title 
    end)
    
    -- render
    local out = {}
    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