Module:BCIChapters: Difference between revisions

From Candypedia
No edit summary
Tag: Reverted
No edit summary
Tag: Reverted
Line 3: Line 3:
-- Special cases mapping: Actual title to MediaWiki-safe title
-- Special cases mapping: Actual title to MediaWiki-safe title
local special_cases = {
local special_cases = {
     ["Augustus, You Jerk #2"] = {page = "Augustus, You Jerk 2", seq = 24}
     ["Augustus, You Jerk #2"] = {page = "Augustus, You Jerk 2", seq = 24, display = "Augustus, You Jerk #2"}
}
}


Line 36: Line 36:
     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 59: Line 59:
     local chapters = p.getChaptersList()
     local chapters = p.getChaptersList()
     for _, chapter in ipairs(chapters) do
     for _, chapter in ipairs(chapters) do
         if tonumber(chapter.seq) == tonumber(seq) then
         if chapter.seq == seq then
             return chapter[detail]
             return chapter[detail]
         end
         end
Line 84: Line 84:
     local chapters = p.getChaptersList()
     local chapters = p.getChaptersList()
     for i, chapter in ipairs(chapters) do
     for i, chapter in ipairs(chapters) do
         if tonumber(chapter.seq) == tonumber(seq) and i < #chapters then
         if chapter.seq == seq and i < #chapters then
             local nextTitle = chapters[i + 1].title
             local nextTitle = chapters[i + 1].title
             return special_cases[nextTitle] and special_cases[nextTitle].page or nextTitle
             return special_cases[nextTitle] and special_cases[nextTitle].page or nextTitle
Line 110: Line 110:
     local chapters = p.getChaptersList()
     local chapters = p.getChaptersList()
     for i, chapter in ipairs(chapters) do
     for i, chapter in ipairs(chapters) do
         if tonumber(chapter.seq) == tonumber(seq) and i > 1 then
         if chapter.seq == seq and i > 1 then
             local prevTitle = chapters[i - 1].title
             local prevTitle = chapters[i - 1].title
             return special_cases[prevTitle] and special_cases[prevTitle].page or prevTitle
             return special_cases[prevTitle] and special_cases[prevTitle].page or prevTitle
Line 158: Line 158:
             :css('font-size', '125%')
             :css('font-size', '125%')
             :css('padding', '5px')
             :css('padding', '5px')
             :wikitext(special_cases[chapterTitle] and special_cases[chapterTitle].display or normalizedTitle)
             :wikitext(special_cases[chapterTitle] and special_cases[chapterTitle].display or chapterTitle)
             :done()
             :done()
         :done()
         :done()
Line 234: Line 234:
     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]]", special_cases[prev_chapter] and special_cases[prev_chapter].page or prev_chapter, prev_chapter) or "← Previous")
         :wikitext(prev_chapter ~= "N/A" and string.format("[[%s|← %s]]", special_cases[prev_chapter] and special_cases[prev_chapter].page or prev_chapter, special_cases[prev_chapter] and special_cases[prev_chapter].display or 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 →]]", special_cases[next_chapter] and special_cases[next_chapter].page or next_chapter, next_chapter) or "Next →")
         :wikitext(next_chapter ~= "N/A" and string.format("[[%s|%s →]]", special_cases[next_chapter] and special_cases[next_chapter].page or next_chapter, special_cases[next_chapter] and special_cases[next_chapter].display or next_chapter) or "Next →")
         :done()
         :done()
     nav_row:done()
     nav_row:done()

Revision as of 16:12, 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"] = {page = "Augustus, You Jerk 2", seq = 24, display = "Augustus, You Jerk #2"}
}

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

-- Normalize apostrophes in the title
function p.normalizeApostrophes(str)
    str = string.gsub(str, "’", "'")
    str = string.gsub(str, "&#39;", "'")
    str = string.gsub(str, "%%27", "'")
    return str
end

-- Get the list of chapters
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

    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

