Module:BCIChapters: Difference between revisions

From Candypedia
No edit summary
No edit summary
Line 47: Line 47:
     local chapters = {}
     local chapters = {}
     for seq, title, date, pagecount in string.gmatch(content, "{{BCIChapter|seq=(%d+)|title=([^|]+)|date=([^|]+)|pagecount=(%d+)}}") do
     for seq, title, date, pagecount in string.gmatch(content, "{{BCIChapter|seq=(%d+)|title=([^|]+)|date=([^|]+)|pagecount=(%d+)}}") do
         table.insert(chapters, {seq = seq, title = title, date = date, pagecount = tonumber(pagecount)})
         table.insert(chapters, {seq = tonumber(seq), title = title, date = date, pagecount = tonumber(pagecount)})
     end
     end


Line 53: Line 53:
end
end


function p.getChapterDetails(chapterTitle, detail)
function p.getChapterBySeq(seq)
     local chapters = p.getChaptersList()
     local chapters = p.getChaptersList()
    local safeTitle = p.getSafeTitle(chapterTitle)
    local decodedTitle = p.urlDecode(safeTitle)
    local normalizedTitle = p.normalizeApostrophes(decodedTitle)
     for _, chapter in ipairs(chapters) do
     for _, chapter in ipairs(chapters) do
         if chapter.title == normalizedTitle then
         if chapter.seq == tonumber(seq) then
             return chapter[detail]
             return chapter
         end
         end
     end
     end
Line 66: Line 63:
end
end


function p.getNextChapter(chapterTitle)
function p.getChapterDetailsBySeq(seq, detail)
    local chapter = p.getChapterBySeq(seq)
    if chapter then
        return chapter[detail]
    end
    return nil
end
 
function p.getNextChapterBySeq(seq)
     local chapters = p.getChaptersList()
     local chapters = p.getChaptersList()
    local safeTitle = p.getSafeTitle(chapterTitle)
    local decodedTitle = p.urlDecode(safeTitle)
    local normalizedTitle = p.normalizeApostrophes(decodedTitle)
     for i, chapter in ipairs(chapters) do
     for i, chapter in ipairs(chapters) do
         if chapter.title == normalizedTitle and i < #chapters then
         if chapter.seq == tonumber(seq) and i < #chapters then
             return p.getOriginalTitle(chapters[i + 1].title)
             return chapters[i + 1].title
         end
         end
     end
     end
Line 79: Line 81:
end
end


function p.getPreviousChapter(chapterTitle)
function p.getPreviousChapterBySeq(seq)
     local chapters = p.getChaptersList()
     local chapters = p.getChaptersList()
    local safeTitle = p.getSafeTitle(chapterTitle)
    local decodedTitle = p.urlDecode(safeTitle)
    local normalizedTitle = p.normalizeApostrophes(decodedTitle)
     for i, chapter in ipairs(chapters) do
     for i, chapter in ipairs(chapters) do
         if chapter.title == normalizedTitle and i > 1 then
         if chapter.seq == tonumber(seq) and i > 1 then
             return p.getOriginalTitle(chapters[i - 1].title)
             return chapters[i - 1].title
         end
         end
     end
     end
Line 102: Line 101:


function p.infoboxBCIChapter(frame)
function p.infoboxBCIChapter(frame)
     local chapterTitle = frame.args.chapter_title or mw.title.getCurrentTitle().text
     local chapterSeq = frame.args.seq or "N/A"
     local safeTitle = p.getSafeTitle(chapterTitle)
    local chapter = p.getChapterBySeq(chapterSeq)
     local originalTitle = p.getOriginalTitle(safeTitle)
   
    if not chapter then
        return "No data found for chapter sequence number: " .. chapterSeq
    end
 
    local title = chapter.title
    local date = chapter.date
    local pagecount = chapter.pagecount
     local next_chapter = p.getNextChapterBySeq(chapterSeq) or "N/A"
     local prev_chapter = p.getPreviousChapterBySeq(chapterSeq) or "N/A"
 
     local image = frame.args.image
     local image = frame.args.image
     local caption = frame.args.caption
     local caption = frame.args.caption
