Module:InfoboxExtractor

From JoJo's Bizarre Encyclopedia - JoJo Wiki
Revision as of 04:29, 5 July 2023 by Vish (talk | contribs)
Jump to navigation Jump to search

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

local p = {}

local function extractInfobox(wikitext)
    local startInfobox = string.find(wikitext, "{{[^}]*Infobox")
    if not startInfobox then
        return nil
    end

    local braceCount = 0
    local endInfobox = startInfobox

    for i = startInfobox, #wikitext do
        local char = wikitext:sub(i, i)
        if char == '{' then
            braceCount = braceCount + 1
        elseif char == '}' then
            braceCount = braceCount - 1
        end

        if braceCount == 0 then
            endInfobox = i
            break
        end
    end

    return wikitext:sub(startInfobox, endInfobox)
end

local function getLabelValue(infobox, label)
    local pattern = "|" .. label .. " *= *"
    local matchStart, matchEnd = string.find(infobox, pattern)
    if not matchStart then
        return nil
    end

    local value = ""
    local braceCount = 0
    local i = matchEnd + 1
    local isNewLine = false

    -- need to do - 2 so it ignores the }} at the end of the infobox
    while i <= #infobox - 2 do
        local char = infobox:sub(i, i)

        if char == '{' then
            braceCount = braceCount + 1
        elseif char == '}' then
            braceCount = braceCount - 1
        end

        if braceCount == 0 and char == '\n' then
            isNewLine = true
        elseif isNewLine and char == '|' then
            break
        else
            isNewLine = false
            value = value .. char
        end

        i = i + 1
    end

    return value
end

local function getImages(value, imageTab)
    local images = {}

    local function processTabber(content)
        local startPos, endPos, tabberContent = string.find(content, "{{Tabber%s*\n?(.-)}}")
        if not startPos or not endPos then
            return
        end

        local tabPattern = "|([^|]+)|%[%[File:([^|%]]+)"
        for tab, image in string.gmatch(tabberContent, tabPattern) do
            local trimmedTab = mw.text.trim(tab)
            if not imageTab or mw.ustring.lower(trimmedTab) == mw.ustring.lower(imageTab) then
                table.insert(images, { tab = trimmedTab, image = image })
                mw.log("Tab: " .. trimmedTab .. " Image: " .. image) -- Debugging statement
            end
        end
    end

    local function processSwitch(content)
        local startPos, endPos, switchContent = string.find(content, "{{Switch%s*\n?(.-)}}")
        if not startPos or not endPos then
            return
        end

        local tabPattern = "|Tab%d+%s*=%s*([^|]+)|%[%[File:([^|%]]+)"
        for tab, image in string.gmatch(switchContent, tabPattern) do
            local trimmedTab = mw.text.trim(tab)
            if not imageTab or mw.ustring.lower(trimmedTab) == mw.ustring.lower(imageTab) then
                table.insert(images, { tab = trimmedTab, image = image })
                mw.log("Tab: " .. trimmedTab .. " Image: " .. image) -- Debugging statement
            end
        end
    end

    local function processNestedTabber(content)
        local startPos, endPos, nestedTabberContent = string.find(content, "{{Tabber%s*\n?(.-)}}")
        if not startPos or not endPos then
            return
        end

        local nestedTabPattern = "|([^|]+)|{{Switch%s*\n?(.-)}}"
        for tab, switchContent in string.gmatch(nestedTabberContent, nestedTabPattern) do
            local trimmedTab = mw.text.trim(tab)
            processSwitch(switchContent)
        end
    end

    -- Process Tabber
    local tabberPattern = "{{Tabber%s*\n?(.-)}}"
    for tabberContent in string.gmatch(value, tabberPattern) do
        processTabber(tabberContent)
        processNestedTabber(tabberContent)
    end

    return images
end

function p.extract(frame)
    local preprocess = frame.args.template == "true"
    local pageTitle = frame.args[1]
    if preprocess then
        pageTitle = frame:preprocess(pageTitle)
    end
    if not pageTitle then
        return "Error: Please provide a page title."
    end

    local page = mw.title.new(pageTitle)
    if not page or not page.exists then
        return "Error: Page not found."
    end

    local wikitext = page:getContent()
    local infobox = extractInfobox(wikitext)

    if not infobox then
        return "Error: Infobox not found."
    end

    local label = frame.args[2]
    if not label then
        return "Error: Please provide a label."
    end

    local imageTab = frame.args.imageTab or nil

    local value = getLabelValue(infobox, label)

    if not value then
        return "Error: Label not found in infobox."
    end

    local images = getImages(value, imageTab)

    if label:lower() == "image" then
        local imageIndex = tonumber(frame.args.imageIndex) or 1
        local size = frame.args.size or ""
        local link = frame.args.link or ""

        if imageIndex < 1 or imageIndex > #images then
            return "Error: Invalid image index."
        end

        local selectedImage = images[imageIndex]
        if link ~= "" then
            return string.format('[[File:%s|%s|link=%s]]', selectedImage.image, size, link)
        else
            return string.format('[[File:%s|%s]]', selectedImage.image, size)
        end
    else
        local processedValue = frame:preprocess(value)
        return processedValue
    end
end

return p