Module:InfoboxExtractor: Difference between revisions

From JoJo's Bizarre Encyclopedia - JoJo Wiki
Jump to navigation Jump to search
Content deleted Content added
No edit summary
Tag: Manual revert
No edit summary
Tag: Reverted
Line 64: Line 64:
end
end


local function getImages(value)
local function getImages(value, imageTab, imageTab2)
local images = {}
local images = {}
local pattern = "%[%[File:([^|%]]+)"
local pattern = "%[%[File:([^|%]]+)"
local tabPattern = "|([^|\n]+)|%[%[File:([^|%]]+)"
for image in string.gmatch(value, pattern) do
table.insert(images, image)
-- Check if there is a tabber template
if string.find(value, "{{Tabber") then
-- Extract images for each tab
for tabName, tabImage in string.gmatch(value, tabPattern) do
if imageTab then
-- If imageTab is specified, check if it matches the tab name
if tabName == imageTab then
table.insert(images, tabImage)
end
elseif imageTab2 then
-- If imageTab2 is specified, check if it matches the tab name
if tabName == imageTab2 then
table.insert(images, tabImage)
end
else
-- If no imageTab or imageTab2 is specified, add all tab images to the images table
table.insert(images, tabImage)
end
end
else
-- If there is no tabber template, extract images as before
for image in string.gmatch(value, pattern) do
table.insert(images, image)
end
end
end

return images
return images
end
end


function p.extract(frame)
function p.extract(frame)
local preprocess = frame.args.template == "true"
local preprocess = frame.args.template == "true"
local pageTitle = frame.args[1]
local pageTitle = frame.args[1]
if preprocess then
if preprocess then
Line 105: Line 130:
return "Error: Label not found in infobox."
return "Error: Label not found in infobox."
end
end

local imageTab = frame.args.imageTab
local imageTab2 = frame.args.imageTab2


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


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


local selectedImage = images[imageIndex]
local selectedImage = images[1] -- Get the first image
if link ~= "" then
if link ~= "" then
return string.format('[[File:%s|%s|link=%s]]', selectedImage, size, link)
return string.format('[[File:%s|%s|link=%s]]', selectedImage, size, link)
else
else
return string.format('[[File:%s|%s]]', selectedImage, size)
return string.format('[[File:%s|%s]]', selectedImage, size)
end
end
else
else

Revision as of 04:48, 5 July 2023

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, imageTab2)
    local images = {}
    local pattern = "%[%[File:([^|%]]+)"
    local tabPattern = "|([^|\n]+)|%[%[File:([^|%]]+)"
    
    -- Check if there is a tabber template
    if string.find(value, "{{Tabber") then
        -- Extract images for each tab
        for tabName, tabImage in string.gmatch(value, tabPattern) do
            if imageTab then
                -- If imageTab is specified, check if it matches the tab name
                if tabName == imageTab then
                    table.insert(images, tabImage)
                end
            elseif imageTab2 then
                -- If imageTab2 is specified, check if it matches the tab name
                if tabName == imageTab2 then
                    table.insert(images, tabImage)
                end
            else
                -- If no imageTab or imageTab2 is specified, add all tab images to the images table
                table.insert(images, tabImage)
            end
        end
    else
        -- If there is no tabber template, extract images as before
        for image in string.gmatch(value, pattern) do
            table.insert(images, image)
        end
    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 value = getLabelValue(infobox, label)

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

    local imageTab = frame.args.imageTab
    local imageTab2 = frame.args.imageTab2

    if label:lower() == "image" then
        local images = getImages(value, imageTab, imageTab2)
        local size = frame.args.size or ""
        local link = frame.args.link or ""

        if #images == 0 then
            return "Error: No image found."
        end

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

return p