Module:VolDiff

From JoJo's Bizarre Encyclopedia - JoJo Wiki
Revision as of 09:47, 31 July 2024 by Vish (talk | contribs)
Jump to navigation Jump to search

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

local p = {}

-- Helper function to parse chapter number from the chapter link
local function getChapterNumber(chapterLink)
    local fullMatch = mw.ustring.match(chapterLink, "%[%[(.-)%]%]")
    if fullMatch then
        local splitMatch = mw.text.split(fullMatch, "|")
        return splitMatch[1]
    end
    return nil
end

-- Helper function to extract content under a specific section header
local function extractSection(content, header)
    -- Define the pattern to find the section header
    local startPattern = mw.ustring.gsub(header, "=", "%%=")
    -- Find the start of the specified header
    local sectionStart, sectionEnd = mw.ustring.find(content, startPattern)
    -- If the section is not found, return nil
    if not sectionStart then
        return nil
    end
    -- Find the next section header (either === or higher)
    local nextHeaderPattern = "\n==+%s*[^=]"
    local nextHeaderStart, nextHeaderEnd = mw.ustring.find(content, nextHeaderPattern, sectionEnd + 1)
    -- Extract the content until the next section header or the end of the content
    if nextHeaderStart then
        return mw.ustring.sub(content, sectionEnd + 1, nextHeaderStart - 1)
    else
        return mw.ustring.sub(content, sectionEnd + 1)
    end
end

-- Function to find all Diff positions
local function findDiffPositions(content)
    local positions = {}
    local startPos = 1

    while true do
        local diffStart, diffEnd = mw.ustring.find(content, "{{%s*Diff%s*[|\n]", startPos)
        if not diffStart then
            break
        end
        positions[#positions + 1] = {diffStart, diffEnd}
        startPos = diffEnd + 1
    end

    return positions
end

-- Function to find the end position of the VolDiffs template
local function findEndOfVolDiffs(content)
    local endPos = mw.ustring.find(content, "}}%s*$")
    if endPos then
        return endPos + 2
    else
        return #content
    end
end

-- Function to remove non-matching Diff templates
local function removeNonMatchingDiffs(content, currentTitle)
    local diffPositions = findDiffPositions(content)
    local newContent = content
    local nonMatchingDiffs = {}

    local volDiffsEndPos = findEndOfVolDiffs(content)

    for i, pos in ipairs(diffPositions) do
        local diffStart = pos[1]
        local diffEnd = i < #diffPositions and diffPositions[i + 1][1] - 1 or volDiffsEndPos - 3 -- Adjust to avoid including the VolDiffs closing braces
        local diffContent = mw.ustring.sub(content, diffStart, diffEnd)
        local diffChapter = mw.ustring.match(diffContent, "Chapter%s*=%s*(%[%[.-%]%])")
        local chapterNumber = getChapterNumber(diffChapter)

        mw.log('Processing Diff: ', diffContent)

        -- Check if the current page title matches the chapter number
        if not (chapterNumber and mw.ustring.match(currentTitle, chapterNumber)) then
            table.insert(nonMatchingDiffs, {diffStart, diffEnd})
            mw.log('Removing Diff: ', diffContent)
        end
    end

    -- Remove non-matching Diff entries
    for i = #nonMatchingDiffs, 1, -1 do
        local pos = nonMatchingDiffs[i]
        newContent = mw.ustring.sub(newContent, 1, pos[1] - 1) .. mw.ustring.sub(newContent, pos[2] + 1)
    end

    mw.log('Final VolDiffs content: ', newContent)

    return newContent
end

-- Main function to process the VolDiffs template
function p.main(frame)
    local args = frame:getParent().args
    local currentTitle = mw.title.getCurrentTitle().text
    local sourcePage = args[1]
    local sectionType = args[2] or "magazine"
    local sectionHeader = sectionType == "reprint" and "===Reprint Differences===" or "===Magazine Differences==="

    if not sourcePage then
        return "Error: No source page specified."
    end

    -- Fetch the content from the specified source page
    local content = mw.title.new(sourcePage):getContent()
    if not content then
        return "Error: Could not fetch content from the specified source page."
    end

    -- Extract the section content
    local volDiffsContent = extractSection(content, sectionHeader)
    if not volDiffsContent then
        return "Error: No section found with header " .. sectionHeader
    end

    mw.log('Initial VolDiffs content: ', volDiffsContent)

    -- Remove non-matching Diff entries
    volDiffsContent = removeNonMatchingDiffs(volDiffsContent, currentTitle)

    mw.log('Processed VolDiffs content: ', volDiffsContent)

    return frame:preprocess(volDiffsContent)
end

return p