Module:BCIChapterNavbox: Difference between revisions
NeonWabbit (talk | contribs) No edit summary |
capitalisation change |
||
(4 intermediate revisions by one other user not shown) | |||
Line 7: | Line 7: | ||
start_year = 2010, | start_year = 2010, | ||
end_year = 2014, | end_year = 2014, | ||
group = 'group1', | group = 'group1', | ||
list = 'list1', | list = 'list1', | ||
Line 15: | Line 14: | ||
start_year = 2015, | start_year = 2015, | ||
end_year = 2019, | end_year = 2019, | ||
group = 'group2', | group = 'group2', | ||
list = 'list2', | list = 'list2', | ||
Line 23: | Line 21: | ||
start_year = 2020, | start_year = 2020, | ||
end_year = 2024, | end_year = 2024, | ||
group = 'group3', | group = 'group3', | ||
list = 'list3', | list = 'list3', | ||
} | } | ||
} | |||
-- Special cases mapping: Actual title to MediaWiki-safe title | |||
local special_cases = { | |||
["Augustus, You Jerk #2"] = "Augustus, You Jerk 2" | |||
} | } | ||
Line 33: | Line 35: | ||
local year = tonumber(chapter_year) | local year = tonumber(chapter_year) | ||
return year and year >= start_year and year <= end_year | return year and year >= start_year and year <= end_year | ||
end | |||
-- Function to get the MediaWiki-safe title | |||
function p.getSafeTitle(frame) | |||
local title = frame.args and frame.args.title or frame | |||
return special_cases[title] or title | |||
end | end | ||
Line 61: | Line 69: | ||
local navboxParams = { | local navboxParams = { | ||
bodyclass = 'hlist', | bodyclass = 'hlist', | ||
name = ' | name = 'Bittersweet Club International chapters', | ||
title = "[[Bittersweet Club International]] | title = "[[Bittersweet Club International]] chapters", | ||
titlestyle = '', | titlestyle = '', | ||
image = '', | image = '', | ||
Line 71: | Line 79: | ||
-- Add groups and lists | -- Add groups and lists | ||
for _, group in ipairs(groups) do | for _, group in ipairs(groups) do | ||
navboxParams[group.group] = group.name | navboxParams[group.group] = group.name | ||
Line 78: | Line 85: | ||
local year = tonumber(string.sub(chapter.date, 1, 4)) | local year = tonumber(string.sub(chapter.date, 1, 4)) | ||
if isChapterInYearRange(year, group.start_year, group.end_year) then | if isChapterInYearRange(year, group.start_year, group.end_year) then | ||
local encodedTitle = mw.uri.encode( | local safeTitle = p.getSafeTitle(chapter.title) | ||
local encodedTitle = mw.uri.encode(safeTitle, 'PATH') | |||
table.insert(list, '* [[' .. encodedTitle .. '|' .. chapter.title .. ']]') | table.insert(list, '* [[' .. encodedTitle .. '|' .. chapter.title .. ']]') | ||
end | end |
Latest revision as of 07:01, 20 November 2024
Documentation for this module may be created at Module:BCIChapterNavbox/doc
local p = {}
-- Define the 5-year ranges and styles
local groups = {
{
name = '2010–2014',
start_year = 2010,
end_year = 2014,
group = 'group1',
list = 'list1',
},
{
name = '2015–2019',
start_year = 2015,
end_year = 2019,
group = 'group2',
list = 'list2',
},
{
name = '2020–2024',
start_year = 2020,
end_year = 2024,
group = 'group3',
list = 'list3',
}
}
-- Special cases mapping: Actual title to MediaWiki-safe title
local special_cases = {
["Augustus, You Jerk #2"] = "Augustus, You Jerk 2"
}
-- Function to check if a chapter is within a year range
local function isChapterInYearRange(chapter_year, start_year, end_year)
local year = tonumber(chapter_year)
return year and year >= start_year and year <= end_year
end
-- Function to get the MediaWiki-safe title
function p.getSafeTitle(frame)
local title = frame.args and frame.args.title or frame
return special_cases[title] or title
end
function p.generateNavbox(frame)
local bciChaptersModule = require('Module:BCIChapters')
local chapters = bciChaptersModule.getChaptersList()
-- Determine the last publish year dynamically
local last_publish_year = 0
for _, chapter in ipairs(chapters) do
local year = tonumber(string.sub(chapter.date, 1, 4))
if year and year > last_publish_year then
last_publish_year = year
end
end
-- Update the last group end year to the last publish year
if last_publish_year > 0 then
for _, group in ipairs(groups) do
if group.start_year == 2020 then
group.end_year = last_publish_year
group.name = '2020–' .. last_publish_year
break
end
end
end
local navboxParams = {
bodyclass = 'hlist',
name = 'Bittersweet Club International chapters',
title = "[[Bittersweet Club International]] chapters",
titlestyle = '',
image = '',
above = '',
below = ''
}
-- Add groups and lists
for _, group in ipairs(groups) do
navboxParams[group.group] = group.name
local list = {}
for _, chapter in ipairs(chapters) do
local year = tonumber(string.sub(chapter.date, 1, 4))
if isChapterInYearRange(year, group.start_year, group.end_year) then
local safeTitle = p.getSafeTitle(chapter.title)
local encodedTitle = mw.uri.encode(safeTitle, 'PATH')
table.insert(list, '* [[' .. encodedTitle .. '|' .. chapter.title .. ']]')
end
end
navboxParams[group.list] = table.concat(list, '\n')
end
return frame:expandTemplate{ title = 'Navbox', args = navboxParams }
end
return p