Module:Excerpt: Difference between revisions

Content deleted Content added
Bug fix: display title correctly where only part of the bold text is in italics, e.g. '''Foo ''Bar''''' or '''''Foo'' Bar'''
Prefer image= to [[File:... when extracting an image from an infobox with multiple images. Accept non-standard argument names such as Ship image= and Ship caption=. Refactor to use matchany().
Line 7:
if errors then error(text, 2) end
return ""
end
 
-- In text, match pre..list[1]..post or pre..list[2]..post or ...
local function matchany(text, pre, list, post)
local match
for i = 1, #list do
local altmatch = mw.ustring.match(text, "|%s*alt%s*=%s*(pre .. list[^}|i]*)" .. post)
if match then return match end
end
return nil
end
 
-- Check image for suitability
local function checkimage(image)
local page = mw.ustring.matchmatchany(image, "(", {"[Ff]ile", "[Ii]mage"}, "%s*:[^|%]]*)") -- match File:(name) ...or Image:(name)
or mw.ustring.match(image, "([Ii]mage%s*:[^|%]]*)") -- or Image:(name) ...
if not page then return false end
 
-- Limit to image types: .gif, .jpg, .jpeg, .png, .svg, .tiff, .xcf (exclude .ogg audio etc.)
if not mw.ustring.matchmatchany(page, "%.", {"[Gg][Ii][Ff]", "[Jj][Pp][Ee]?[Gg]", "[Pp][Nn][Gg]", "[Ss][Vv][Gg]", "[Tt][Ii][Ff][Ff]", "[Xx][Cc][Ff]"}, "%s*$") then
return false
and not mw.ustring.match(page, "%.[Jj][Pp][Ee]?[Gg]%s*$")
end
and not mw.ustring.match(page, "%.[Pp][Nn][Gg]%s*$")
and not mw.ustring.match(page, "%.[Ss][Vv][Gg]%s*$")
and not mw.ustring.match(page, "%.[Tt][Ii][Ff][Ff]%s*$")
and not mw.ustring.match(page, "%.[Xx][Cc][Ff]%s*$") then return false end
 
local title = mw.title.new(":" .. page) -- Read description page (for :File:Foo rather than File:Foo)
Line 38 ⟶ 44:
local startre = ""
if start then startre = "^" end -- a true flag restricts search to start of string
local image = mw.ustring.matchmatchany(text, startre .. "%[%[%s*", {"[Ff]ile", "[Ii]mage"}, "%s*:.*") -- [[File: or [[Image: ...
or mw.ustring.match(text, startre .. "%[%[%s*[Ii]mage%s*:.*") -- or [[Image: ...
if image then
image = mw.ustring.match(image, "%b[]%s*") -- matching [[...]] to handle wikilinks nested in caption
Line 50 ⟶ 55:
local token = nil
if mw.ustring.match(text, "{{%s*[Ii]nfobox") then
local image = mw.ustring.matchmatchany(text, "|%s*", {"image", "PD_image", "image_flag", "Ship image", "Cover"}, "%s*=%s*([^}|].*)") -- parse image= argument...
 
or mw.ustring.match(text, "|%s*PD_image%s*=%s*([^}|]-)") -- or its known alternatives such as...
or mw.ustring.match(text, "|%s*image_flag%s*=%s*([^}|]-)") -- image_flag= from Infobox country
or mw.ustring.match(text, "|%s*Cover%s*=%s*([^}|]-)") -- or Cover= from Infobox album
if image then -- add in relevant optional parameters: caption, alt text and image size
token = "[["parseimage(image, true) -- Addlook File: unless name already beginsfor image=[[File:...]] or Image:etc.
if not (mw.ustring.match(image,token "^[Ff]ile%s*:")then
image or= mw.ustring.match(image, "^[Ii^}|]mage%s*:")) then-- remove later arguments
token = token"[[" ..-- Add "File:" unless name already begins File: or Image:
if not matchany(image, "^", {"[Ff]ile", "[Ii]mage"}, "%s*:") then
if alt then token = token .. "|alt=File:" .. alt end
end
token = token .. image
local caption = mw.ustring.matchmatchany(text, "|%s*", {"[Cc]aption", "Ship caption"}, "%s*=%s*([^}|]*)")
if caption then token = token .. "|" .. caption end
local andalt not= mw.ustring.match(pagetext, "|%.[Pp][Nn][Gg]s*alt%s*$=%s*([^}|]*)")
if image_sizealt then token = token .. "|alt=" .. image_sizealt end
local orimage_size = mw.ustring.match(text, "|%s*PD_imageimage_size%s*=%s*([^}|]-*)") -- or its known alternatives such as...
if image_size then token = token .. "|" .. image_size end
token = mw.ustring.gsub(token, "\n","") .. "]]\n"
end
token = mw.ustring.gsub(token, "\n","") .. image"\n"
local caption = mw.ustring.match(text, "|%s*[Cc]aption%s*=%s*([^}|]*)")
if caption then token = token .. "|" .. caption end
local alt = mw.ustring.match(text, "|%s*alt%s*=%s*([^}|]*)")
if alt then token = token .. "|alt=" .. alt end
local image_size = mw.ustring.match(text, "|%s*image_size%s*=%s*([^}|]*)")
if image_size then token = token .. "|" .. image_size end
token = mw.ustring.gsub(token, "\n","") .. "]]\n"
end
end
Line 157 ⟶ 164:
t = t .. token
elseif files < maxfile then -- discard template, but if we are still collecting images...
local image = parseimageargimage(token, false) or argimageparseimage(token, false) -- look for embedded [[File:...]], |image=, etc.
if image and checkimage(image) then -- if image is found and qualifies (not a sound file, non-free, etc.)
files = files + 1 -- count the file, whether displaying it or not
Line 163 ⟶ 170:
image = mw.ustring.gsub(image, "|%s*frameless%s*%f[|%]]", "") -- make image a thumbnail, not frameless etc.
image = mw.ustring.gsub(image, "|%s*framed?%s*%f[|%]]", "")
if not mw.ustring.matchmatchany(image, "|%s*", {"thumb", "thumbnail"}, "%s*%f[|%]]") then
and not mw.ustring.match(image, "|%s*thumbnail%s*%f[|%]]") then
image = mw.ustring.gsub(image, "(%]%]%s*)$", "|thumb%1")
end