Module:Excerpt: Difference between revisions

Content deleted Content added
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().
Parse caption found in infobox better, to skip pipes in nested wikilinks
Line 50:
return image
end
 
-- Parse a caption, which ends at a | (end of parameter) or } (end of infobox) but may contain nested [..] and {..}
local function parsecaption(caption)
if not caption then return nil end
local len = mw.ustring.len(caption)
local pos = 1
while pos <= len do
local linkstart, linkend = mw.ustring.find(caption, "%b[]", pos)
linkstart = linkstart or len + 1 -- avoid comparison with nil when no link
local templatestart, templateend = mw.ustring.find(caption, "%b{}", pos)
templatestart = templatestart or len + 1 -- avoid comparison with nil when no template
local argend = mw.ustring.find(caption, "[|}]", pos) or len + 1
if linkstart < templatestart and linkstart < argend then
pos = linkend + 1 -- skip wikilink
elseif templatestart < argend then
pos = templateend + 1 -- skip template
else -- argument ends before the next wikilink or template
return mw.ustring.sub(caption, 1, argend - 1)
end
end
end
 
-- Attempt to construct a [[File:...]] block from {{infobox ... |image= ...}}
Line 66 ⟶ 87:
end
token = token .. image
local caption = parsecaption(matchany(text, "|%s*", {"[Cc]aption", "Ship caption"}, "%s*=%s*([^}|].*)"))
if caption and mw.ustring.match(caption, "%S") 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 and mw.ustring.match(image_size, "%S") then token = token .. "|" .. image_size end
token = token .. "]]"
end
Line 166 ⟶ 187:
local image = argimage(token) or parseimage(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
if options.fileflags and options.fileflags[files] then -- if displaying this image