Jump to content

Module:Chabad Days: Difference between revisions

Module for the main-page Chabad Days box (per Hebrew תבנית:אירועים בלוח העברי)
 
Footer link as a button; button() for box button rows
 
(5 intermediate revisions by the same user not shown)
Line 7: Line 7:
local MONTHS = {
local MONTHS = {
['Tevet'] = 'Teves', ['Tamuz'] = 'Tammuz', ['Av'] = 'Menachem Av',
['Tevet'] = 'Teves', ['Tamuz'] = 'Tammuz', ['Av'] = 'Menachem Av',
['Adar II'] = 'Adar', ['Heshvan'] = 'Cheshvan', ['Tishri'] = 'Tishrei',
['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)
local function cleanLine(line)
-- "5587 (10/12/1826 by the [[Gentile Calendar|secular count]]) - text"  ->  "[[5587]] (1826) – text"
line = line:gsub('<ref[^>]-/>', ''):gsub('<ref.-</ref>', '')
line = line:gsub("^(%*+)%s*('*)(%d%d%d%d)('*)%s*%(%d+/%d+/(%d+)[^%)]*%)%s*('*)%s*%-%s*",
-- "5587 (10/12/1826 by the [[Gentile Calendar|secular count]]) - text"  ->  "[[5587]] (1826) - Text"
function(stars, b1, year, b2, greg, b3)
local b1, year, b2, paren, b3, text = line:match("^%*+%s*('*)(%d%d%d%d)('*)%s*%((.-)%)%s*('*)%s*%-%s*(.*)$")
return '* ' .. b1 .. '[[' .. year .. ']]' .. b2 .. ' (' .. greg .. ') ' .. b3 .. '– '
if not year then
end)
b1, year, b2, b3, text = line:match("^%*+%s*('*)(%d%d%d%d)('*)%s*('*)%s*%-%s*(.*)$")
line = line:gsub("^(%*+)%s*('*)(%d%d%d%d)('*)%s*%-%s*", '* %2[[%3]]%4 – ')
end
return line
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._button(target, pageName, year)
return '<span style="display:inline-block; padding:0.25em 0.9em; border:1px solid #a2a9b1; border-radius:4px; background:#ffffff;">[[' ..
target .. '#Chabad Days|' .. pageName .. ' ' .. year .. ' - More on this day]]</span>'
end
 
-- button only, for use in a box's button row
function p.button(frame)
local day = frame:callParserFunction('#time', 'xjj')
local month = frame:callParserFunction('#time', 'xjF')
month = MONTHS[month] or month
local pageName = day .. ' ' .. month
local title = mw.title.new(pageName)
if not (title and title.exists) then return '' end
if title.isRedirect and title.redirectTarget then title = title.redirectTarget end
return p._button(title.fullText, pageName, frame:callParserFunction('#time', 'xjY'))
end
end


Line 33: Line 61:
local pageName = day .. ' ' .. month
local pageName = day .. ' ' .. month
local title = mw.title.new(pageName)
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 content = title and title.exists and title:getContent()
local out = {}
local out = {}
Line 39: Line 70:
content:match('\n==%s*Chabad Days%s*==%s*\n(.*)$')
content:match('\n==%s*Chabad Days%s*==%s*\n(.*)$')
if section then
if section then
section = section:gsub('<ref[^>]-/>', ''):gsub('<ref.-</ref>', '')
local prefix = ''
local prefix = ''
for line in (section .. '\n'):gmatch('(.-)\n') do
for line in (section .. '\n'):gmatch('(.-)\n') do
local head = line:match('^===%s*(.-)%s*===%s*$')
local head = line:match('^===%s*(.-)%s*===%s*$')
if head then
if line:match('^====') then
head = head:gsub('%[%[[^|%]]*|', ''):gsub('[%[%]]', '')
-- sub-sub-heading: ignore
if head == 'Born' then prefix = "''Born:'' "
elseif head then
elseif head == 'Passed Away' then prefix = "''Passed away:'' "
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
else prefix = '' end
elseif line:match('^%*') then
elseif line:match('^%*') then
local l = cleanLine(line)
local head2, text = cleanLine(line)
if prefix ~= '' then
if text then
l = l:gsub(' – ', ' – ' .. prefix, 1)
table.insert(out, head2 .. ' - ' .. prefix .. text)
else
table.insert(out, head2)
end
end
table.insert(out, l)
end
end
end
end
Line 64: Line 99:
end
end
local year = frame:callParserFunction('#time', 'xjY')
local year = frame:callParserFunction('#time', 'xjY')
local footer = '<div style="clear:both; text-align:right; margin-top:0.4em;">' ..
local target = content and title.fullText or nil
"'''[[" .. pageName .. "]] [[" .. year .. "]]''' – " ..
local footer = ''
"'''[[" .. pageName .. "#Chabad Days|More on this day]]'''</div>"
if args.button ~= 'no' and target then
footer = '<div style="clear:both; text-align:right; margin-top:0.4em;">' .. p._button(target, pageName, year) .. '</div>'
end
return '<div style="text-align:justify;">\n' .. body .. '\n</div>\n' .. footer
return '<div style="text-align:justify;">\n' .. body .. '\n</div>\n' .. footer
end
end


return p
return p

Latest revision as of 14:27, 20 November 2026

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._button(target, pageName, year)
	return '<span style="display:inline-block; padding:0.25em 0.9em; border:1px solid #a2a9b1; border-radius:4px; background:#ffffff;">[[' ..
		target .. '#Chabad Days|' .. pageName .. ' ' .. year .. ' - More on this day]]</span>'
end

-- button only, for use in a box's button row
function p.button(frame)
	local day = frame:callParserFunction('#time', 'xjj')
	local month = frame:callParserFunction('#time', 'xjF')
	month = MONTHS[month] or month
	local pageName = day .. ' ' .. month
	local title = mw.title.new(pageName)
	if not (title and title.exists) then return '' end
	if title.isRedirect and title.redirectTarget then title = title.redirectTarget end
	return p._button(title.fullText, pageName, frame:callParserFunction('#time', 'xjY'))
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
			section = section:gsub('<ref[^>]-/>', ''):gsub('<ref.-</ref>', '')
			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 target = content and title.fullText or nil
	local footer = ''
	if args.button ~= 'no' and target then
		footer = '<div style="clear:both; text-align:right; margin-top:0.4em;">' .. p._button(target, pageName, year) .. '</div>'
	end
	return '<div style="text-align:justify;">\n' .. body .. '\n</div>\n' .. footer
end

return p