Jump to content

Module:Chabad Days

Revision as of 14:09, 20 November 2026 by Shraga (talk | contribs) (Strip footnotes from box lines)

Documentation for this module may be created at Module:Chabad Days/doc

-- Module:Chabad Days
-- Shows today's events from the "Chabad Days" section of the matching day article
-- (e.g. "10 Kislev"), as on the Hebrew main page's ימי חב"ד box.
local p = {}

-- #time English month names -> Chabadpedia spelling
local MONTHS = {
	['Tevet'] = 'Teves', ['Tamuz'] = 'Tammuz', ['Av'] = 'Menachem Av',
	['Adar II'] = 'Adar', ['Nisan'] = 'Nissan', ['Heshvan'] = 'Cheshvan', ['Tishri'] = 'Tishrei',
}

local function ucfirstText(t)
	-- capitalize the first visible letter: "the X" -> "The X", "[[A|the X]]" -> "[[A|The X]]"
	local a, rest = t:match("^(%[%[[^|%]]*|)(.*)$")
	if a then return a .. rest:gsub('^%l', string.upper) end
	return (t:gsub('^%l', string.upper))
end

local function cleanLine(line)
	line = line:gsub('<ref[^>]-/>', ''):gsub('<ref.-</ref>', '')
	-- "5587 (10/12/1826 by the [[Gentile Calendar|secular count]]) - text"  ->  "[[5587]] (1826) – Text"
	local b1, year, b2, paren, b3, text = line:match("^%*+%s*('*)(%d%d%d%d)('*)%s*%((.-)%)%s*('*)%s*%-%s*(.*)$")
	if not year then
		b1, year, b2, b3, text = line:match("^%*+%s*('*)(%d%d%d%d)('*)%s*('*)%s*%-%s*(.*)$")
	end
	if not year then return line end
	local greg = paren and paren:match('.*(%d%d%d%d)')
	local out = '* ' .. b1 .. '[[' .. year .. ']]' .. b2
	if greg then out = out .. ' (' .. greg .. ')' end
	return out, b3 .. ucfirstText(text)
end

function p.main(frame)
	local args = frame.args
	local day = args.day
	local month = args.month
	if not day or day == '' then
		day = frame:callParserFunction('#time', 'xjj')
	end
	if not month or month == '' then
		month = frame:callParserFunction('#time', 'xjF')
	end
	month = MONTHS[month] or month
	local pageName = day .. ' ' .. month
	local title = mw.title.new(pageName)
	if title and title.exists and title.isRedirect and title.redirectTarget then
		title = title.redirectTarget
	end
	local content = title and title.exists and title:getContent()
	local out = {}
	if content then
		local section = content:match('\n==%s*Chabad Days%s*==%s*\n(.-)\n==[^=]') or
			content:match('\n==%s*Chabad Days%s*==%s*\n(.*)$')
		if section then
			local prefix = ''
			for line in (section .. '\n'):gmatch('(.-)\n') do
				local head = line:match('^===%s*(.-)%s*===%s*$')
				if line:match('^====') then
					-- sub-sub-heading: ignore
				elseif head then
					head = head:gsub('%[%[[^|%]]*|', ''):gsub('[%[%]]', ''):lower()
					if head:find('born') or head:find('birth') then prefix = "''Born:'' "
					elseif head:find('pass') or head:find('death') or head:find('died') then prefix = "''Passed away:'' "
					else prefix = '' end
				elseif line:match('^%*') then
					local head2, text = cleanLine(line)
					if text then
						table.insert(out, head2 .. ' – ' .. prefix .. text)
					else
						table.insert(out, head2)
					end
				end
			end
		end
	end
	local body
	if #out > 0 then
		body = table.concat(out, '\n')
	else
		body = "''No Chabad events are recorded for this day yet.''"
	end
	local year = frame:callParserFunction('#time', 'xjY')
	local footer = '<div style="clear:both; text-align:right; margin-top:0.4em;">' ..
		"'''" .. (content and ('[[' .. title.fullText .. '|' .. pageName .. ']]') or pageName) ..
		' [[' .. year .. "]]'''" ..
		(content and (" – '''[[" .. title.fullText .. "#Chabad Days|More on this day]]'''") or '') ..
		'</div>'
	return '<div style="text-align:justify;">\n' .. body .. '\n</div>\n' .. footer
end

return p