Jump to content

Module:Excerpt

Permanently protected module
From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Certes (talk | contribs) at 16:45, 22 April 2018 (Module to extract the lead from an article. Unlike #lsth, it removes infoboxes, hatnotes, etc.). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.
(diff) ← Previous revision | Latest revision (diff) | Newer revision → (diff)

local p = {}

-- Entry point for Lua callers
-- Returns a string value: text of the lead of a page
function p._lead(pagename)
	-- Find the lead section of the named page
	-- %b{} removes any leading 
	-- If the page exists, protected or not, this is some other value
	-- Note: this check does NOT record a wikilink or transclusion from the calling page to pagename
	title=mw.title.new(pagename)
	text=title.getContent(title)
	text=mw.ustring.gsub(text,"%c%s*==.*","") -- remove first heading and everything after it
	text=mw.ustring.gsub(text,"<noinclude>.-</noinclude>","") -- remove noinclude bits
	text=mw.ustring.gsub(text,"^%A*%b{}%s*","") -- remove infoboxes, hatnotes, tags, etc. from the front
	text=mw.ustring.gsub(text,"<ref.->.-</ref>","") -- 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 }
	local args = frame.args -- from calling module
	local pargs = frame:getParent().args -- from template
	return frame:preprocess(p._lead(args[1] or pargs[1]));
end

return p