Module:On This Day: Difference between revisions

From Zelda Wiki, the Zelda encyclopedia
Jump to navigation Jump to search
No edit summary
m (maintenance category link fix)
 
(29 intermediate revisions by 3 users not shown)
Line 1: Line 1:
local p = {}
local p = {}
local h = {}
local h = {}
local cargo = mw.ext.cargo
 
local utilsCode = require("Module:UtilsCode")
local utilsCargo = require("Module:UtilsCargo")
local utilsLayout = require("Module:UtilsLayout")
local utilsString = require("Module:UtilsString")
local utilsTable = require("Module:UtilsTable")
local utilsVar = require("Module:UtilsVar")


function p._Main(frame)
function p._Main(frame)
local entry = h.GetEntryForToday()
local entry = h.GetEntryForToday()
return h.DisplaySection(entry)
return h.DisplaySection(entry)
end
function p.AddToPreview(frame)
local args = frame:getParent().args
utilsVar.add("entries", args)
end
function p.Preview(frame)
local entries = utilsVar.get("entries")
local eventsByDate = utilsTable.keyBy(entries, function(entry)
return string.sub(entry.date, 6)
end)
local calendar = {
{"January", 31},
{"February", 29},
{"March", 31},
{"April", 30},
{"May", 31},
{"June", 30},
{"July", 31},
{"August", 31},
{"September", 30},
{"October", 31},
{"November", 30},
{"December", 31},
}
local rows = {}
for i, month in ipairs(calendar) do
for day = 1, month[2] do
local d = h.toString(day)
local m = h.toString(i)
local date = m.."-"..d
local event = eventsByDate[date]
table.insert(rows, {
id = date,
cells = {
{
sortValue = date,
content = month[1].." "..day,
},
event and event.date or "",
event and h.DisplaySection(event) or ""
},
})
end
end
local wikitable = utilsLayout.table({
headers = {"Day", "Date", "On This Day"},
rows = rows,
sortable = true,
})
local styles = frame:extensionTag({
name = "templatestyles",
args = { src = "Template:Main Page Infoblocks/Styles.css" }
})
return styles..wikitable
end
function h.toString(num)
if num < 10 then
return "0"..num
else
return tostring(num)
end
end
end


function h.GetEntryForToday()
function h.GetEntryForToday()
--local formattedDate = os.date("%m-%d")
local formattedDate = os.date("%m-%d")
local formattedDate = "03-03"
local entries = utilsCargo.query("OnThisDay", "date, title, filename, text, link", {})
local entries = cargo.query("OnThisDay", "date, title, filename, text, link", {})
local todayEntries = {}
local todayEntries = {}
Line 24: Line 91:


function h.DisplaySection(entry)
function h.DisplaySection(entry)
if not utilsCode.IsEmpty(entry) then
local result = ""
local diffYears = os.date("%Y") - tonumber(string.sub(entry["date"], 1, 4))
local header = "On this day"
result = mw.html.create("div")
if entry then
:addClass("infoblock")
header = header .. ", " ..  (os.date("%Y") - tonumber(string.sub(entry["date"], 1, 4))) .. " years ago..."
:attr("id", "on-this-day")
else
:node(mw.html.create("div")
header = header .. "..."
:addClass("infoblock-header")
entry = {}
:wikitext("On this day, " .. diffYears .. " years ago..."))
entry["filename"] = "LA Mr. Write Artwork.png"
:node(mw.html.create("div")
entry["title"] = "Nothing happened"
:addClass("infoblock-content")
entry["text"] = "But you can change that! All it takes is one edit to make history. [[Special:RecentChanges|Zelda Wiki history]], that is.\n* [https://discordapp.com/invite/eJnnvYb Join our Discord]\n* [[Guidelines:Getting Started|Learn how to edit]]\n* [[:Category:Articles needing attention|Check out pages needing attention]]"
:wikitext("[[File:" .. entry["filename"] .. "|left|150x150px]]" .. entry["text"] .. "<br>" .. mw.getCurrentFrame():preprocess("<nowiki>[</nowiki>[[" .. entry["link"] .. "|Read more...]]<nowiki>]</nowiki>")))
entry["link"] = "Guidelines:Getting Started"
end
end
return result
local purgeButton = mw.html.create("span")
:addClass("plainlinks otd-refresh-button")
:wikitext(mw.getCurrentFrame():preprocess("<nowiki>[</nowiki>[{{FULLURL:Main Page|action=purge}} refresh]<nowiki>]</nowiki>"))
local html = mw.html.create("div")
:addClass("infoblock")
:attr("id", "on-this-day")
:tag("div")
:addClass("infoblock-header")
:wikitext(header)
:done()
:tag("div")
:addClass("infoblock-body")
:tag("div")
:addClass("infoblock-text")
:tag("div")
:addClass("infoblock-title")
:tag("div")
:addClass("infoblock-title-text")
:wikitext(entry["title"])
:done()
:tag("span")
:addClass("plainlinks otd-refresh-button")
:wikitext(mw.getCurrentFrame():preprocess("<nowiki>[</nowiki>[{{FULLURL:Main Page|action=purge}} refresh]<nowiki>]</nowiki>"))
:done()
:done()
:tag("div")
:addClass("infoblock-content")
:wikitext(entry["text"])
:done()
:done()
:tag("div")
:addClass("infoblock-image")
:wikitext("[[File:" .. entry["filename"] .. "|200x200px]]")
:done()
:done()
 
