Module:VolDiff

From JoJo's Bizarre Encyclopedia - JoJo Wiki
Revision as of 20:05, 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 result = ""

    -- Extract the main parameters for DiffBox
    local part = args.Part or ""
    local title = args.Title or ""

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

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

    result = result .. '|\n'

    -- Iterate through each Diff entry
    local hasDiffs = false
    for i = 1, 50 do
        local part = args["Part" .. i]
        local chapter = args["Chapter" .. i]
        local page = args["Page" .. i]
        local image1 = args["Image1" .. i]
        local image2 = args["Image2" .. i]
        local desc = args["Desc" .. i]
        local diffLabel1 = args["Label1" .. i]
        local diffLabel2 = args["Label2" .. i]

        if part and chapter and page and image1 and image2 and desc then
            local chapterNumber = getChapterNumber(chapter)

            -- 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',
                    part, chapter, page, image1, image2, desc)

                -- 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