Module:DailyFeatured: Difference between revisions
Appearance
No edit summary |
No edit summary |
||
| Line 28: | Line 28: | ||
end | end | ||
local function | local function removeTemplates(s) | ||
local previous | |||
repeat | |||
previous = s | |||
s = s:gsub("%b{}", "") | |||
until s == previous | |||
return s | |||
end | |||
local function cleanLeadWikitext(s) | |||
if not s or s == "" then | if not s or s == "" then | ||
return "" | return "" | ||
| Line 37: | Line 48: | ||
s = s:gsub("<ref.->.-</ref>", "") | s = s:gsub("<ref.->.-</ref>", "") | ||
s = s:gsub("<ref.->", "") | s = s:gsub("<ref.->", "") | ||
s = s:gsub("%[%[ | s = s:gsub("%[%[[Ff]ile:.-%]%]", "") | ||
s = s:gsub("%[%[[Ii]mage:.-%]%]", "") | |||
s = removeTemplates(s) | |||
s = s:gsub("%[%[ | |||
s = | |||
local cleanedLines = {} | |||
for line in mw.text.gsplit(s, "\n", true) do | |||
local trimmed = mw.text.trim(line) | |||
if trimmed ~= "" | |||
and not trimmed:match("^__%u+__$") | |||
and not trimmed:match("^%[%[[Cc]ategory:") | |||
and not trimmed:match("^%s*|") | |||
and not trimmed:match("^%s*}") then | |||
table.insert(cleanedLines, trimmed) | |||
end | |||
end | end | ||
return mw.text.trim(table.concat(cleanedLines, "\n\n")) | |||
end | end | ||
local function | local function extractLeadFromContent(wikitext) | ||
if not wikitext or wikitext == "" then | if not wikitext or wikitext == "" then | ||
return "" | return "" | ||
end | end | ||
local leadLines = {} | |||
for line in mw.text.gsplit(wikitext, "\n", true) do | for line in mw.text.gsplit(wikitext, "\n", true) do | ||
if line:match("^%s*==+%s*[^=].-==+%s*$") then | |||
break | break | ||
end | end | ||
table.insert(leadLines, line) | |||
end | end | ||
return cleanLeadWikitext(table.concat(leadLines, "\n")) | |||
end | end | ||
| Line 201: | Line 164: | ||
local page = mw.title.new(title) | local page = mw.title.new(title) | ||
local | local lead = "" | ||
local image = nil | local image = nil | ||
if page and page.exists then | if page and page.exists then | ||
local content = page:getContent() or "" | local content = page:getContent() or "" | ||
lead = extractLeadFromContent(content) | |||
image = extractImageFromContent(content) | image = extractImageFromContent(content) | ||
end | end | ||
if | if lead == "" then | ||
lead = "Featured page for " .. os.date("%d %B %Y") .. "." | |||
end | end | ||
return { | return { | ||
title = title, | title = title, | ||
lead = lead, | |||
image = image | image = image | ||
} | } | ||
| Line 241: | Line 203: | ||
"'''[[%s]]'''<div style=\"margin-top:0.35em;\"><small>%s</small></div>", | "'''[[%s]]'''<div style=\"margin-top:0.35em;\"><small>%s</small></div>", | ||
data.title, | data.title, | ||
data.lead | |||
) | ) | ||
end | end | ||
| Line 251: | Line 213: | ||
return '<div class="featured-box"><div class="featured-box-title">Featured page</div><div class="featured-box-body">' .. err .. '</div></div>' | return '<div class="featured-box"><div class="featured-box-title">Featured page</div><div class="featured-box-body">' .. err .. '</div></div>' | ||
end | end | ||
local imageBlock = "" | local imageBlock = "" | ||
| Line 265: | Line 218: | ||
if data.image then | if data.image then | ||
imageBlock = string.format( | imageBlock = string.format( | ||
'<div class="featured-box-image">[[%s|frameless| | '<div class="featured-box-image">[[%s|frameless|170x170px|link=%s|%s]]</div>', | ||
data.image, | data.image, | ||
data.title, | data.title, | ||
| Line 278: | Line 231: | ||
'%s' .. | '%s' .. | ||
'<div class="featured-box-text">' .. | '<div class="featured-box-text">' .. | ||
'<div class="featured-box- | '<div class="featured-box-page-title">\'\'\'[[%s]]\'\'\'</div>' .. | ||
'<div class="featured-box- | '<div class="featured-box-lead">%s</div>' .. | ||
'<div class="featured-box- | '<div class="featured-box-open">[[:%s|Open page]]</div>' .. | ||
'</div>' .. | '</div>' .. | ||
'</div>' .. | '</div>' .. | ||
| Line 286: | Line 239: | ||
imageBlock, | imageBlock, | ||
data.title, | data.title, | ||
data.lead, | |||
data.title | |||
) | ) | ||
end | end | ||
return p | return p | ||
Revision as of 09:56, 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 removeTemplates(s)
local previous
repeat
previous = s
s = s:gsub("%b{}", "")
until s == previous
return s
end
local function cleanLeadWikitext(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("%[%[[Ff]ile:.-%]%]", "")
s = s:gsub("%[%[[Ii]mage:.-%]%]", "")
s = removeTemplates(s)
local cleanedLines = {}
for line in mw.text.gsplit(s, "\n", true) do
local trimmed = mw.text.trim(line)
if trimmed ~= ""
and not trimmed:match("^__%u+__$")
and not trimmed:match("^%[%[[Cc]ategory:")
and not trimmed:match("^%s*|")
and not trimmed:match("^%s*}") then
table.insert(cleanedLines, trimmed)
end
end
return mw.text.trim(table.concat(cleanedLines, "\n\n"))
end
local function extractLeadFromContent(wikitext)
if not wikitext or wikitext == "" then
return ""
end
local leadLines = {}
for line in mw.text.gsplit(wikitext, "\n", true) do
if line:match("^%s*==+%s*[^=].-==+%s*$") then
break
end
table.insert(leadLines, line)
end
return cleanLeadWikitext(table.concat(leadLines, "\n"))
end
local function normalizeFileName(value)
if not value or value == "" then
return nil
end
value = mw.text.trim(value)
value = value:gsub("<!%-%-.-%-%->", "")
value = mw.text.trim(value)
if value == "" then
return nil
end
local linkedFile = value:match("%[%[%s*[Ff]ile%s*:%s*([^%]|%]]+)")
or value:match("%[%[%s*[Ii]mage%s*:%s*([^%]|%]]+)")
if linkedFile then
value = linkedFile
else
value = value:gsub("^%s*[Ff]ile%s*:%s*", "")
value = value:gsub("^%s*[Ii]mage%s*:%s*", "")
value = value:gsub("|.*$", "")
value = value:gsub("%]%].*$", "")
end
value = mw.text.trim(value)
if value == "" then
return nil
end
if value:lower() == "none" or value:lower() == "no" then
return nil
end
return "File:" .. value
end
local function extractImageFromContent(wikitext)
if not wikitext or wikitext == "" then
return nil
end
for line in mw.text.gsplit(wikitext, "\n", true) do
local value = line:match("^%s*|%s*image%d*%s*=%s*(.-)%s*$")
if value then
local file = normalizeFileName(value)
if file then
return file
end
end
end
local fallback = wikitext:match("%[%[%s*[Ff]ile%s*:%s*([^%]|%]]+)")
or wikitext:match("%[%[%s*[Ii]mage%s*:%s*([^%]|%]]+)")
if fallback then
return normalizeFileName(fallback)
end
return nil
end
local function getFeaturedData()
local pages = getPool()
if #pages == 0 then
return nil, "No featured pages defined."
end
local day = tonumber(os.date("%j")) or 1
local index = ((day - 1) % #pages) + 1
local title = pages[index]
local page = mw.title.new(title)
local lead = ""
local image = nil
if page and page.exists then
local content = page:getContent() or ""
lead = extractLeadFromContent(content)
image = extractImageFromContent(content)
end
if lead == "" then
lead = "Featured page for " .. os.date("%d %B %Y") .. "."
end
return {
title = title,
lead = lead,
image = image
}
end
function p.main(frame)
local args = frame and frame.args or {}
local data, err = getFeaturedData()
if not data then
return err
end
if args.titleonly == "1" then
return data.title
end
if args.imageonly == "1" then
return data.image or ""
end
return string.format(
"'''[[%s]]'''<div style=\"margin-top:0.35em;\"><small>%s</small></div>",
data.title,
data.lead
)
end
function p.box()
local data, err = getFeaturedData()
if not data then
return '<div class="featured-box"><div class="featured-box-title">Featured page</div><div class="featured-box-body">' .. err .. '</div></div>'
end
local imageBlock = ""
if data.image then
imageBlock = string.format(
'<div class="featured-box-image">[[%s|frameless|170x170px|link=%s|%s]]</div>',
data.image,
data.title,
data.title
)
end
return string.format(
'<div class="featured-box">' ..
'<div class="featured-box-title">Featured page</div>' ..
'<div class="featured-box-body">' ..
'%s' ..
'<div class="featured-box-text">' ..
'<div class="featured-box-page-title">\'\'\'[[%s]]\'\'\'</div>' ..
'<div class="featured-box-lead">%s</div>' ..
'<div class="featured-box-open">[[:%s|Open page]]</div>' ..
'</div>' ..
'</div>' ..
'</div>',
imageBlock,
data.title,
data.lead,
data.title
)
end
return p