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(title) 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
newtext = newtext .. sep .. paras[p]
sep = "\n\n"
end
text = newtext
end
text = mw.ustring.gsub(text, "{{%s*[Ee]fn%s*|.-}}", "") -- remove {{efn|footnote}}
text = mw.ustring.gsub(text, "{{%s*[Ee]fn-la%s*|.-}}", "") -- {{efn-la}} alias for {{efn}}
text = mw.ustring.gsub(text, "{{%s*[Ee]l[mn]%s*|.-}}", "") -- {{elm}} and {{eln}} alias for {{efn}}
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 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(args[1] or pargs[1], paralist))
end
return p