Module:GetFirstImage: Difference between revisions

From IxWiki
Jump to navigation Jump to search
Created page with "local p = {} function p.getFirstImage(frame) local pageName = frame.args[1] if not pageName then return '' end local page = mw.title.new(pageName) if not page then return '' end local content = page:getContent() if not content then return '' end -- Look for the first [[File: or [[Image: in the content local fileName = content:match("%[%[[Ff]ile:([^%]|]+)") or content:match("%[%[[Ii]mage:([^%]|]+)") if fileName then..."
 
mNo edit summary
Line 3: Line 3:
function p.getFirstImage(frame)
function p.getFirstImage(frame)
     local pageName = frame.args[1]
     local pageName = frame.args[1]
     if not pageName then return '' end
     if not pageName then return 'Placeholderv2.png' end
      
      
     local page = mw.title.new(pageName)
     local page = mw.title.new(pageName)
     if not page then return '' end
     if not page then return 'Placeholderv2.png' end
      
      
     local content = page:getContent()
     local content = page:getContent()
     if not content then return '' end
     if not content then return 'Placeholderv2.png' end
   
    -- Look for the first [[File: or [[Image: in the content
    local fileName = content:match("%[%[[Ff]ile:([^%]|]+)") or content:match("%[%[[Ii]mage:([^%]|]+)")
      
      
    -- Look for images in various formats
    local fileName = content:match("%[%[[Ff]ile:([^%]|]+)") or  -- [[File: syntax
                    content:match("%[%[[Ii]mage:([^%]|]+)") or -- [[Image: syntax
                    content:match("|%s*[Ii]mage%s*=%s*([^%|%}]+)") or -- |image= in templates
                    content:match("|%s*[Ii]mage%d+%s*=%s*([^%|%}]+)") or -- |image1=, |image2=, etc.
                    content:match("{{[Ii]nfobox[^}]*|%s*[Ii]mage%s*=%s*([^%|%}]+)") -- Infobox image parameter
     if fileName then
     if fileName then
        -- Remove any leading or trailing whitespace
        fileName = fileName:match("^%s*(.-)%s*$")
        -- Remove "File:" or "Image:" prefix if present
        fileName = fileName:gsub("^[Ff]ile:", ""):gsub("^[Ii]mage:", "")
         return fileName
         return fileName
     else
     else

Revision as of 14:02, 30 July 2024

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

local p = {}

function p.getFirstImage(frame)
    local pageName = frame.args[1]
    if not pageName then return 'Placeholderv2.png' end
    
    local page = mw.title.new(pageName)
    if not page then return 'Placeholderv2.png' end
    
    local content = page:getContent()
    if not content then return 'Placeholderv2.png' end
    
    -- Look for images in various formats
    local fileName = content:match("%[%[[Ff]ile:([^%]|]+)") or  -- [[File: syntax
                     content:match("%[%[[Ii]mage:([^%]|]+)") or -- [[Image: syntax
                     content:match("|%s*[Ii]mage%s*=%s*([^%|%}]+)") or -- |image= in templates
                     content:match("|%s*[Ii]mage%d+%s*=%s*([^%|%}]+)") or -- |image1=, |image2=, etc.
                     content:match("{{[Ii]nfobox[^}]*|%s*[Ii]mage%s*=%s*([^%|%}]+)") -- Infobox image parameter

    if fileName then
        -- Remove any leading or trailing whitespace
        fileName = fileName:match("^%s*(.-)%s*$")
        -- Remove "File:" or "Image:" prefix if present
        fileName = fileName:gsub("^[Ff]ile:", ""):gsub("^[Ii]mage:", "")
        return fileName
    else
        return 'Placeholderv2.png'  -- Default image if no image is found
    end
end

return p