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
No edit summary
 
(40 intermediate revisions by 2 users not shown)
Line 1: Line 1:
local p = {}
local p = {}

local ExtractSection = require("Module:ExtractSection")


local function extractInfobox(wikitext)
local function extractInfobox(wikitext)
Line 64: Line 66:
end
end


local function getImages(value, imageTab)
local function getImages(value)
local images = {}
local images = {}
local pattern = "%[%[File:([^|%]]+)"

for image in string.gmatch(value, pattern) do
local function processTabber(content)
table.insert(images, image)
local startPattern = "{{Tabber%s*\n?"
end
local endPattern = "}}"
local startPos, endPos, tabberContent = string.find(content, startPattern .. "(.-)" .. endPattern)
if startPos and endPos then
local tabPattern = "|%s*([^|]-)%s*|%s*%[%[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
return image
end
end
else
local pattern = "%[%[File:([^|%]]+)"
local image = string.match(content, pattern)
if image then
return image
end
end
return nil
end

processTabber(value)

return images
return images
end
end
Line 103: Line 82:
end
end
if not pageTitle then
if not pageTitle then
return "Error: Please provide a page title."
return "<strong class=\"error\">Error: Please provide a page title.</strong>"
end
end


local page = mw.title.new(pageTitle)
local page = mw.title.new(pageTitle)
if not page or not page.exists then
if not page or not page.exists then
return "Error: Page not found."
return "<strong class=\"error\">Error: Page not found.</strong>"
end
end


Line 115: Line 94:


if not infobox then
if not infobox then
return "Error: Infobox not found."
return "<strong class=\"error\">Error: Infobox not found.</strong>"
end
end


local label = frame.args[2]
local label = frame.args[2]
if not label then
if not label then
return "Error: Please provide a label."
return "<strong class=\"error\">Error: Please provide a label.</strong>"
end
end


local value = getLabelValue(infobox, label)
local value = getLabelValue(infobox, label)

if not value then
if not value then
return "Error: Label not found in infobox."
return "<strong class=\"error\">Error: Label not found in infobox.</strong>"
end
local tab = frame.args.tab or ""
if tab ~= "" then
local tabValue = ExtractSection.extractTab(value, frame.args.tab)
if tabValue then
value = tabValue
end
end

if not value and frame.args.tab then
return "<strong class=\"error\">Error: Tab not found in infobox.</strong>"
end
end


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

for _, image in ipairs(images) do
if image.tab == imageTab then
if imageIndex < 1 or imageIndex > #images then
selectedImage = image.image
return "Error: Invalid image index."
break
end

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

Latest revision as of 18:50, 23 December 2024

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

local p = {}

local ExtractSection = require("Module:ExtractSection")

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)
    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 preprocess = frame.args.template == "true"
    local pageTitle = frame.args[1]
    if preprocess then
        pageTitle = frame:preprocess(pageTitle)
    end
    if not pageTitle then
        return "<strong class=\"error\">Error: Please provide a page title.</strong>"
    end

    local page = mw.title.new(pageTitle)
    if not page or not page.exists then
        return "<strong class=\"error\">Error: Page not found.</strong>"
    end

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

    if not infobox then
        return "<strong class=\"error\">Error: Infobox not found.</strong>"
    end

    local label = frame.args[2]
    if not label then
        return "<strong class=\"error\">Error: Please provide a label.</strong>"
    end

    local value = getLabelValue(infobox, label)
    if not value then
        return "<strong class=\"error\">Error: Label not found in infobox.</strong>"
    end
    local tab = frame.args.tab or ""
    if tab ~= "" then
        local tabValue = ExtractSection.extractTab(value, frame.args.tab)
        if tabValue then
            value = tabValue
        end
    end

    if not value and frame.args.tab then
        return "<strong class=\"error\">Error: Tab not found in infobox.</strong>"
    end

    if label:lower() == "image" then
        local images = getImages(value)
        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, size, link)
        else
        	return string.format('[[File:%s|%s]]', selectedImage, size)
        end
    else
        local processedValue = frame:preprocess(value)
        return processedValue
    end
end

return p