Module:InfoboxExtractor

From JoJo's Bizarre Encyclopedia - JoJo Wiki
Revision as of 14:08, 1 May 2023 by Vish (talk | contribs)
Jump to navigation Jump to search

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

local function getImages(value)
    local images = {}
    local pattern = "%[%[File:([^|%]]+)"
    for image in string.gmatch(value, pattern) do
        table.insert(images, image)
    end
    return images
end

function p.extract(frame)
    local pageTitle = frame.args[1]
    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 value = getLabelValue(infobox, label)

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

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

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

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