Module:Glossary: Difference between revisions
Appearance
Content deleted Content added
No edit summary |
No edit summary |
||
| (2 intermediate revisions by the same user not shown) | |||
| Line 3: | Line 3: | ||
function p.generate(frame) |
function p.generate(frame) |
||
local maxEntries = tonumber(frame.args[1]) or 30 |
|||
local current = mw.title.getCurrentTitle() |
local current = mw.title.getCurrentTitle() |
||
local content = current:getContent() or "" |
local content = current:getContent() or "" |
||
| Line 8: | Line 9: | ||
local seen = {} |
local seen = {} |
||
local entries = {} |
local entries = {} |
||
local |
local count = 0 |
||
for link in content:gmatch("%[%[([^%]|]+)") do |
for link in content:gmatch("%[%[([^%]|]+)") do |
||
| ⚫ | |||
| ⚫ | |||
| ⚫ | |||
| ⚫ | |||
local title = link:match("^(.-)#") or link |
local title = link:match("^(.-)#") or link |
||
title = mw.text.trim(title) |
title = mw.text.trim(title) |
||
| Line 17: | Line 22: | ||
seen[title] = true |
seen[title] = true |
||
local defTitle |
local defTitle |
||
if title:match("^Definition:") then |
|||
defTitle = title |
|||
| ⚫ | |||
| ⚫ | |||
| ⚫ | |||
table.insert(debug, "NOT FOUND: " .. defTitle) |
|||
else |
else |
||
| ⚫ | |||
| ⚫ | |||
| ⚫ | |||
| ⚫ | |||
local text = page:getContent() |
local text = page:getContent() |
||
if |
if text then |
||
table.insert(debug, "NO CONTENT: " .. defTitle) |
|||
| ⚫ | |||
text = text:gsub("%b{}", "") |
text = text:gsub("%b{}", "") |
||
text = text:gsub("^%s+", "") |
text = text:gsub("^%s+", "") |
||
local sentence = text:match("^(.-%.)") |
local sentence = text:match("^(.-%.)") |
||
if |
if sentence then |
||
table.insert(debug, "NO SENTENCE: " .. defTitle) |
|||
| ⚫ | |||
| ⚫ | |||
table.insert(entries, { |
table.insert(entries, { |
||
title = title, |
title = title:gsub("^Definition:", ""), |
||
summary = sentence |
summary = sentence |
||
}) |
}) |
||
| ⚫ | |||
end |
end |
||
end |
end |
||
| Line 51: | Line 53: | ||
local out = {} |
local out = {} |
||
| ⚫ | |||
-- debug output (remove once working) |
|||
table.insert(out, "=== DEBUG ===") |
|||
for _, d in ipairs(debug) do |
|||
table.insert(out, d) |
|||
| ⚫ | |||
table.insert(out, "=== END DEBUG ===") |
|||
for _, e in ipairs(entries) do |
for _, e in ipairs(entries) do |
||
table.insert(out, |
table.insert(out, e.summary) |
||
"'''%s''': %s", e.title, e.summary |
|||
| ⚫ | |||
end |
end |
||
Latest revision as of 21:03, 12 April 2026
Documentation for this module may be created at Module:Glossary/doc
-- Module:AutoGlossary
local p = {}
function p.generate(frame)
local maxEntries = tonumber(frame.args[1]) or 30
local current = mw.title.getCurrentTitle()
local content = current:getContent() or ""
local seen = {}
local entries = {}
local count = 0
for link in content:gmatch("%[%[([^%]|]+)") do
if count >= maxEntries then
break
end
local title = link:match("^(.-)#") or link
title = mw.text.trim(title)
if not seen[title] then
seen[title] = true
local defTitle
if title:match("^Definition:") then
defTitle = title
else
defTitle = "Definition:" .. title
end
local page = mw.title.new(defTitle)
if page then
local text = page:getContent()
if text then
text = text:gsub("%b{}", "")
text = text:gsub("^%s+", "")
local sentence = text:match("^(.-%.)")
if sentence then
table.insert(entries, {
title = title:gsub("^Definition:", ""),
summary = sentence
})
count = count + 1
end
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, e.summary)
end
return table.concat(out, "\n\n")
end
return p