Module:Sandbox/Dat Hack3r: Difference between revisions

From Our World of Text Wiki
Jump to navigation Jump to search
Dat Hack3r (talk | contribs)
No edit summary
Dat Hack3r (talk | contribs)
m Forgot to add the colon (":") after the namespace.
 
(23 intermediate revisions by the same user not shown)
Line 1: Line 1:
local p = {}
local p = {}


-- Function to calculate the previous week's number based on the current date
local horizontal = require('Module:List').horizontal
function p.lastReleaseNumber(frame)
local mw_error = require('Module:Error').error
    -- Define the start date of Week 1
 
    local week1Start = os.time{year=2024, month=9, day=8}
function p._main(s_start, s_end, increment, title, title_format, text_format)
   
title_format = title_format or string.gsub(title.text, '%d+', '%%d', 1)
    -- Convert the input date from the frame to a time object
text_format = text_format or '%d'
    local year = tonumber(frame.args.year)
local curr_num
    local month = tonumber(frame.args.month)
if title_format then
    local day = tonumber(frame.args.day)
curr_num = tonumber(string.match(title.text, string.gsub(title_format, "%%d", "(%%d+)")))
    local currentDate = os.time{year=year, month=month, day=day}
else
   
curr_num = tonumber(string.match(title.text, '%d+'))
    -- Calculate the difference in days from the start of Week 1 to the current date
end
    local daysDifference = os.difftime(currentDate, week1Start) / (24 * 60 * 60)
 
   
local navlist = {}
    -- Calculate the current week number
local prepad = ''
    local currentWeek = math.floor(daysDifference / 7) + 1
local postpad = ''
   
for i=curr_num - 5,curr_num + 5 do
    -- Calculate the previous week number, ensuring it does not go below 1
if i < s_start then
    local previousWeek = math.max(currentWeek - 1, 1)
prepad = prepad..'<span style="visibility: hidden;"> • '..string.format(text_format, i)..'</span>'
   
elseif s_end and i > s_end then
    return previousWeek
postpad = postpad..'<span style="visibility: hidden;"> • '..string.format(text_format, i)..'</span>'
else
if mw.title.new(string.format(title_format, i), title.namespace).exists then
table.insert(navlist, '[['..title.nsText..':'..string.format(title_format, i)..'|'..string.format(text_format, i)..']]')
else
table.insert(navlist, '<span class="seriesNavigation-item-inactive">'..string.format(text_format, i)..'</span>')
end
end
end
navlist[1] = prepad..navlist[1]
navlist[#navlist] = navlist[#navlist]..postpad
 
local templatestyles = mw.getCurrentFrame():extensionTag{
name = "templatestyles",
args = {src = 'Module:Series navigation/styles.css'}
}
return templatestyles..'<div class="toccolours seriesNavigation" role="navigation" aria-label="Range">'..horizontal(navlist)..'</div>'
end
 
function p.main(frame)
local s_start      = tonumber(frame:getParent().args['start']) or 1
local s_end        = tonumber(frame:getParent().args['end'])
local increment    = tonumber(frame:getParent().args['increment']) or 1
local title        = mw.title.getCurrentTitle()
local title_format = frame:getParent().args['title_format']
local text_format  = frame:getParent().args['text_format']
return p._main(s_start, s_end, increment, title, title_format, text_format)
end
end


return p
return p

Latest revision as of 12:07, 17 December 2024

Documentation for this module may be created at Module:Sandbox/Dat Hack3r/doc

local p = {}

local horizontal = require('Module:List').horizontal
local mw_error = require('Module:Error').error

function p._main(s_start, s_end, increment, title, title_format, text_format)
	title_format = title_format or string.gsub(title.text, '%d+', '%%d', 1)
	text_format = text_format or '%d'
	local curr_num
	if title_format then
		curr_num = tonumber(string.match(title.text, string.gsub(title_format, "%%d", "(%%d+)")))
	else
		curr_num = tonumber(string.match(title.text, '%d+'))
	end

	local navlist = {}
	local prepad = ''
	local postpad = ''
	for i=curr_num - 5,curr_num + 5 do
		if i < s_start then
			prepad = prepad..'<span style="visibility: hidden;"> • '..string.format(text_format, i)..'</span>'
		elseif s_end and i > s_end then
			postpad = postpad..'<span style="visibility: hidden;"> • '..string.format(text_format, i)..'</span>'
		else
			if mw.title.new(string.format(title_format, i), title.namespace).exists then
				table.insert(navlist, '[['..title.nsText..':'..string.format(title_format, i)..'|'..string.format(text_format, i)..']]')
			else
				table.insert(navlist, '<span class="seriesNavigation-item-inactive">'..string.format(text_format, i)..'</span>')
			end
		end
	end
	navlist[1] = prepad..navlist[1]
	navlist[#navlist] = navlist[#navlist]..postpad

	local templatestyles = mw.getCurrentFrame():extensionTag{
		name = "templatestyles",
		args = {src = 'Module:Series navigation/styles.css'}
	}
	return templatestyles..'<div class="toccolours seriesNavigation" role="navigation" aria-label="Range">'..horizontal(navlist)..'</div>'
end

function p.main(frame)
	local s_start      = tonumber(frame:getParent().args['start']) or 1
	local s_end        = tonumber(frame:getParent().args['end'])
	local increment    = tonumber(frame:getParent().args['increment']) or 1
	local title        = mw.title.getCurrentTitle()
	local title_format = frame:getParent().args['title_format']
	local text_format  = frame:getParent().args['text_format']
	return p._main(s_start, s_end, increment, title, title_format, text_format)
end

return p