モジュール:InfoboxImage
このモジュールについての説明文ページを モジュール: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 "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.svg",
41 "Replace this image male (blue).svg",
42 "Replace this image male.svg",
43 "Silver - Replace this image female.svg",
44 "Silver - Replace this image male.svg",
45 "Replace this image.svg",
46 "Cricket no pic.png",
47 "CarersLogo.gif",
48 "Diagram Needed.svg",
49 "Example.jpg",
50 "Image placeholder.png",
51 "No male portrait.svg",
52 "Nocover-upload.png",
53 "NoDVDcover copy.png",
54 "Noribbon.svg",
55 "No portrait-BFD-test.svg",
56 "Placeholder barnstar ribbon.png",
57 "Project Trains no image.png",
58 "Image-request.png",
59 "Sin bandera.svg",
60 "Sin escudo.svg",
61 "Replace this image - temple.png",
62 "Replace this image butterfly.png",
63 "Replace this image.svg",
64 "Replace this image1.svg",
65 "Resolution angle.png",
66 "Image-No portrait-text-BFD-test.svg",
67 "Insert image here.svg",
68 "No image available.png",
69 "NO IMAGE YET square.png",
70 "NO IMAGE YET.png",
71 "No Photo Available.svg",
72 "No Screenshot.svg",
73 "No-image-available.jpg",
74 "Null.png",
75 "PictureNeeded.gif",
76 "Place holder.jpg",
77 "Unbenannt.JPG",
78 "UploadACopyrightFreeImage.svg",
79 "UploadAnImage.gif",
80 "UploadAnImage.svg",
81 "UploadAnImageShort.svg",
82 "CarersLogo.gif",
83 "Diagram Needed.svg",
84 "No male portrait.svg",
85 "NoDVDcover copy.png",
86 "Placeholder barnstar ribbon.png",
87 "Project Trains no image.png",
88 "Image-request.png",
89 "Noimage.gif",
90 }
91
92 local categories = {
93 url_image_links = "[[Category:Pages using infoboxes with URL in image parameter]]",
94 thumbnail_images = "[[Category:Pages using infoboxes with thumbnail images]]",
95 }
96
97 local function trackable()
98 local ns = mw.title.getCurrentTitle().nsText:lower()
99 return not (ns == 'user' or ns == 'user talk')
100 end
101
102 function i.IsPlaceholder(image)
103 -- change underscores to spaces
104 image = mw.ustring.gsub(image, "_", " ");
105 assert(image ~= nil, 'mw.ustring.gsub(image, "_", " ") must not return nil')
106 -- if image starts with [[ then remove that and anything after |
107 if mw.ustring.sub(image,1,2) == "[[" then
108 image = mw.ustring.sub(image,3);
109 image = mw.ustring.gsub(image, "([^|]*)|.*", "%1");
110 assert(image ~= nil, 'mw.ustring.gsub(image, "([^|]*)|.*", "%1") must not return nil')
111 end
112 -- Trim spaces
113 image = mw.ustring.gsub(image, '^[ ]*(.-)[ ]*$', '%1');
114 assert(image ~= nil, "mw.ustring.gsub(image, '^[ ]*(.-)[ ]*$', '%1') must not return nil")
115 -- remove prefix if exists
116 local allNames = mw.site.namespaces[6].aliases
117 allNames[#allNames + 1] = mw.site.namespaces[6].name
118 allNames[#allNames + 1] = mw.site.namespaces[6].canonicalName
119 for i, name in ipairs(allNames) do
120 if mw.ustring.lower(mw.ustring.sub(image, 1, mw.ustring.len(name) + 1)) == mw.ustring.lower(name .. ":") then
121 image = mw.ustring.sub(image, mw.ustring.len(name) + 2);
122 break
123 end
124 end
125 -- Trim spaces
126 image = mw.ustring.gsub(image, '^[ ]*(.-)[ ]*$', '%1');
127 -- capitalise first letter
128 image = mw.ustring.upper(mw.ustring.sub(image,1,1)) .. mw.ustring.sub(image,2);
129
130 for i,j in pairs(placeholder_image) do
131 if image == j then
132 return true
133 end
134 end
135 return false
136 end
137
138 function i.InfoboxImage(frame)
139 local image = frame.args["image"];
140
141 if image == "" or image == nil then
142 return "";
143 end
144 if image == " " then
145 return image;
146 end
147 if frame.args["suppressplaceholder"] ~= "no" then
148 if i.IsPlaceholder(image) == true then
149 return "";
150 end
151 end
152
153 if string.find(image, "^%[*https?:") then
154 -- Error category.
155 return trackable() and categories.url_image_links or ""
156 end
157
158 if mw.ustring.sub(image,1,2) == "[[" then
159 -- search for thumbnail images and add to tracking cat if found
160 local cat = "";
161 if mw.title.getCurrentTitle().namespace == 0 and (mw.ustring.find(image, "|%s*thumb%s*[|%]]") or mw.ustring.find(image, "|%s*thumbnail%s*[|%]]")) then
162 cat = trackable() and categories.thumbnail_images or ""
163 end
164 return image .. cat;
165 elseif mw.ustring.sub(image,1,2) == "{{" and mw.ustring.sub(image,1,3) ~= "{{{" then
166 return image;
167 elseif mw.ustring.sub(image,1,1) == "<" then
168 return image;
169 elseif mw.ustring.sub(image,1,8) == mw.ustring.char(127).."'\"`UNIQ" then
170 -- Found strip marker at begining, so pass don't process at all
171 return image;
172 else
173 local result = "";
174 local page = frame.args["page"];
175 local size = frame.args["size"];
176 local maxsize = frame.args["maxsize"];
177 local sizedefault = frame.args["sizedefault"];
178 local alt = frame.args["alt"];
179 local link = frame.args["link"];
180 local title = frame.args["title"];
181 local border = frame.args["border"];
182 local upright = frame.args["upright"] or "";
183 local thumbtime = frame.args["thumbtime"] or "";
184 local center = frame.args["center"];
185 local class = frame.args["class"];
186
187 -- remove prefix if exists
188 local allNames = mw.site.namespaces[6].aliases
189 allNames[#allNames + 1] = mw.site.namespaces[6].name
190 allNames[#allNames + 1] = mw.site.namespaces[6].canonicalName
191 for i, name in ipairs(allNames) do
192 if mw.ustring.lower(mw.ustring.sub(image, 1, mw.ustring.len(name) + 1)) == mw.ustring.lower(name .. ":") then
193 image = mw.ustring.sub(image, mw.ustring.len(name) + 2);
194 break
195 end
196 end
197
198 if maxsize ~= "" and maxsize ~= nil then
199 -- if no sizedefault then set to maxsize
200 if sizedefault == "" or sizedefault == nil then
201 sizedefault = maxsize
202 end
203 -- check to see if size bigger than maxsize
204 if size ~= "" and size ~= nil then
205 local sizenumber = tonumber(mw.ustring.match(size,"%d*")) or 0;
206 local maxsizenumber = tonumber(mw.ustring.match(maxsize,"%d*")) or 0;
207 if sizenumber>maxsizenumber and maxsizenumber>0 then
208 size = maxsize;
209 end
210 end
211 end
212 -- add px to size if just a number
213 if (tonumber(size) or 0) > 0 then
214 size = size .. "px";
215 end
216 -- add px to sizedefault if just a number
217 if (tonumber(sizedefault) or 0) > 0 then
218 sizedefault = sizedefault .. "px";
219 end
220
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 class ~= nil and class ~= "" then
253 result = result .. "|class=" .. class;
254 end
255 -- if alt value is a keyword then do not use as a description
256 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
257 alt = nil;
258 end
259 if title ~= "" and title ~= nil then
260 -- does title param contain any templatestyles? If yes then set to blank.
261 if mw.ustring.match(frame:preprocess(title), 'UNIQ%-%-templatestyles', 1) ~= nil then
262 title = nil;
263 end
264 end
265 if title ~= "" and title ~= nil then
266 result = result .. "|" .. title;
267 end
268 result = result .. "]]";
269
270 return result;
271 end
272 end
273
274 return i;