Jump to content

Module:DailyFeatured: Difference between revisions

From the Vrienden Universe, a fictional wiki
Created page with "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) if line ~= "" then line = line:gsub("^%*%s*", "") 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 =..."
 
No edit summary
Line 19: Line 19:


local function cleanWikitext(s)
local function cleanWikitext(s)
if not s or s == "" then return "" end
if not s or s == "" then
return ""
end
s = s:gsub("<!--.-?-->", "")
s = s:gsub("<!--.-?-->", "")
s = s:gsub("%[%[File:.-%]%]", "")
s = s:gsub("%[%[File:.-%]%]", "")
Line 30: Line 32:


local function extractHeadlineFromContent(wikitext)
local function extractHeadlineFromContent(wikitext)
if not wikitext or wikitext == "" then return "" end
if not wikitext or wikitext == "" then
return ""
end


-- Prefer first section heading if present
for line in mw.text.gsplit(wikitext, "\n", true) do
for line in mw.text.gsplit(wikitext, "\n", true) do
line = cleanWikitext(line)
line = cleanWikitext(line)
Line 37: Line 42:
line = line:gsub("^=+%s*", ""):gsub("%s*=+$", "")
line = line:gsub("^=+%s*", ""):gsub("%s*=+$", "")
line = mw.text.trim(line)
line = mw.text.trim(line)
if line ~= "" then return line end
if line ~= "" then
return line
end
end
end
end
end


-- Otherwise first meaningful non-empty line
for line in mw.text.gsplit(wikitext, "\n", true) do
for line in mw.text.gsplit(wikitext, "\n", true) do
line = cleanWikitext(line)
line = cleanWikitext(line)
Line 62: Line 70:
end
end


local day = tonumber(os.date("%j"))
local day = tonumber(os.date("%j")) -- day of year
local index = (day % #pages) + 1
local index = (day % #pages) + 1
local title = pages[index]
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 page = mw.title.new(title)

Revision as of 20:03, 4 February 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)
		if line ~= "" then
			line = line:gsub("^%*%s*", "")
			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("%[%[File:.-%]%]", "")
	s = s:gsub("%{%b{}%}", "")
	s = s:gsub("<ref.->.-</ref>", "")
	s = s:gsub("<ref.->", "")
	s = s:gsub("\r", ""):gsub("[ \t]+", " ")
	return mw.text.trim(s)
end

local function extractHeadlineFromContent(wikitext)
	if not wikitext or wikitext == "" then
		return ""
	end

	-- Prefer first section heading if present
	for line in mw.text.gsplit(wikitext, "\n", true) do
		line = cleanWikitext(line)
		if line:match("^==+%s*[^=].-==+%s*$") then
			line = line:gsub("^=+%s*", ""):gsub("%s*=+$", "")
			line = mw.text.trim(line)
			if line ~= "" then
				return line
			end
		end
	end

	-- Otherwise first meaningful non-empty line
	for line in mw.text.gsplit(wikitext, "\n", true) do
		line = cleanWikitext(line)
		if line ~= ""
			and not line:match("^__%u+__$")
			and not line:match("^%[%[Category:") then
			if #line > 180 then
				line = line:sub(1, 177) .. "…"
			end
			return line
		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")) -- day of year
	local index = (day % #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