Module:Chabad Days
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