Module:Excerpt: Difference between revisions

Content deleted Content added
Remove footnotes {{efn|not valid in Tennessee}} etc.
Add paragraphs= argument; improve layout
Line 3:
-- 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>%c%s*==.-</noinclude>*","") -- remove noincludefirst heading and everything after bitsit
text = mw.ustring.gsub(text, "<%s*ref.-noinclude>.-<%s*/%s*ref%s*noinclude>", "") -- remove refsnoinclude bits
repeat
local oldtext=text
text = mw.ustring.gsub(text,"^%A-%b{}%s*","") -- remove infoboxes, hatnotes, tags, etc. from the front
until text == oldtext
 
text=mw.ustring.gsub(text,"{{%s*[Ee]fn%s*|.-}}","") -- remove {{efn|footnote}}
if #paragraphlist > 0 then -- limit to requested paragraphs e.g. {1, 3, 4, 5}
text=mw.ustring.gsub(text,"{{%s*[Ee]fn-la%s*|.-}}","") -- {{efn-la}} alias for {{efn}}
text local paras = mw.ustringtext.gsubsplit(text, "{{\n%s*[Ee]l[mn]%s*|.-}}","\n") -- {{elm}}%s* may include \n if andthree {{eln}}or aliasmore forappear {{efn}}together
local sep="" -- no separator before first paragraph
text=mw.ustring.gsub(text,"<%s*ref[^>]-/%s*>","") -- remove refs cited elsewhere
local newtext = ""
text=mw.ustring.gsub(text,"<%s*ref.->.-<%s*/%s*ref%s*>","") -- remove refs
for _, p in pairs(paragraphlist) do
text=mw.ustring.match(text,"%C*'''.*") or text -- start at the first line with bold text, if any
newtext = newtext .. sep .. paras[p]
sep = "\n\n"
end
text = newtext
end
 
text = mw.ustring.gsub(text, "{{%cs*[Ee]fn%s*==|.*-}}", "") -- remove first heading and everything after it{{efn|footnote}}
text = mw.ustring.gsub(text, "{{%s*[Ee]fn-la%s*|.-}}", "") -- remove{{efn-la}} alias for {{efn|footnote}}
text = mw.ustring.gsub(text, "{{%s*[Ee]fn-lal[mn]%s*|.-}}", "") -- {{efn-laelm}} 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
Line 25 ⟶ 39:
-- 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
 
return frame:preprocess(p._lead(args[1] or pargs[1]))
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