return tostring(html)
end
end
p.Templates = {
["On This Day"] = {},
["On This Day/Store"] = {},
}


return p
return p

Latest revision as of 16:30, 22 May 2023

This is the main module for the following templates:
local p = {}
local h = {}

local utilsCargo = require("Module:UtilsCargo")
local utilsLayout = require("Module:UtilsLayout")
local utilsString = require("Module:UtilsString")
local utilsTable = require("Module:UtilsTable")
local utilsVar = require("Module:UtilsVar")

function p._Main(frame)
	local entry = h.GetEntryForToday()
	return h.DisplaySection(entry)
end

function p.AddToPreview(frame)
	local args = frame:getParent().args
	utilsVar.add("entries", args)
end

function p.Preview(frame)
	local entries = utilsVar.get("entries")
	local eventsByDate = utilsTable.keyBy(entries, function(entry)
		return string.sub(entry.date, 6)
	end)

	local calendar = {
		{"January", 31},
		{"February", 29},
		{"March", 31},
		{"April", 30},
		{"May", 31},
		{"June", 30},
		{"July", 31},
		{"August", 31},
		{"September", 30},
		{"October", 31},
		{"November", 30},
		{"December", 31},
	}
	local rows = {}
	for i, month in ipairs(calendar) do
		for day = 1, month[2] do
			local d = h.toString(day)
			local m = h.toString(i)
			local date = m.."-"..d
			local event = eventsByDate[date]
			table.insert(rows, {
				id = date,
				cells = {
					{
						sortValue = date,
						content = month[1].." "..day,
					},
					event and event.date or "", 
					event and h.DisplaySection(event) or ""
				},
			})
		end
	end
	local wikitable = utilsLayout.table({
		headers = {"Day", "Date", "On This Day"},
		rows = rows,
		sortable = true,
	})
	local styles = frame:extensionTag({
		name = "templatestyles",
		args = { src = "Template:Main Page Infoblocks/Styles.css" }
	})
	return styles..wikitable
end
function h.toString(num)
	if num < 10 then
		return "0"..num
	else
		return tostring(num)
	end
end

function h.GetEntryForToday()
	local formattedDate = os.date("%m-%d")
	local entries = utilsCargo.query("OnThisDay", "date, title, filename, text, link", {})
	
	local todayEntries = {}
	for key, entry in ipairs(entries) do
		if string.sub(entry["date"], 6) == formattedDate then
			table.insert(todayEntries, entry)
		end
	end
	return todayEntries[1]
end

function h.DisplaySection(entry)
	local result = ""
	local header = "On this day"
	if entry then
		header = header .. ", " ..  (os.date("%Y") - tonumber(string.sub(entry["date"], 1, 4))) .. " years ago..."
	else
		header = header .. "..."
		entry = {}
		entry["filename"] = "LA Mr. Write Artwork.png"
		entry["title"] = "Nothing happened"
		entry["text"] = "But you can change that! All it takes is one edit to make history. [[Special:RecentChanges|Zelda Wiki history]], that is.\n* [https://discordapp.com/invite/eJnnvYb Join our Discord]\n* [[Guidelines:Getting Started|Learn how to edit]]\n* [[:Category:Articles needing attention|Check out pages needing attention]]"
		entry["link"] = "Guidelines:Getting Started"
	end
	
	local purgeButton = mw.html.create("span")
		:addClass("plainlinks otd-refresh-button")
		:wikitext(mw.getCurrentFrame():preprocess("<nowiki>[</nowiki>[{{FULLURL:Main Page|action=purge}} refresh]<nowiki>]</nowiki>"))
	
	local html = mw.html.create("div")
		:addClass("infoblock")
		:attr("id", "on-this-day")
		:tag("div")
			:addClass("infoblock-header")
			:wikitext(header)
			:done()
		:tag("div")
			:addClass("infoblock-body")
			:tag("div")
				:addClass("infoblock-text")
				:tag("div")
					:addClass("infoblock-title")
					:tag("div")
						:addClass("infoblock-title-text")
						:wikitext(entry["title"])
						:done()
					:tag("span")
						:addClass("plainlinks otd-refresh-button")
						:wikitext(mw.getCurrentFrame():preprocess("<nowiki>[</nowiki>[{{FULLURL:Main Page|action=purge}} refresh]<nowiki>]</nowiki>"))
						:done()
					:done()
				:tag("div")
					:addClass("infoblock-content")
					:wikitext(entry["text"])
					:done()
				:done()
			:tag("div")
				:addClass("infoblock-image")
				:wikitext("[[File:" .. entry["filename"] .. "|200x200px]]")
				:done()
		:done()

	return tostring(html)
end

p.Templates = {
	["On This Day"] = {},
	["On This Day/Store"] = {},
}

return p