-- Get chapter details by title
function p.getChapterDetails(chapterTitle, detail)
    local chapters = p.getChaptersList()
    local decodedTitle = p.urlDecode(chapterTitle)
    local normalizedTitle = p.normalizeApostrophes(decodedTitle)
    for _, chapter in ipairs(chapters) do
        if chapter.title == normalizedTitle then
            return chapter[detail]
        end
    end
    return nil
end

-- Get chapter details by sequence number
function p.getChapterDetailsBySeq(seq, detail)
    local chapters = p.getChaptersList()
    for _, chapter in ipairs(chapters) do
        if chapter.seq == seq then
            return chapter[detail]
        end
    end
    return nil
end

-- Get next chapter by title
function p.getNextChapter(chapterTitle)
    local chapters = p.getChaptersList()
    local decodedTitle = p.urlDecode(chapterTitle)
    local normalizedTitle = p.normalizeApostrophes(decodedTitle)
    for i, chapter in ipairs(chapters) do
        if chapter.title == normalizedTitle and i < #chapters then
            local nextTitle = chapters[i + 1].title
            return special_cases[nextTitle] and special_cases[nextTitle].page or nextTitle
        end
    end
    return nil
end

-- Get next chapter by sequence number
function p.getNextChapterBySeq(seq)
    local chapters = p.getChaptersList()
    for i, chapter in ipairs(chapters) do
        if chapter.seq == seq and i < #chapters then
            local nextTitle = chapters[i + 1].title
            return special_cases[nextTitle] and special_cases[nextTitle].page or nextTitle
        end
    end
    return nil
end

-- Get previous chapter by title
function p.getPreviousChapter(chapterTitle)
    local chapters = p.getChaptersList()
    local decodedTitle = p.urlDecode(chapterTitle)
    local normalizedTitle = p.normalizeApostrophes(decodedTitle)
    for i, chapter in ipairs(chapters) do
        if chapter.title == normalizedTitle and i > 1 then
            local prevTitle = chapters[i - 1].title
            return special_cases[prevTitle] and special_cases[prevTitle].page or prevTitle
        end
    end
    return nil
end

-- Get previous chapter by sequence number
function p.getPreviousChapterBySeq(seq)
    local chapters = p.getChaptersList()
    for i, chapter in ipairs(chapters) do
        if chapter.seq == seq and i > 1 then
            local prevTitle = chapters[i - 1].title
            return special_cases[prevTitle] and special_cases[prevTitle].page or prevTitle
        end
    end
    return nil
end

-- Format date
function p.formatDate(date)
    local year, month, day = string.match(date, "(%d+)%-(%d+)%-(%d+)")
    if year and month and day then
        return string.format("{{Start date|%s|%s|%s}}", year, month, day)
    else
        return date
    end
end

-- Function to get chapter details and populate the infobox
function p.infoboxBCIChapter(frame)
    local chapterTitle = frame.args.chapter_title or mw.title.getCurrentTitle().text
    local decodedTitle = p.urlDecode(chapterTitle)
    local normalizedTitle = p.normalizeApostrophes(decodedTitle)
    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

    -- Handle special cases
    local seq = special_cases[chapterTitle] and special_cases[chapterTitle].seq or p.getChapterDetails(normalizedTitle, "seq") or "N/A"
    local date = p.getChapterDetailsBySeq(seq, "date") or "N/A"
    local pagecount = p.getChapterDetailsBySeq(seq, "pagecount") or "N/A"
    local next_chapter = p.getNextChapterBySeq(seq) or "N/A"
    local prev_chapter = p.getPreviousChapterBySeq(seq) or "N/A"

    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(special_cases[chapterTitle] and special_cases[chapterTitle].display or chapterTitle)
            :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)):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]]", special_cases[prev_chapter] and special_cases[prev_chapter].page or prev_chapter, special_cases[prev_chapter] and special_cases[prev_chapter].display or prev_chapter) or "← Previous")
        :done()
    nav_row:tag('td')
        :css('text-align', 'right')
        :wikitext(next_chapter ~= "N/A" and string.format("[[%s|%s →]]", special_cases[next_chapter] and special_cases[next_chapter].page or next_chapter, special_cases[next_chapter] and special_cases[next_chapter].display or 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 frame:preprocess(tostring(infobox))
end

return p