Jump to content

Module:Glossary: Difference between revisions

From Insurer Brain
Content deleted Content added
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..."
 
No edit summary
Line 16: Line 16:
if not seen[title] then
if not seen[title] then
seen[title] = true
seen[title] = true
local page = mw.title.new(title)
-- look up the Definition: namespace version
local defTitle = "Definition:" .. title
local page = mw.title.new(defTitle)
if page and page.exists then
if page and page.exists then
local text = page:getContent() or ""
local text = page:getContent() or ""
-- strip templates and clean up
-- strip templates, get first sentence
text = text:gsub("%b{}", "")
text = text:gsub("%b{}", "")
text = text:gsub("^%s+", "")
text = text:gsub("^%s+", "")
local sentence = text:match("^(.-%.)")
-- collect up to 3 sentences
if sentence then
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, {
table.insert(entries, {
title = title,
title = title,
summary = table.concat(sentences, " ")
summary = sentence
})
})
end
end

Revision as of 20:54, 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 ""
    
    -- 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
            
            -- look up the Definition: namespace version
            local defTitle = "Definition:" .. title
            local page = mw.title.new(defTitle)
            
            if page and page.exists then
                local text = page:getContent() or ""
                -- strip templates, get first sentence
                text = text:gsub("%b{}", "")
                text = text:gsub("^%s+", "")
                local sentence = text:match("^(.-%.)")
                if sentence then
                    table.insert(entries, {
                        title = title,
                        summary = sentence
                    })
                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