Line 112: Line 121:


     caption = caption and caption ~= "" and caption or nil
     caption = caption and caption ~= "" and caption or nil
    local seq = p.getChapterDetails(chapterTitle, "seq") or "N/A"
    local date = p.getChapterDetails(chapterTitle, "date") or "N/A"
    local pagecount = p.getChapterDetails(chapterTitle, "pagecount") or "N/A"
    local next_chapter = p.getNextChapter(chapterTitle) or "N/A"
    local prev_chapter = p.getPreviousChapter(chapterTitle) or "N/A"


     local infobox = mw.html.create('table')
     local infobox = mw.html.create('table')
Line 129: Line 132:
             :css('font-size', '125%')
             :css('font-size', '125%')
             :css('padding', '5px')
             :css('padding', '5px')
             :wikitext(originalTitle)
             :wikitext(title)
             :done()
             :done()
         :done()
         :done()
Line 205: Line 208:
     nav_row:tag('td')
     nav_row:tag('td')
         :css('text-align', 'left')
         :css('text-align', 'left')
         :wikitext(prev_chapter ~= "N/A" and string.format("[[%s:%s|← %s]]", mw.title.getCurrentTitle().nsText, p.getSafeTitle(prev_chapter), p.getOriginalTitle(prev_chapter)) or "← Previous")
         :wikitext(prev_chapter ~= "N/A" and string.format("[[%s|← %s]]", p.getSafeTitle(prev_chapter), prev_chapter) or "← Previous")
         :done()
         :done()
     nav_row:tag('td')
     nav_row:tag('td')
         :css('text-align', 'right')
         :css('text-align', 'right')
         :wikitext(next_chapter ~= "N/A" and string.format("[[%s:%s|%s →]]", mw.title.getCurrentTitle().nsText, p.getSafeTitle(next_chapter), p.getOriginalTitle(next_chapter)) or "Next →")
         :wikitext(next_chapter ~= "N/A" and string.format("[[%s|%s →]]", p.getSafeTitle(next_chapter), next_chapter) or "Next →")
         :done()
         :done()
     nav_row:done()
     nav_row:done()

Revision as of 03:47, 27 May 2024

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

local p = {}

-- Special cases mapping: Actual title to MediaWiki-safe title
local special_cases = {
    ["Augustus, You Jerk #2"] = "Augustus, You Jerk 2"
}

function p.urlDecode(str)
    str = string.gsub(str, '%%(%x%x)', function(x) return string.char(tonumber(x, 16)) end)
    return str
end

function p.normalizeApostrophes(str)
    str = string.gsub(str, "’", "'")
    str = string.gsub(str, "&#39;", "'")
    str = string.gsub(str, "%%27", "'")
    return str
end

function p.getSafeTitle(title)
    return special_cases[title] or title
end

function p.getOriginalTitle(safeTitle)
    for original, safe in pairs(special_cases) do
        if safe == safeTitle then
            return original
        end
    end
    return safeTitle
end

function p.getChaptersList()
    local listPage = mw.title.new('Template:BCIChapterList')
    if not listPage then
        return {}
    end

    local content = listPage:getContent()
    if not content then
        return {}
    end

    -- Normalize apostrophes in content
    content = p.normalizeApostrophes(content)

    local chapters = {}
    for seq, title, date, pagecount in string.gmatch(content, "{{BCIChapter|seq=(%d+)|title=([^|]+)|date=([^|]+)|pagecount=(%d+)}}") do
        table.insert(chapters, {seq = tonumber(seq), title = title, date = date, pagecount = tonumber(pagecount)})
    end

    return chapters
end

function p.getChapterBySeq(seq)
    local chapters = p.getChaptersList()
    for _, chapter in ipairs(chapters) do
        if chapter.seq == tonumber(seq) then
            return chapter
        end
    end
    return nil
end

function p.getChapterDetailsBySeq(seq, detail)
    local chapter = p.getChapterBySeq(seq)
    if chapter then
        return chapter[detail]
    end
    return nil
end

function p.getNextChapterBySeq(seq)
    local chapters = p.getChaptersList()
    for i, chapter in ipairs(chapters) do
        if chapter.seq == tonumber(seq) and i < #chapters then
            return chapters[i + 1].title
        end
    end
    return nil
end

function p.getPreviousChapterBySeq(seq)
    local chapters = p.getChaptersList()
    for i, chapter in ipairs(chapters) do
        if chapter.seq == tonumber(seq) and i > 1 then
            return chapters[i - 1].title
        end
    end
    return nil
