Module:Glossary: Difference between revisions

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
 
(4 intermediate revisions by the same user not shown)
Line 3:
 
function p.generate(frame)
local maxEntries = tonumber(frame.args[1]) or 30
local current = mw.title.getCurrentTitle()
local content = current:getContent() or ""
-- find all [[internal links]]
local seen = {}
local entries = {}
local count = 0
for link in content:gmatch("%[%[([^%]|]+)") do
if count >= 3maxEntries then
break
))end
end
local title = link:match("^(.-)#") or link
title = mw.text.trim(title)
Line 16 ⟶ 21:
if not seen[title] then
seen[title] = true
local page = mw.title.new(title)
iflocal page and page.exists thendefTitle
if local text = pagetitle:getContentmatch("^Definition:") or ""then
--defTitle strip= templates and clean uptitle
text = text:gsub("%b{}", "")else
textdefTitle = text"Definition:gsub("^%s+", "").. title
end
-- collect up to 3 sentences
local sentencespage = {}mw.title.new(defTitle)
if page local count = 0then
forlocal sentencetext in= textpage:gmatchgetContent("([^%.]+%.)") do
if text sentence = mw.text.trim(sentence)then
iftext sentence ~= text:gsub("%b{}", then"")
text = text:gsub("^%s+", end"")
local titlesentence = title,text:match("^(.-%.)")
})if sentence then
table.insert(entries, {
summary title = table.concattitle:gsub(sentences"^Definition:", " "),
breaksummary = sentence
if #sentences > 0 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
Line 47 ⟶ 48:
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, stringe.format(summary)
"'''%s''': %s", e.title, e.summary
))
end