Module:VolDiff

From JoJo's Bizarre Encyclopedia - JoJo Wiki
Revision as of 20:12, 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)
    local match = mw.ustring.match(chapterLink, "Chapter (%d+)")
    return match
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 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 DiffBox and its parameters
    local diffBoxContent = mw.ustring.match(content, "{{DiffBox(.-)}}")

    if not diffBoxContent then
        return "Error: No DiffBox found on the specified source page."
    end

    local diffBoxArgs = mw.text.split(diffBoxContent, "|")
    local part, title, label1, label2

    for _, arg in ipairs(diffBoxArgs) do
        local key, value = mw.ustring.match(arg, "(%w+)%s*=%s*(.*)")
        if key and value then
            key = mw.text.trim(key)
            value = mw.text.trim(value)

            if key == "Part" then
                part = value
            elseif key == "Title" then
                title = value
            elseif key == "Label1" then
                label1 = value
            elseif key == "Label2" then
                label2 = value
            end
        end
    end

    -- Start the DiffBox template
    result = result .. string.format('{{DiffBox|Part=%s|Title=%s', part, title)

    -- Add optional labels if they are present
    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 fetched content to extract Diff entries
    local hasDiffs = false
    for diff in mw.ustring.gmatch(content, "{{Diff(.-)}}") do
        local diffArgs = mw.text.split(diff, "|")
        local diffPart, diffChapter, diffPage, diffImage1, diffImage2, diffDesc, diffLabel1, diffLabel2

        for _, arg in ipairs(diffArgs) do
            local key, value = mw.ustring.match(arg, "(%w+)%s*=%s*(.*)")
            if key and value then
                key = mw.text.trim(key)
                value = mw.text.trim(value)

                if key == "Part" then
                    diffPart = value
                elseif key == "Chapter" then
                    diffChapter = value
                elseif key == "Page" then
                    diffPage = value
                elseif key == "Image1" then
                    diffImage1 = value
                elseif key == "Image2" then
                    diffImage2 = value
                elseif key == "Desc" then
                    diffDesc = value
                elseif key == "Label1" then
                    diffLabel1 = value
                elseif key == "Label2" then
                    diffLabel2 = value
                end
            end
        end

        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