Module:Glossary
Appearance
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