Module:Excerpt/sandbox: Difference between revisions

Content deleted Content added
better error handling
Merge falsy() and is() functions
Line 11:
end
 
-- Helper function to test for truthy and falsy values
local function falsyis(value)
if not value or value == "" or value == "0" or value == "false" or value == "no" then
return truefalse
end
return falsetrue
end
 
Line 425:
t = t .. token -- keep wanted block templates
 
elseif not falsyis(options.keepTables) and mw.ustring.sub(token, 1, 2) == '{|' then
t = t .. token -- keep tables
 
Line 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 falsyis(options.fragment) then
local escapedFragment = mw.ustring.gsub( options.fragment, "[%^%$%(%)%%%.%[%]%*%+%-%?]", "%%%0" )
local fragments = ""
Line 507:
text = fragments
end
if falsynot is(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 falsynot is(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 650:
 
-- remove '''bold text''' if requested
if not falsyis(pageopts.nobold) then text = mw.ustring.gsub(text, "'''", "") end
 
text = filetext .. text
Line 763:
return frame:preprocess(text)
end
end
 
local function is(v)
return (v or '') ~= ''
end