Module:Excerpt: Difference between revisions

Content deleted Content added
Adding Refn to list of removable templates
Parse templates more carefully to catch unwanted templates with other templates nested inside. Also removing a few redundant % signs.
Line 41:
local function argimage(text)
local token = nil
if mw.ustring.match(text, "%{%{%s*[Ii]nfobox") then
local image = mw.ustring.match(text, "|%s*image%s*=%s*([^%}|]*)") -- parse image= argument...
or mw.ustring.match(text, "|%s*Cover%s*=%s*([^%}|]-)") -- or Cover= from Infobox album
if image then -- add in relevant parameters: caption, alt text and image size
token = "[[" -- Add File: unless name already begins File: or Image:
Line 51:
end
token = token .. image
local caption = mw.ustring.match(text, "|%s*[Cc]aption%s*=%s*([^%}|]*)")
if caption then token = token .. "|" .. caption end
local alt = mw.ustring.match(text, "|%s*alt%s*=%s*([^%}|]*)")
if alt then token = token .. "|alt=" .. alt end
local image_size = mw.ustring.match(text, "|%s*image_size%s*=%s*([^%}|]*)")
if image_size then token = token .. "|" .. image_size end
token = mw.ustring.gsub(token, "\n","") .. "]]\n"
Line 62:
 
return token
end
 
-- Help gsub to remove unwanted templates
-- If template is unwanted then return "" (replace by nothing) else return nil (keep existing string)
 
local function striptemplate(t)
forlocal _, t inunwanted pairs= {"[Ee]fn", "[Ee]fn-la", "[Ee]l[mn]", "[Rr]p?", "[Ss]fn[bp]", "[Ss]f[bn]", "NoteTag", "#[Tt]ag:%s*[Rr]ef", "[Rr]efn?",
"[CcDd]n", "[Cc]itation needed", "[Dd]isambiguation needed"} do
for _, u in pairs(unwanted) do
if mw.ustring.match(t, "^{{%s*" .. u .. "%s*%f[|}]") then return "" end -- unwanted template: remove
end
return nil -- not an unwanted template: keep
end
 
Line 105 ⟶ 117:
text = mw.ustring.gsub(text, "<%s*ref.->.-<%s*/%s*ref%s*>", "") -- remove refs
text = mw.ustring.gsub(text, "<%s*imagemap.->.-<%s*/%s*imagemap%s*>", "") -- remove imagemaps
text = mw.ustring.gsub(text, "%b{}", striptemplate) -- remove unwanted templates such as references
for _, t in pairs {"[Ee]fn", "[Ee]fn-la", "[Ee]l[mn]", "[Rr]p?", "[Ss]fn[bp]", "[Ss]f[bn]", "NoteTag", "#[Tt]ag:%s*[Rr]ef", "[Rr]efn",
"[CcDd]n", "[Cc]itation needed", "[Dd]isambiguation needed"} do
text = mw.ustring.gsub(text, "{{%s*" .. t .. "%s*|.-}}", "") -- remove ref and footnote templates
end
text = mw.ustring.gsub(text, "\n%s*{{%s*[Tt][Oo][Cc].-}}", "\n") -- remove most common tables of contents