Module:InfoboxImage: Difference between revisions

Jump to navigation Jump to search
imported>WOSlinker
No edit summary
imported>WOSlinker
No edit summary
Line 40: Line 40:
function i.IsPlaceholder(image)
function i.IsPlaceholder(image)
     -- change underscores to spaces
     -- change underscores to spaces
     image2 = mw.ustring.gsub(image, "_", " ");
     image = mw.ustring.gsub(image, "_", " ");
     -- if image starts with [[ then remove that and anything after |
     -- if image starts with [[ then remove that and anything after |
     if mw.ustring.sub(image2,1,2) == "[[" then
     if mw.ustring.sub(image,1,2) == "[[" then
         image2 = mw.ustring.sub(image2,3);
         image = mw.ustring.sub(image,3);
         image2 = mw.ustring.gsub(image2, "([^|]*)|.*", "%1");
         image = mw.ustring.gsub(image, "([^|]*)|.*", "%1");
     end
     end
    -- Trim spaces
    image = mw.ustring.gsub(image, '^[ ]*(.-)[ ]*$', '%1');
     -- remove file: or image: prefix if exists
     -- remove file: or image: prefix if exists
     if mw.ustring.lower(mw.ustring.sub(image2,1,5)) == "file:" then
     if mw.ustring.lower(mw.ustring.sub(image,1,5)) == "file:" then
         image2 = mw.ustring.sub(image2,6,mw.ustring.len(image2));
         image = mw.ustring.sub(image,6,mw.ustring.len(image));
     end
     end
     if mw.ustring.lower(mw.ustring.sub(image2,1,6)) == "image:" then
     if mw.ustring.lower(mw.ustring.sub(image,1,6)) == "image:" then
         image2 = mw.ustring.sub(image2,7,mw.ustring.len(image2));
         image = mw.ustring.sub(image,7,mw.ustring.len(image));
     end
     end
    -- Trim spaces
    image = mw.ustring.gsub(image, '^[ ]*(.-)[ ]*$', '%1');
     -- capitalise first letter
     -- capitalise first letter
     image2 = mw.ustring.upper(mw.ustring.sub(image2,1,1)) .. mw.ustring.sub(image2,2);
     image = mw.ustring.upper(mw.ustring.sub(image,1,1)) .. mw.ustring.sub(image,2);


     for i,j in pairs(placeholder_image) do
     for i,j in pairs(placeholder_image) do
         if image2 == j then
         if image == j then
             return true
             return true
         end
         end

Revision as of 21:39, 15 March 2013

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

-- Inputs:
--    image - Can either be a bare filename or a fully formatted image link
--    size - size to display the image
--    sizedefault - default size to display the image if size param is blank
--    alt - alt text for image
--    suppressplaceholder - if yes then checks to see if image is a placeholder and suppresses it
-- Outputs:
--    Formatted image.

local i = {};

local placeholder_image = {
    "Blue - Replace this image female.svg",
    "Blue - Replace this image male.svg",
    "Female no free image yet.png",
    "Flag of None (square).svg",
    "Flag of None.svg",
    "Flag of.svg",
    "Green - Replace this image female.svg",
    "Green - Replace this image male.svg",
    "Image is needed female.svg",
    "Image is needed male.svg",
    "Location map of None.svg",
    "Male no free image yet.png",
    "Missing flag.png",
    "No flag.svg",
    "No free portrait.svg",
    "No portrait (female).svg",
    "No portrait (male).svg",
    "Red - Replace this image female.svg",
    "Red - Replace this image male.svg",
    "Replace this image female (blue).svg",
    "Replace this image female.svg",
    "Replace this image male (blue).svg",
    "Replace this image male.svg",
    "Silver - Replace this image female.svg",
    "Silver - Replace this image male.svg",
}

function i.IsPlaceholder(image)
    -- change underscores to spaces
    image = mw.ustring.gsub(image, "_", " ");
    -- if image starts with [[ then remove that and anything after |
    if mw.ustring.sub(image,1,2) == "[[" then
        image = mw.ustring.sub(image,3);
        image = mw.ustring.gsub(image, "([^|]*)|.*", "%1");
    end
    -- Trim spaces
    image = mw.ustring.gsub(image, '^[ ]*(.-)[ ]*$', '%1');
    -- remove file: or image: prefix if exists
    if mw.ustring.lower(mw.ustring.sub(image,1,5)) == "file:" then
        image = mw.ustring.sub(image,6,mw.ustring.len(image));
    end
    if mw.ustring.lower(mw.ustring.sub(image,1,6)) == "image:" then
        image = mw.ustring.sub(image,7,mw.ustring.len(image));
    end
    -- Trim spaces
    image = mw.ustring.gsub(image, '^[ ]*(.-)[ ]*$', '%1');
    -- capitalise first letter
    image = mw.ustring.upper(mw.ustring.sub(image,1,1)) .. mw.ustring.sub(image,2);

    for i,j in pairs(placeholder_image) do
        if image == j then
            return true
        end
    end
    return false
end

function i.InfoboxImage(frame)
    local image = frame.args["image"];
    
    if image == "" or image == nil then
        return "";
    end
    if frame.args["suppressplaceholder"] == "yes" then
        if i.IsPlaceholder(image) == true then
            return "";
        end
    end

    if mw.ustring.sub(image,1,2) == "[[" then
        return image;
    else
        local result = "";
        local size = frame.args["size"];
        local sizedefault = frame.args["sizedefault"];
        local alt = frame.args["alt"];
        
        -- remove file: or image: prefix if exists
        if mw.ustring.lower(mw.ustring.sub(image,1,5)) == "file:" then
            image = mw.ustring.sub(image,6,mw.ustring.len(image));
        end
        if mw.ustring.lower(mw.ustring.sub(image,1,6)) == "image:" then
            image = mw.ustring.sub(image,7,mw.ustring.len(image));
        end
        
        -- add px to size if just a number
        if (tonumber(size) or 0) > 0 then
            size = size .. "px";
        end
        
        result = "[[File:" .. image;
        if size ~= "" and size ~= nil then
            result = result .. "|" .. size;
        elseif sizedefault ~= "" and sizedefault ~= nil then
            result = result .. "|" .. sizedefault;
        else
            result = result .. "|frameless";
        end
        if alt ~= "" and alt ~= nil then
            result = result .. "|alt=" .. alt;
        end
        result = result .. "]]";
        
        return result;
    end
end

return i;