Module:Excerpt: Difference between revisions

Content deleted Content added
Remove files and images. Remove initial white space rather than seeking bold text.
Add support for files= to show selected images from the source
Line 3:
-- Entry point for Lua callers
-- Returns a string value: text of the lead of a page
function p._lead(pagename, paragraphlistoptions)
if not pagename then return "" end -- Return blank text rather than an error splurge
local title = mw.title.new(pagename) -- Find the lead section of the named page
Line 16:
until text == oldtext
 
local paragraphlist = options.paragraphs or {}
if #paragraphlist > 0 then -- limit to requested paragraphs e.g. {1, 3, 4, 5}
local paras = mw.text.split(text, "\n%s*\n") -- %s* may include \n if three or more appear together
Line 32 ⟶ 33:
text = mw.ustring.gsub(text, "<%s*ref[^>]-/%s*>", "") -- remove refs cited elsewhere
text = mw.ustring.gsub(text, "<%s*ref.->.-<%s*/%s*ref%s*>", "") -- remove refs
text = mw.ustring.gsub(text, "%[%[%s*[Ff]ile%s*:%C*%]%]", "") -- remove files
text = mw.ustring.gsub(text, "%[%[%s*[Ii]mage%s*:%C*%]%]", "") -- remove images
 
local filelist = options.files or {}
text = mw.ustring.gsub(text, "^%s*", "") -- remove initial white space
local keepfile = {} -- keepfile[n] is true if we want to keep the nth image
for _, psv in pairs(parasetsfilelist) do
keepfile[v] = true
end
 
text = mw.ustring.gsub(text, "%[%[%s*[Ii]mage%s*:%C*%]%]", "[[File:") -- removenow imageswe can ignore Image:
local n = 1 -- image count
local paralisttext2 = {}""
for t, f in mw.ustring.gmatch(text.."\n[[File:#DUMMY#]]", "(.-)(%[%[%s*[Ff]ile%s*:%C*%]%])") do -- split around files
text2 = text2 .. t -- always keep the non-file text
if keepfile[n] then text2 = text2 .. f end -- only keep file text if we want this image
n = n + 1
end
text = mw.ustring.gsub(texttext2, "^%s*", "") -- remove initial white space
text = mw.ustring.gsub(text, "\n%[%[%s*[Ff]ile%s*File:%C*#DUMMY#%]%]$", "") -- remove filesdummy image
return text
end
 
-- Convert a comma-separated list of numbers or min-max ranges into a list of numbers, e.g. "1,3-5" → {1,3,4,5}
function p.numberlist(str)
local ranges = mw.text.split(str, ",") -- parse ranges, e.g. "1,3-5" → {"1","3-5"}
local nlist = {}
for _, r in pairs(ranges) do
local min, max = mw.ustring.match(psr, "^%s*(%d+)%s*%-%s*(%d+)%s*$") -- "3-5" → min=3 max=5
if not max then min, max = mw.ustring.match(psr, "^%s*((%d+))%s*$") end -- "1" → min=1 max=1
if max then
for p = min, max do table.insert(paralistnlist, p) end
end
end
return nlist
end
 
Line 48 ⟶ 76:
pagename = mw.ustring.match(pagename, "%[%[%s*(.-)[]|#]") or pagename -- "[[Foo|Bar]]" → "Foo"
 
local parasetsparalist = mwp.text.splitnumberlist(args["paragraphs"] or pargs["paragraphs"] or "", ",") -- parse paragraphs, e.g. "1,3-5" → {"1","3-5"}
local filelist = p.numberlist(args["files"] or pargs["files"] or "", ",") -- parse file numbers
local paralist = {}
for _, ps in pairs(parasets) do
local min, max = mw.ustring.match(ps,"^%s*(%d+)%s*%-%s*(%d+)%s*$") -- "3-5" → min=3 max=5
if not max then min, max = mw.ustring.match(ps,"^%s*((%d+))%s*$") end -- "1" → min=1 max=1
if max then
for p = min, max do table.insert(paralist, p) end
end
end
 
return frame:preprocess(p._lead(pagename, {paragraphs = paralist, files = filelist}))
end