Module:VolDiff

From JoJo's Bizarre Encyclopedia - JoJo Wiki
Revision as of 22:46, 28 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)
    -- Match the entire chapter link
    local fullMatch = mw.ustring.match(chapterLink, "%[%[(.-)%]%]")
    if fullMatch then
        -- Split at the pipe if there is a display title
        local splitMatch = mw.text.split(fullMatch, "|")
        -- Return the first part before the pipe (if exists) or the entire match
        return splitMatch[1]
    end
    return nil
end

-- Helper function to extract content under a specific section header
local function extractSection(content, header)
    local pattern = header .. "(.-)(===)"
    local sectionContent = mw.ustring.match(content, pattern)
    if not sectionContent then
        -- Try to match until the end of the content if the section is the last one
        pattern = header .. "(.-)$"
        sectionContent = mw.ustring.match(content, pattern)
    end
    return sectionContent
end

-- Helper function to capture the entire Desc parameter including nested braces and wikitext links
local function captureDescParameter(diff)
    local descStart, descEnd = mw.ustring.find(diff, "Desc%s*=%s*")
    if not descStart then
        return nil
    end
    
    local depth = 0
    local finalDesc = ""
    for i = descEnd + 1, #diff do
        local char = diff:sub(i, i)
        finalDesc = finalDesc .. char
        if char == '{' or char == '[' then
            depth = depth + 1
        elseif char == '}' or char == ']' then
            depth = depth - 1
        elseif char == '|' and depth == 0 then
            break
        end
    end

    return finalDesc
end

-- Main function to process the DiffBox 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==="
    local result = ""

    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 sectionContent = extractSection(content, sectionHeader)
    if not sectionContent then
        return "Error: No section found with header " .. sectionHeader
    end

    -- Extract the DiffBox content
    local diffBoxContent = mw.ustring.match(sectionContent, "{{DiffBox(.-)}}")
    if not diffBoxContent then
        return "Error: No DiffBox found in the specified section."
    end

    -- Extract DiffBox parameters
    local part = mw.ustring.match(diffBoxContent, "Part%s*=%s*([^|]+)") or ""
    local title = mw.ustring.match(diffBoxContent, "Title%s*=%s*([^|]+)") or ""
    local label1 = mw.ustring.match(diffBoxContent, "Label1%s*=%s*([^|]*)") or ""
    local label2 = mw.ustring.match(diffBoxContent, "Label2%s*=%s*([^|]*)") or ""

    -- Start the DiffBox template
    result = result .. string.format('{{DiffBox|Part=%s|Title=%s', part, title)
    if label1 ~= "" then result = result .. string.format('|Label1=%s', label1) end
    if label2 ~= "" then result = result .. string.format('|Label2=%s', label2) end
    result = result .. '|\n'

    -- Process the section content to extract Diff entries
    local hasDiffs = false
    for diff in mw.ustring.gmatch(sectionContent, "{{Diff(.-)}}") do
        local diffPart = mw.ustring.match(diff, "Part%s*=%s*([^|]+)")
        local diffChapter = mw.ustring.match(diff, "Chapter%s*=%s*(%[%[.-%]%])")
        local diffPage = mw.ustring.match(diff, "Page%s*=%s*([^|]+)")
        local diffImage1 = mw.ustring.match(diff, "Image1%s*=%s*([^|]+)")
        local diffImage2 = mw.ustring.match(diff, "Image2%s*=%s*([^|]+)")
        local diffDesc = captureDescParameter(diff)
        local diffLabel1 = mw.ustring.match(diff, "Label1%s*=%s*([^|]*)") or ""
        local diffLabel2 = mw.ustring.match(diff, "Label2%s*=%s*([^|]*)") or ""

        if diffPart and diffChapter and diffPage and diffImage1 and diffImage2 and diffDesc then
            local chapterNumber = getChapterNumber(diffChapter)

            -- Check if the current page title matches the chapter number
            if chapterNumber and mw.ustring.match(currentTitle, chapterNumber) then
                hasDiffs = true
                result = result .. string.format('{{Diff|Part=%s|Chapter=%s|Page=%s|Image1=%s|Image2=%s|Desc=%s',
                    diffPart, diffChapter, diffPage, diffImage1, diffImage2, diffDesc)

                -- Add optional labels if they are present
                if diffLabel1 ~= "" then result = result .. string.format('|Label1=%s', diffLabel1) end
                if diffLabel2 ~= "" then result = result .. string.format('|Label2=%s', diffLabel2) end

                result = result .. '}}\n'
            end
        end
    end

    -- End the DiffBox template or provide a message if no diffs are found
    if hasDiffs then
        result = result .. '}}'
    else
        result = "No differences found for this chapter."
    end

    return frame:preprocess(result)
end

return p