local p = {}
-- Entry point for Lua callers
-- Returns a string value: text of the lead of a page
function p._lead(pagename, paragraphlist)
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
if not title then return "" end
local text = title:getContent() or ""
text = mw.ustring.gsub(text, "%c%s*==.*","") -- remove first heading and everything after it
text = mw.ustring.gsub(text, "<noinclude>.-</noinclude>", "") -- remove noinclude bits
repeat
local oldtext=text
text = mw.ustring.gsub(text,"^%A-%b{}%s*","") -- remove infoboxes, hatnotes, tags, etc. from the front
until text == oldtext
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
local sep="" -- no separator before first paragraph
local newtext = ""
for _, p in pairs(paragraphlist) do
if paras[p] then newtext = newtext .. sep .. paras[p] end -- else p exceeds number of paragraphs found
sep = "\n\n"
end
text = newtext
end
for _, t in pairs {"[Ee]fn", "[Ee]fn-la", "[Ee]l[mn]", "[Rr]", "[Ss]fn[bp]", "[Ss]fb"} do -- remove refs and footnotes
text = mw.ustring.gsub(text, "{{%s*" .. t .. "%s*|.-}}", "")
end
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.match(text, "%C*'''.*") or text -- start at the first line with bold text, if any
return text
end
-- Entry point for template callers using #invoke:
function p.lead(frame)
-- args = { 1 = page name, paragraphs = list e.g. "1,3-5" }
local args = frame.args -- from calling module
local pargs = frame:getParent().args -- from template
local pagename = args[1] or pargs[1] or ""
pagename = mw.ustring.match(pagename, "%[%[%s*(.-)[]|#]") or pagename -- "[[Foo|Bar]]" → "Foo"
local parasets = mw.text.split(args["paragraphs"] or pargs["paragraphs"] or "", ",") -- parse paragraphs, e.g. "1,3-5" → {"1","3-5"}
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, paralist))
end
return p