Module:InfoboxImage

From Viki
Jump to navigation Jump to search

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

  1 -- Inputs:
  2 --    image - Can either be a bare filename (with or without the File:/Image: prefix) or a fully formatted image link
  3 --    page - page to display for multipage images (DjVu)
  4 --    size - size to display the image
  5 --    maxsize - maximum size for image
  6 --    sizedefault - default size to display the image if size param is blank
  7 --    alt - alt text for image
  8 --    title - title text for image
  9 --    border - set to yes if border
 10 --    center - set to yes, if the image has to be centered
 11 --    upright - upright image param
 12 --    suppressplaceholder - if yes then checks to see if image is a placeholder and suppresses it
 13 --    link - page to visit when clicking on image
 14 --    class - HTML classes to add to the image
 15 -- Outputs:
 16 --    Formatted image.
 17 -- More details available at the "Module:InfoboxImage/doc" page
 18 
 19 local i = {};
 20 
 21 local placeholder_image = {
 22     "Blue - Replace this image female.svg",
 23     "Blue - Replace this image male.svg",
 24     "Female no free image yet.png",
 25     "Flag of None (square).svg",
 26     "Flag of None.svg",
 27     "Flag of.svg",
 28     "Green - Replace this image female.svg",
 29     "Green - Replace this image male.svg",
 30     "Image is needed female.svg",
 31     "Image is needed male.svg",
 32     "Location map of None.svg",
 33     "Male no free image yet.png",
 34     "Missing flag.png",
 35     "No flag.svg",
 36     "No free portrait.svg",
 37     "No portrait (female).svg",
 38     "No portrait (male).svg",
 39     "Red - Replace this image female.svg",
 40     "Red - Replace this image male.svg",
 41     "Replace this image female (blue).svg",
 42     "Replace this image female.svg",
 43     "Replace this image male (blue).svg",
 44     "Replace this image male.svg",
 45     "Silver - Replace this image female.svg",
 46     "Silver - Replace this image male.svg",
 47     "Replace this image.svg",
 48 	"Cricket no pic.png",
 49 	"CarersLogo.gif",
 50 	"Diagram Needed.svg",
 51 	"Example.jpg",
 52 	"Image placeholder.png",
 53 	"No male portrait.svg",
 54 	"Nocover-upload.png",
 55 	"NoDVDcover copy.png",
 56 	"Noribbon.svg",
 57 	"No portrait-BFD-test.svg",
 58 	"Placeholder barnstar ribbon.png",
 59 	"Project Trains no image.png",
 60 	"Image-request.png",
 61 	"Sin bandera.svg",
 62 	"Sin escudo.svg",
 63 	"Replace this image - temple.png",
 64 	"Replace this image butterfly.png",
 65 	"Replace this image.svg",
 66 	"Replace this image1.svg",
 67 	"Resolution angle.png",
 68 	"Image-No portrait-text-BFD-test.svg",
 69 	"Insert image here.svg",
 70 	"No image available.png",
 71 	"NO IMAGE YET square.png",
 72 	"NO IMAGE YET.png",
 73 	"No Photo Available.svg",
 74 	"No Screenshot.svg",
 75 	"No-image-available.jpg",
 76 	"Null.png",
 77 	"PictureNeeded.gif",
 78 	"Place holder.jpg",
 79 	"Unbenannt.JPG",
 80 	"UploadACopyrightFreeImage.svg",
 81 	"UploadAnImage.gif",
 82 	"UploadAnImage.svg",
 83 	"UploadAnImageShort.svg",
 84 	"CarersLogo.gif",
 85 	"Diagram Needed.svg",
 86 	"No male portrait.svg",
 87 	"NoDVDcover copy.png",
 88 	"Placeholder barnstar ribbon.png",
 89 	"Project Trains no image.png",
 90 	"Image-request.png",
 91 	"Noimage.gif",
 92 }
 93 
 94 function i.IsPlaceholder(image)
 95     -- change underscores to spaces
 96     image = mw.ustring.gsub(image, "_", " ");
 97     assert(image ~= nil, 'mw.ustring.gsub(image, "_", " ") must not return nil')
 98     -- if image starts with [[ then remove that and anything after |
 99     if mw.ustring.sub(image,1,2) == "[[" then
100         image = mw.ustring.sub(image,3);
101         image = mw.ustring.gsub(image, "([^|]*)|.*", "%1");
102         assert(image ~= nil, 'mw.ustring.gsub(image, "([^|]*)|.*", "%1") must not return nil')
103     end
104     -- Trim spaces
105     image = mw.ustring.gsub(image, '^[ ]*(.-)[ ]*$', '%1');
106     assert(image ~= nil, "mw.ustring.gsub(image, '^[ ]*(.-)[ ]*$', '%1') must not return nil")
107     -- remove prefix if exists
108     local allNames = mw.site.namespaces[6].aliases
109     allNames[#allNames + 1] = mw.site.namespaces[6].name
110     allNames[#allNames + 1] = mw.site.namespaces[6].canonicalName
111     for i, name in ipairs(allNames) do
112         if mw.ustring.lower(mw.ustring.sub(image, 1, mw.ustring.len(name) + 1)) == mw.ustring.lower(name .. ":") then
113             image = mw.ustring.sub(image, mw.ustring.len(name) + 2);
114             break
115         end
116     end
117     -- Trim spaces
118     image = mw.ustring.gsub(image, '^[ ]*(.-)[ ]*$', '%1');
119     -- capitalise first letter
120     image = mw.ustring.upper(mw.ustring.sub(image,1,1)) .. mw.ustring.sub(image,2);
121 
122     for i,j in pairs(placeholder_image) do
123         if image == j then
124             return true
125         end
126     end
127     return false
128 end
129 
130 function i.InfoboxImage(frame)
131     local image = frame.args["image"];
132     
133     if image == "" or image == nil then
134         return "";
135     end
136     if image == " " then
137         return image;
138     end
139     if frame.args["suppressplaceholder"] ~= "no" then
140         if i.IsPlaceholder(image) == true then
141             return "";
142         end
143     end
144 
145     if mw.ustring.lower(mw.ustring.sub(image,1,5)) == "http:" then
146         return "";
147     end
148     if mw.ustring.lower(mw.ustring.sub(image,1,6)) == "[http:" then
149         return "";
150     end
151     if mw.ustring.lower(mw.ustring.sub(image,1,7)) == "[[http:" then
152         return "";
153     end
154     if mw.ustring.lower(mw.ustring.sub(image,1,6)) == "https:" then
155         return "";
156     end
157     if mw.ustring.lower(mw.ustring.sub(image,1,7)) == "[https:" then
158         return "";
159     end
160     if mw.ustring.lower(mw.ustring.sub(image,1,8)) == "[[https:" then
161         return "";
162     end
163 
164     if mw.ustring.sub(image,1,2) == "[[" then
165         -- search for thumbnail images and add to tracking cat if found
166         local cat = "";
167         if mw.title.getCurrentTitle().namespace == 0 and (mw.ustring.find(image, "|%s*thumb%s*[|%]]") or mw.ustring.find(image, "|%s*thumbnail%s*[|%]]")) then
168             cat = "[[Category:Pages using infoboxes with thumbnail images]]";
169         end
170         return image .. cat;
171     elseif mw.ustring.sub(image,1,2) == "{{" and mw.ustring.sub(image,1,3) ~= "{{{" then
172         return image;
173     elseif mw.ustring.sub(image,1,1) == "<" then
174         return image;
175     elseif mw.ustring.sub(image,1,5) == mw.ustring.char(127).."UNIQ" then
176         -- Found strip marker at begining, so pass don't process at all
177         return image;
178     elseif mw.ustring.sub(image,4,9) == "`UNIQ-" then
179         -- Found strip marker at begining, so pass don't process at all
180         return image;
181     else
182         local result = "";
183         local page = frame.args["page"];
184         local size = frame.args["size"];
185         local maxsize = frame.args["maxsize"];
186         local sizedefault = frame.args["sizedefault"];
187         local alt = frame.args["alt"];
188         local link = frame.args["link"];
189         local title = frame.args["title"];
190         local border = frame.args["border"];
191         local upright = frame.args["upright"] or "";
192         local thumbtime = frame.args["thumbtime"] or "";
193         local center = frame.args["center"];
194         local class = frame.args["class"];
195         
196         -- remove prefix if exists
197         local allNames = mw.site.namespaces[6].aliases
198         allNames[#allNames + 1] = mw.site.namespaces[6].name
199         allNames[#allNames + 1] = mw.site.namespaces[6].canonicalName
200         for i, name in ipairs(allNames) do
201             if mw.ustring.lower(mw.ustring.sub(image, 1, mw.ustring.len(name) + 1)) == mw.ustring.lower(name .. ":") then
202                 image = mw.ustring.sub(image, mw.ustring.len(name) + 2);
203                 break
204             end
205         end
206         
207         if maxsize ~= "" and maxsize ~= nil then
208             -- if no sizedefault then set to maxsize
209             if sizedefault == "" or sizedefault == nil then
210                 sizedefault = maxsize
211             end
212             -- check to see if size bigger than maxsize
213             if size ~= "" and size ~= nil then
214                 local sizenumber = tonumber(mw.ustring.match(size,"%d*")) or 0;
215                 local maxsizenumber = tonumber(mw.ustring.match(maxsize,"%d*")) or 0;
216                 if sizenumber>maxsizenumber and maxsizenumber>0 then
217                     size = maxsize;
218                 end
219             end
220         end
221         -- add px to size if just a number
222         if (tonumber(size) or 0) > 0 then
223             size = size .. "px";
224         end
225         -- add px to sizedefault if just a number
226         if (tonumber(sizedefault) or 0) > 0 then
227             sizedefault = sizedefault .. "px";
228         end
229         
230         result = "[[File:" .. image;
231         if page ~= "" and page ~= nil then
232             result = result .. "|page=" .. page;
233         end
234         if size ~= "" and size ~= nil then
235             result = result .. "|" .. size;
236         elseif sizedefault ~= "" and sizedefault ~= nil then
237             result = result .. "|" .. sizedefault;
238         else
239             result = result .. "|frameless";
240         end
241         if center == "yes" then
242             result = result .. "|center"
243         end
244         if alt ~= "" and alt ~= nil then
245             result = result .. "|alt=" .. alt;
246         end
247         if link ~= "" and link ~= nil then
248             result = result .. "|link=" .. link;
249         end
250         if border == "yes" then
251             result = result .. "|border";
252         end
253         if upright == "yes" then
254             result = result .. "|upright";
255         elseif upright ~= "" then
256             result = result .. "|upright=" .. upright;
257         end
258         if thumbtime ~= "" then
259             result = result .. "|thumbtime=" .. thumbtime;
260         end
261         if class ~= nil and class ~= "" then
262             result = result .. "|class=" .. class;
263         end
264         -- if alt value is a keyword then do not use as a description
265         if alt == "thumbnail" or alt == "thumb" or alt == "frameless" or alt == "left" or alt == "center" or alt == "right" or alt == "upright" or alt == "border" or mw.ustring.match(alt or "", '^[0-9]*px$', 1) ~= nil then
266             alt = nil;
267         end
268         if title ~= "" and title ~= nil then
269             -- does title param contain any templatestyles? If yes then set to blank.
270             if mw.ustring.match(frame:preprocess(title), 'UNIQ%-%-templatestyles', 1) ~= nil then
271                 title = nil;
272             end
273         end
274         if title ~= "" and title ~= nil then
275             result = result .. "|" .. title;
276         end
277         result = result .. "]]";
278         
279         return result;
280     end
281 end
282 
283 return i;