Module:DailyFeatured: Difference between revisions
Appearance
Per.kuskan (talk | contribs) No edit summary |
No edit summary |
||
| Line 7: | Line 7: | ||
for line in mw.text.gsplit(content or "", "\n", true) do | for line in mw.text.gsplit(content or "", "\n", true) do | ||
line = mw.text.trim(line) | line = mw.text.trim(line) | ||
line = line:gsub("<!%-%-.-%-%->", "") | |||
line = mw.text.trim(line) | |||
if line ~= "" then | if line ~= "" then | ||
line = line:gsub("^%*%s*", "") | line = line:gsub("^%*%s*", "") | ||
line = mw.text.trim(line) | |||
local linkedTitle = line:match("^%[%[([^%]|]+)|?.-%]%]$") | |||
if linkedTitle then | |||
line = linkedTitle | |||
end | |||
if line ~= "" then | if line ~= "" then | ||
table.insert(pages, line) | table.insert(pages, line) | ||
| Line 22: | Line 33: | ||
return "" | return "" | ||
end | end | ||
s = s:gsub("<!--.- | |||
s = s:gsub("<!%-%-.-%-%->", "") | |||
s = s:gsub("<ref[^>/]-/>", "") | |||
s = s:gsub("<ref.->.-</ref>", "") | |||
s = s:gsub("<ref.->", "") | |||
s = s:gsub("%[%[File:.-%]%]", "") | s = s:gsub("%[%[File:.-%]%]", "") | ||
s = s:gsub("%[%[Image:.-%]%]", "") | |||
s = s:gsub("%{%b{}%}", "") | s = s:gsub("%{%b{}%}", "") | ||
s = s:gsub(" | s = s:gsub("'''", "") | ||
s = s:gsub(" | s = s:gsub("''", "") | ||
s = s:gsub("\r", ""):gsub("[ \t]+", " ") | s = s:gsub("%[%[([^%]|]+)|([^%]]+)%]%]", "%2") | ||
s = s:gsub("%[%[([^%]]+)%]%]", "%1") | |||
s = s:gsub("\r", "") | |||
s = s:gsub("[ \t]+", " ") | |||
return mw.text.trim(s) | return mw.text.trim(s) | ||
end | |||
local function shorten(s) | |||
if mw.ustring.len(s) > 200 then | |||
return mw.ustring.sub(s, 1, 197) .. "…" | |||
end | |||
return s | |||
end | end | ||
| Line 36: | Line 64: | ||
end | end | ||
for line in mw.text.gsplit(wikitext, "\n", true) do | for line in mw.text.gsplit(wikitext, "\n", true) do | ||
local raw = line | local raw = line | ||
local cleaned = cleanWikitext(line) | local cleaned = cleanWikitext(line) | ||
if raw:match("^==+%s*[^=].-==+%s*$") then | if raw:match("^==+%s*[^=].-==+%s*$") then | ||
break | break | ||
| Line 48: | Line 74: | ||
if cleaned ~= "" | if cleaned ~= "" | ||
and not cleaned:match("^__%u+__$") | and not cleaned:match("^__%u+__$") | ||
and not cleaned:match("^%[%[ | and not cleaned:match("^%[%[[Cc]ategory:") then | ||
return shorten(cleaned) | |||
end | end | ||
end | end | ||
for line in mw.text.gsplit(wikitext, "\n", true) do | for line in mw.text.gsplit(wikitext, "\n", true) do | ||
local cleaned = cleanWikitext(line) | local cleaned = cleanWikitext(line) | ||
if cleaned:match("^==+%s*[^=].-==+%s*$") then | if cleaned:match("^==+%s*[^=].-==+%s*$") then | ||
cleaned = cleaned:gsub("^=+%s*", ""):gsub("%s*=+$", "") | cleaned = cleaned:gsub("^=+%s*", ""):gsub("%s*=+$", "") | ||
cleaned = mw.text.trim(cleaned) | cleaned = mw.text.trim(cleaned) | ||
if cleaned ~= "" then | if cleaned ~= "" then | ||
return cleaned | return shorten(cleaned) | ||
end | end | ||
end | end | ||
end | end | ||
for line in mw.text.gsplit(wikitext, "\n", true) do | for line in mw.text.gsplit(wikitext, "\n", true) do | ||
local cleaned = cleanWikitext(line) | local cleaned = cleanWikitext(line) | ||
if cleaned ~= "" | if cleaned ~= "" | ||
and not cleaned:match("^__%u+__$") | and not cleaned:match("^__%u+__$") | ||
and not cleaned:match("^%[%[ | and not cleaned:match("^%[%[[Cc]ategory:") then | ||
return shorten(cleaned) | |||
end | end | ||
end | end | ||
| Line 86: | Line 107: | ||
function p.main(frame) | function p.main(frame) | ||
local pages = getPool() | local pages = getPool() | ||
if #pages == 0 then | if #pages == 0 then | ||
return "No featured pages defined." | return "No featured pages defined." | ||
end | end | ||
local day = tonumber(os.date("%j")) | local day = tonumber(os.date("%j")) or 1 | ||
local index = (day % #pages) + 1 | local index = ((day - 1) % #pages) + 1 | ||
local title = pages[index] | local title = pages[index] | ||
local args = (frame and frame.args) or {} | local args = (frame and frame.args) or {} | ||
if args.titleonly == "1" then | if args.titleonly == "1" then | ||
return title | return title | ||
Revision as of 09:50, 14 May 2026
local p = {}
local function getPool()
local content = mw.getCurrentFrame():expandTemplate{ title = "Featured pool" }
local pages = {}
for line in mw.text.gsplit(content or "", "\n", true) do
line = mw.text.trim(line)
line = line:gsub("<!%-%-.-%-%->", "")
line = mw.text.trim(line)
if line ~= "" then
line = line:gsub("^%*%s*", "")
line = mw.text.trim(line)
local linkedTitle = line:match("^%[%[([^%]|]+)|?.-%]%]$")
if linkedTitle then
line = linkedTitle
end
if line ~= "" then
table.insert(pages, line)
end
end
end
return pages
end
local function cleanWikitext(s)
if not s or s == "" then
return ""
end
s = s:gsub("<!%-%-.-%-%->", "")
s = s:gsub("<ref[^>/]-/>", "")
s = s:gsub("<ref.->.-</ref>", "")
s = s:gsub("<ref.->", "")
s = s:gsub("%[%[File:.-%]%]", "")
s = s:gsub("%[%[Image:.-%]%]", "")
s = s:gsub("%{%b{}%}", "")
s = s:gsub("'''", "")
s = s:gsub("''", "")
s = s:gsub("%[%[([^%]|]+)|([^%]]+)%]%]", "%2")
s = s:gsub("%[%[([^%]]+)%]%]", "%1")
s = s:gsub("\r", "")
s = s:gsub("[ \t]+", " ")
return mw.text.trim(s)
end
local function shorten(s)
if mw.ustring.len(s) > 200 then
return mw.ustring.sub(s, 1, 197) .. "…"
end
return s
end
local function extractHeadlineFromContent(wikitext)
if not wikitext or wikitext == "" then
return ""
end
for line in mw.text.gsplit(wikitext, "\n", true) do
local raw = line
local cleaned = cleanWikitext(line)
if raw:match("^==+%s*[^=].-==+%s*$") then
break
end
if cleaned ~= ""
and not cleaned:match("^__%u+__$")
and not cleaned:match("^%[%[[Cc]ategory:") then
return shorten(cleaned)
end
end
for line in mw.text.gsplit(wikitext, "\n", true) do
local cleaned = cleanWikitext(line)
if cleaned:match("^==+%s*[^=].-==+%s*$") then
cleaned = cleaned:gsub("^=+%s*", ""):gsub("%s*=+$", "")
cleaned = mw.text.trim(cleaned)
if cleaned ~= "" then
return shorten(cleaned)
end
end
end
for line in mw.text.gsplit(wikitext, "\n", true) do
local cleaned = cleanWikitext(line)
if cleaned ~= ""
and not cleaned:match("^__%u+__$")
and not cleaned:match("^%[%[[Cc]ategory:") then
return shorten(cleaned)
end
end
return ""
end
function p.main(frame)
local pages = getPool()
if #pages == 0 then
return "No featured pages defined."
end
local day = tonumber(os.date("%j")) or 1
local index = ((day - 1) % #pages) + 1
local title = pages[index]
local args = (frame and frame.args) or {}
if args.titleonly == "1" then
return title
end
local page = mw.title.new(title)
local headline = ""
if page and page.exists then
local content = page:getContent() or ""
headline = extractHeadlineFromContent(content)
end
if headline == "" then
headline = "Featured page for " .. os.date("%d %B %Y") .. "."
end
return string.format(
"'''[[%s]]'''<div style=\"margin-top:0.35em;\"><small>%s</small></div>",
title,
headline
)
end
return p