end

function p.formatDate(date, frame)
    local year, month, day = string.match(date, "(%d+)%-(%d+)%-(%d+)")
    if year and month and day then
        return frame:expandTemplate{title = "Start date", args = {year, month, day}}
    else
        return date
    end
end

function p.infoboxBCIChapter(frame)
    local chapterSeq = frame.args.seq or "N/A"
    local chapter = p.getChapterBySeq(chapterSeq)
    
    if not chapter then
        return "No data found for chapter sequence number: " .. chapterSeq
    end

    local title = chapter.title
    local date = chapter.date
    local pagecount = chapter.pagecount
    local next_chapter = p.getNextChapterBySeq(chapterSeq) or "N/A"
    local prev_chapter = p.getPreviousChapterBySeq(chapterSeq) or "N/A"

    local image = frame.args.image
    local caption = frame.args.caption
    local major_characters = frame.args.major_characters
    local minor_characters = frame.args.minor_characters
    local locations = frame.args.locations

    caption = caption and caption ~= "" and caption or nil

    local infobox = mw.html.create('table')
    infobox:addClass('infobox')

    -- Add title as heading
    infobox:tag('tr')
        :tag('th')
            :attr('colspan', '2')
            :css('text-align', 'center')
            :css('font-size', '125%')
            :css('padding', '5px')
            :wikitext(title)
            :done()
        :done()

    -- Optionally add image with standard size
    if image and image ~= "" then
        infobox:tag('tr')
            :tag('td')
                :attr('colspan', '2')
                :css('text-align', 'center')
                :wikitext('[[File:' .. image .. '|250px]]')
                :done()
            :done()

        -- Add caption below image if provided
        if caption then
            infobox:tag('tr')
                :tag('td')
                    :attr('colspan', '2')
                    :css('text-align', 'center')
                    :css('font-size', '90%')
                    :css('padding', '5px')
                    :wikitext(caption)
                    :done()
                :done()
        end
    end

    infobox:tag('tr')
        :tag('th'):wikitext('Published'):done()
        :tag('td'):wikitext(p.formatDate(date, frame)):done()
        :done()

    infobox:tag('tr')
        :tag('th'):wikitext('Page count'):done()
        :tag('td'):wikitext(pagecount):done()
        :done()

    -- Add additional manual entry parameters
    if major_characters and major_characters ~= "" then
        infobox:tag('tr')
            :tag('th'):wikitext('Major characters'):done()
            :tag('td'):wikitext(major_characters):done()
            :done()
    end

    if minor_characters and minor_characters ~= "" then
        infobox:tag('tr')
            :tag('th'):wikitext('Minor characters'):done()
            :tag('td'):wikitext(minor_characters):done()
            :done()
    end

    if locations and locations ~= "" then
        infobox:tag('tr')
            :tag('th'):wikitext('Locations'):done()
            :tag('td'):wikitext(locations):done()
            :done()
    end

    -- Add chapter navigation heading
    infobox:tag('tr')
        :tag('th')
            :attr('colspan', '2')
            :css('text-align', 'center')
            :css('font-size', '110%')
            :css('padding', '5px')
            :css('background-color', '#f0f0f0')
            :wikitext('Chapter navigation')
            :done()
        :done()

    -- Add chapter navigation links in separate columns
    local nav_row = infobox:tag('tr')
    nav_row:tag('td')
        :css('text-align', 'left')
        :wikitext(prev_chapter ~= "N/A" and string.format("[[%s|← %s]]", p.getSafeTitle(prev_chapter), prev_chapter) or "← Previous")
        :done()
    nav_row:tag('td')
        :css('text-align', 'right')
        :wikitext(next_chapter ~= "N/A" and string.format("[[%s|%s →]]", p.getSafeTitle(next_chapter), next_chapter) or "Next →")
        :done()
    nav_row:done()

    -- Add link to read chapter in BCI Members' Library
    infobox:tag('tr')
        :tag('td')
            :attr('colspan', '2')
            :css('text-align', 'center')
            :wikitext("[https://bittersweetcandybowl.com/members/ Read in the BCI Members' Library]")
            :done()
        :done()

    -- Return the complete wikitext
    return tostring(infobox)
end

return p