Jump to content

Module:Glossary: Difference between revisions

From Insurer Brain
Content deleted Content added
No edit summary
No edit summary
Line 8: Line 8:
local seen = {}
local seen = {}
local entries = {}
local entries = {}
local debug = {}
for link in content:gmatch("%[%[([^%]|]+)") do
for link in content:gmatch("%[%[([^%]|]+)") do
Line 17: Line 16:
seen[title] = true
seen[title] = true
local defTitle = "Definition:" .. title
-- only prepend if not already in Definition: namespace
local defTitle
if title:match("^Definition:") then
defTitle = title
else
defTitle = "Definition:" .. title
end
-- clean display name (strip namespace prefix)
local displayName = title:gsub("^Definition:", "")
local page = mw.title.new(defTitle)
local page = mw.title.new(defTitle)
if not page then
if page and page.exists then
table.insert(debug, "FAIL mw.title.new: " .. defTitle)
local text = page:getContent() or ""
elseif not page.exists then
text = text:gsub("%b{}", "")
table.insert(debug, "NOT FOUND: " .. defTitle)
text = text:gsub("^%s+", "")
else
local sentence = text:match("^(.-%.)")
local text = page:getContent()
if sentence then
if not text then
table.insert(entries, {
table.insert(debug, "NO CONTENT: " .. defTitle)
title = displayName,
else
summary = sentence
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
Line 51: Line 49:
local out = {}
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
for _, e in ipairs(entries) do
table.insert(out, string.format(
table.insert(out, string.format(

Revision as of 20:57, 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
            
            -- only prepend if not already in Definition: namespace
            local defTitle
            if title:match("^Definition:") then
                defTitle = title
            else
                defTitle = "Definition:" .. title
            end
            
            -- clean display name (strip namespace prefix)
            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)
    
    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