Module:Excerpt/sandbox: Difference between revisions

Content deleted Content added
Move all localization data to Module:Excerpt/l10n and require it here
Add option to excerpt a fragment wrapped with <section> tags. Also change cleanupText so that it takes a single table of parameters
Line 12:
 
-- Helper function to test for falsy values
local function falsy( value )
if not value or value == "" or value == "0" or value == "false" or value == "no" then
return true
Line 484:
end
 
local function cleanupText(text, keepSubsections, keepRefsoptions)
text = mw.ustring.gsub(text, "<!%-%-.-%-%->","") -- remove HTML comments
if falsy(keepSubsections) then
text = mw.ustring.gsub(text, "\n==.*","") -- remove first ==Heading== and everything after it
text = mw.ustring.gsub(text, "^==.*","") -- ...even if the lead is empty
end
text = mw.ustring.gsub(text, "<[Nn][Oo][Ii][Nn][Cc][Ll][Uu][Dd][Ee]>.-</[Nn][Oo][Ii][Nn][Cc][Ll][Uu][Dd][Ee]>", "") -- remove noinclude bits
if mw.ustring.find(text, "[Oo][Nn][Ll][Yy][Ii][Nn][Cc][Ll][Uu][Dd][Ee]") then -- avoid expensive search if possible
Line 496 ⟶ 492:
text = mw.ustring.gsub(text, "</[Oo][Nn][Ll][Yy][Ii][Nn][Cc][Ll][Uu][Dd][Ee]>.*", "") -- remove text after last onlyinclude section
end
if not falsy(keepRefsoptions.fragment) then
local fragment = mw.ustring.match(text, "<%s*[Ss]ection%s+begin%s*=%s*[\"\']?%s*" .. options.fragment .. "%s*[\"\']?%s*/>(.-)<%s*[Ss]ection%s+end=%s*[\"\']?%s*" .. options.fragment .. "%s*[\"\']?%s*/>")
if fragment then text = fragment else text = "" end
end
if falsy(options.keepSubsections) then
text = mw.ustring.gsub(text, "\n==.*","") -- remove first ==Heading== and everything after it
text = mw.ustring.gsub(text, "^==.*","") -- ...even if the lead is empty
end
if falsy(options.keepRefs) then
text = mw.ustring.gsub(text, "<%s*[Rr][Ee][Ff][^>]-/%s*>", "") -- remove refs cited elsewhere
text = mw.ustring.gsub(text, "<%s*[Rr][Ee][Ff].->.-<%s*/%s*[Rr][Ee][Ff]%s*>", "") -- remove refs
Line 600 ⟶ 604:
if not text then return err("Cannot read a valid page: first name is " .. firstpage) end
 
local keepRefstext = cleanupText(text, options.keepRefs)
local keepSubsections = options.keepSubsections
text = cleanupText(text, keepSubsections, keepRefs)
 
local pageopts = {} -- pageopts (even if value is "") have priority over global options
Line 766 ⟶ 768:
function p.checkimage(image) return checkimage(image) end
function p.parseimage(text, start) return parseimage(text, start) end
function p.cleanupText(text, keepSubsections, keepRefsoptions) return cleanupText(text, keepSubsections, keepRefsoptions) end
function p.main(pagenames, options) return main(pagenames, options) end
function p.numberflags(str) return numberflags(str) end