Jump to content

Module:DailyFeatured: Difference between revisions

From the Vrienden Universe, a fictional wiki
No edit summary
No edit summary
Line 28: Line 28:
end
end


local function cleanWikitext(s)
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("%[%[File:.-%]%]", "")
s = s:gsub("%[%[[Ff]ile:.-%]%]", "")
s = s:gsub("%[%[Image:.-%]%]", "")
s = s:gsub("%[%[[Ii]mage:.-%]%]", "")
s = s:gsub("%{%b{}%}", "")
s = removeTemplates(s)
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)
local cleanedLines = {}
end


local function shorten(s)
for line in mw.text.gsplit(s, "\n", true) do
if mw.ustring.len(s) > 200 then
local trimmed = mw.text.trim(line)
return mw.ustring.sub(s, 1, 197) .. "…"
end


return s
if trimmed ~= ""
end
and not trimmed:match("^__%u+__$")
 
and not trimmed:match("^%[%[[Cc]ategory:")
local function isIgnoredLine(raw, cleaned)
and not trimmed:match("^%s*|")
raw = raw or ""
and not trimmed:match("^%s*}") then
cleaned = cleaned or ""
table.insert(cleanedLines, trimmed)
 
end
if cleaned == "" then
return true
end
end


if raw:match("^%s*{{") or raw:match("^%s*|") or raw:match("^%s*}}") then
return mw.text.trim(table.concat(cleanedLines, "\n\n"))
return true
end
 
if cleaned:match("^__%u+__$") then
return true
end
 
if cleaned:match("^%[%[[Cc]ategory:") then
return true
end
 
return false
end
end


local function extractHeadlineFromContent(wikitext)
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
local raw = line
if line:match("^%s*==+%s*[^=].-==+%s*$") then
local cleaned = cleanWikitext(line)
 
if raw:match("^==+%s*[^=].-==+%s*$") then
break
break
end
end


if not isIgnoredLine(raw, cleaned) then
table.insert(leadLines, line)
return shorten(cleaned)
end
end
end


for line in mw.text.gsplit(wikitext, "\n", true) do
return cleanLeadWikitext(table.concat(leadLines, "\n"))
local raw = mw.text.trim(line)
 
if raw:match("^==+%s*[^=].-==+%s*$") then
local cleaned = raw:gsub("^=+%s*", ""):gsub("%s*=+$", "")
cleaned = cleanWikitext(cleaned)
 
if cleaned ~= "" then
return shorten(cleaned)
end
end
end
 
for line in mw.text.gsplit(wikitext, "\n", true) do
local raw = line
local cleaned = cleanWikitext(line)
 
if not isIgnoredLine(raw, cleaned) then
return shorten(cleaned)
end
end
 
return ""
end
end


Line 201: Line 164:


local page = mw.title.new(title)
local page = mw.title.new(title)
local headline = ""
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 ""
headline = extractHeadlineFromContent(content)
lead = extractLeadFromContent(content)
image = extractImageFromContent(content)
image = extractImageFromContent(content)
end
end


if headline == "" then
if lead == "" then
headline = "Featured page for " .. os.date("%d %B %Y") .. "."
lead = "Featured page for " .. os.date("%d %B %Y") .. "."
end
end


return {
return {
title = title,
title = title,
page = page,
lead = lead,
headline = headline,
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,
mw.text.nowiki(data.headline)
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 page = mw.title.new(data.title)
local pool = mw.title.new("Template:Featured pool")
local current = mw.title.getCurrentTitle()
local pageUrl = page and page:fullUrl() or ""
local editUrl = page and page:fullUrl("action=edit") or ""
local poolUrl = pool and pool:fullUrl() or ""
local purgeUrl = current and current:fullUrl("action=purge") or ""


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|160x160px|link=%s|%s]]</div>',
'<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-name">\'\'\'[[%s]]\'\'\'</div>' ..
'<div class="featured-box-page-title">\'\'\'[[%s]]\'\'\'</div>' ..
'<div class="featured-box-headline"><small>%s</small></div>' ..
'<div class="featured-box-lead">%s</div>' ..
'<div class="featured-box-actions">[%s Open page] [%s Edit page] [%s Featured pool] [%s Refresh]</div>' ..
'<div class="featured-box-open">[[:%s|Open page]]</div>' ..
'</div>' ..
'</div>' ..
'</div>' ..
'</div>' ..
Line 286: Line 239:
imageBlock,
imageBlock,
data.title,
data.title,
mw.text.nowiki(data.headline),
data.lead,
pageUrl,
data.title
editUrl,
poolUrl,
purgeUrl
)
)
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