Module:Excerpt: Difference between revisions

Content deleted Content added
Parse "File:" within templates within preamble (testcase: Man Utd). Strip citation/disambiguation needed templates. Declare functions as local.
Parse image= etc. within infobox. Remove HTML comments earlier in case they lurk within infoboxes.
Line 1:
local p = {}
local mRedirect = require('Module:Redirect')
 
local errors
-- Return blank text, or an error message if requested
local function err(text)
if errors then error(text, 2) end
return ""
end
 
-- Attempt to parse [[File:...]] or [[Image:...]], either anywhere or at the start
Line 15 ⟶ 21:
end
 
-- Attempt to construct a [[File:...]] block from {{infobox ... |image= ...}}
-- Return blank text, or an error message if requested
local function errargimage(text, options)
local token = nil
if errors then error(text, 2) end
or if mw.ustring.match(text, "^%b{}%{%s*[Ii]nfobox") or false -- or {{Template}}then
return ""
local image = mw.ustring.match(text, "|%s*image%s*=%s*([^%}|]*)") -- parse image= argument...
or mw.ustring.match(text, "|%s*Cover%s*=%s*([^%}|]-)") -- or Cover= from Infobox album
if image then -- add in relevant parameters: caption, alt text and image size
token = "[[File:" .. image
local caption = mw.ustring.match(text, "|%s*[Cc]aption%s*=%s*([^%}|]*)")
if caption then token = token .. "|caption=" .. 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","") .. "|frame]]\n"
end
end
 
return token
end
 
Line 57 ⟶ 78:
if not text then return err("Cannot read a valid page: first name is " .. firstpage) end
 
text = mw.ustring.gsub(text, "<!%-%-.-%-%->","") -- remove HTML comments
text = mw.ustring.gsub(text, "%c%s*==.*","") -- remove first heading and everything after it
text = mw.ustring.gsub(text, "<noinclude>.-</noinclude>", "") -- remove noinclude bits
Line 82 ⟶ 104:
 
text = mw.ustring.gsub(text,"^%s*","") -- remove initial white space
repeat -- loop around parsing a comment, template, image or paragraph
local token = mw.ustring.match(text, "^%s*<!%-%-.-%-%->b{}%s*") --or false <!--HTML comment-->{{Template}}
or mw.ustring.match(text, "^%b{}%s*") or false -- or {{Template}}
if token then
if inlead then -- keep comments and templates only within text body
t = t .. token
else -- look for [[File:... embedded in an infobox etc. in the preamble
local image = parseimage(token, false) or argimage(token)
if image then -- keep comments and templates only within text body
image = mw.ustring.gsub(image, "|%s*frameless", "|frame") -- excerpt needs a frame to flow around, even if infobox doesn't
files = files + 1
if options.fileflags and options.fileflags[files] then t = t .. image .. "\n" end
end
end