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