Difference between revisions of "Module:Delink/sandbox"

From blackwiki
Jump to navigation Jump to search
blackwiki>Mr. Stradivarius
(try the version from fr:Module:Delink, written by User:Zebulon84)
blackwiki>Mr. Stradivarius
(start sanitizing this)
Line 1: Line 1:
 
-- This module de-links most wikitext.
 
-- This module de-links most wikitext.
 +
 +
local getArgs = require('Module:Arguments').getArgs
 +
local yesno = require('Module:Yesno')
  
 
p = {}
 
p = {}
  
local function delinkReversePipeTrick(s)
+
local function whitespaceYesno(val)
    if mw.ustring.match(s, "^%[%[|.*[|\n]") or s == "[[|]]" then -- Check for newlines or multiple pipes.
+
-- Like yesno, but trims whitespace from vals and removes blank strings.
        return s
+
if type(val) == 'string' then
    else
+
val = val:match('^%s*(.-)%s*$')
        return mw.ustring.match(s, "%[%[|(.*)%]%]")
+
if val == '' then
    end
+
return nil
 +
end
 +
end
 +
return yesno(val)
 
end
 
end
  
local function delinkPipeTrick(s)
+
function p._delinkReversePipeTrick(s)
 +
if mw.ustring.match(s, "^%[%[|.*[|\n]") or s == "[[|]]" then -- Check for newlines or multiple pipes.
 +
return s
 +
else
 +
return mw.ustring.match(s, "%[%[|(.*)%]%]")
 +
end
 +
end
 +
 
 +
function p._delinkPipeTrick(s)
 
-- s the tile area, without bracket nor pipe
 
-- s the tile area, without bracket nor pipe
    -- We need to deal with colons, brackets, and commas, per [[Help:Pipe trick]].
+
-- We need to deal with colons, brackets, and commas, per [[Help:Pipe trick]].
   
+
 
    -- First, remove the text before the first colon, if any.
+
-- First, remove the text before the first colon, if any.
    s = mw.ustring.gsub(s, "^(.-:)", "")
+
s = mw.ustring.gsub(s, "^(.-:)", "")
   
+
 
    -- Next up, brackets and commas.
+
-- Next up, brackets and commas.
    if mw.ustring.match(s, "%(.-%)$") then -- Brackets trump commas.
+
if mw.ustring.match(s, "%(.-%)$") then -- Brackets trump commas.
        s = mw.ustring.match(s, "(.-) ?%(.-%)$")
+
s = mw.ustring.match(s, "(.-) ?%(.-%)$")
    elseif mw.ustring.match(s, ",") then -- If there are no brackets, display only the text before the first comma.
+
elseif mw.ustring.match(s, ",") then -- If there are no brackets, display only the text before the first comma.
        s = mw.ustring.match(s, "(.-),.*$")
+
s = mw.ustring.match(s, "(.-),.*$")
    end
+
end
    return s
+
return s
 
end
 
end
  
local function delinkWikilink(s)
+
function p._delinkWikilink(s)
 
-- s should be a string starting by "[[" and ending by "]]". It should not contain other "]]"
 
-- s should be a string starting by "[[" and ending by "]]". It should not contain other "]]"
+
 
 
-- Deal with nested links
 
-- Deal with nested links
 
local nested
 
local nested
    nested = "[[" .. mw.ustring.gsub( mw.ustring.sub(s, 3), "%[%[.-%]%]", delinkWikilink)
+
nested = "[[" .. mw.ustring.gsub( mw.ustring.sub(s, 3), "%[%[.-%]%]", p._delinkWikilink)
    if nested ~= s then
+
if nested ~= s then
    return nested
+
return nested
    end
+
end
   
+
 
    -- Deal with the reverse pipe trick.
+
-- Deal with the reverse pipe trick.
    if mw.ustring.match(s, "^%[%[|") then
+
if mw.ustring.match(s, "^%[%[|") then
        return delinkReversePipeTrick(s)
+
return p._delinkReversePipeTrick(s)
    end
+
end
   
+
 
    local decoded = mw.uri.decode(s, "PATH") -- decode percent-encoded entities. Leave underscores and plus signs.
+
local decoded = mw.uri.decode(s, "PATH") -- decode percent-encoded entities. Leave underscores and plus signs.
    decoded = mw.text.decode(decoded, true) -- decode HTML entities.
+
decoded = mw.text.decode(decoded, true) -- decode HTML entities.
   
+
 
    -- Check for bad titles. To do this we need to find the
+
-- Check for bad titles. To do this we need to find the
    -- title area of the link, i.e. the part before any pipes.
+
-- title area of the link, i.e. the part before any pipes.
    local titlearea, display = mw.ustring.match(decoded, "^%[%[([^|%]]*)|?(.*)%]%]")
+
local titlearea, display = mw.ustring.match(decoded, "^%[%[([^|%]]*)|?(.*)%]%]")
 +
 
 +
-- Check for bad characters.
 +
if mw.ustring.match(titlearea, "[%[%]<>{}%%%c\n]") then
 +
return s
 +
end
  
    -- Check for bad characters.
+
-- Check for categories, interwikis, and files.
    if mw.ustring.match(titlearea, "[%[%]<>{}%%%c\n]") then
+
local colonprefix = mw.ustring.match(titlearea, "^(.-):") or "" -- Get the text before the first colon.
        return s
+
local ns = mw.site.namespaces[colonprefix] -- see if this is a known namespace
    end
+
if mw.language.isKnownLanguageTag(colonprefix)
   
+
or ( ns and ( ns.canonicalName == "File" or ns.canonicalName == "Category" ) ) then
    -- Check for categories, interwikis, and files.
+
return ""
    local colonprefix = mw.ustring.match(titlearea, "^(.-):") or "" -- Get the text before the first colon.
+
end
    local ns = mw.site.namespaces[colonprefix] -- see if this is a known namespace
 
    if mw.language.isKnownLanguageTag(colonprefix)
 
    or ( ns and ( ns.canonicalName == "File" or ns.canonicalName == "Category" ) ) then
 
        return ""
 
    end
 
   
 
    -- Remove the colon if the link is using the [[Help:Colon trick]].
 
    if mw.ustring.sub(titlearea, 1, 1) ==  ":" then
 
        titlearea  = mw.ustring.sub(titlearea, 2)
 
    end
 
   
 
    -- Deal with links using the [[Help:Pipe trick]].
 
    if display == "" then
 
        return delinkPipeTrick(titlearea )
 
    end
 
   
 
    -- Find the display area of the wikilink
 
    if not display then -- Find if we're dealing with a piped link.
 
        -- Remove new lines from the display of multiline piped links,
 
        -- where the pipe is before the first new line.
 
        titlearea  = mw.ustring.gsub(titlearea, "\n", "")
 
    end
 
  
    return display or titlearea  
+
-- Remove the colon if the link is using the [[Help:Colon trick]].
 +
if mw.ustring.sub(titlearea, 1, 1) ==  ":" then
 +
titlearea  = mw.ustring.sub(titlearea, 2)
 +
end
 +
 
 +
-- Deal with links using the [[Help:Pipe trick]].
 +
if display == "" then
 +
return p._delinkPipeTrick(titlearea )
 +
end
 +
 
 +
-- Find the display area of the wikilink
 +
if not display then -- Find if we're dealing with a piped link.
 +
-- Remove new lines from the display of multiline piped links,
 +
-- where the pipe is before the first new line.
 +
titlearea  = mw.ustring.gsub(titlearea, "\n", "")
 +
end
 +
 
 +
return display or titlearea  
 
end
 
end
  
local function delinkURL(s)
+
function p._delinkURL(s)
    -- Assume we have already delinked internal wikilinks, and that
+
-- Assume we have already delinked internal wikilinks, and that
    -- we have been passed some text between two square brackets [foo].
+
-- we have been passed some text between two square brackets [foo].
   
+
 
    -- If the text contains a line break it is not formatted as a URL, regardless of other content.
+
-- If the text contains a line break it is not formatted as a URL, regardless of other content.
    if mw.ustring.match(s, "\n") then
+
if mw.ustring.match(s, "\n") then
        return s
+
return s
    end
+
end
   
+
 
    -- Check if the text has a valid URL prefix and at least one valid URL character.
+
-- Check if the text has a valid URL prefix and at least one valid URL character.
    local valid_url_prefixes = {"//", "http://", "https://", "ftp://", "gopher://", "mailto:", "news:", "irc://"}  
+
local valid_url_prefixes = {"//", "http://", "https://", "ftp://", "gopher://", "mailto:", "news:", "irc://"}  
    local url_prefix
+
local url_prefix
    for i,v in ipairs(valid_url_prefixes) do
+
for i,v in ipairs(valid_url_prefixes) do
        if mw.ustring.match(s, '^%[' .. v ..'[^"%s].*%]' ) then
+
if mw.ustring.match(s, '^%[' .. v ..'[^"%s].*%]' ) then
            url_prefix = v
+
url_prefix = v
            break
+
break
        end
+
end
    end
+
end
   
+
 
    -- Get display text
+
-- Get display text
    if not url_prefix then
+
if not url_prefix then
    -- Deal with nested links or send back original string.
+
-- Deal with nested links or send back original string.
        return "[" .. mw.ustring.gsub( mw.ustring.sub(s, 2), "%[.-%]", delinkURL )
+
return "[" .. mw.ustring.gsub( mw.ustring.sub(s, 2), "%[.-%]", p._delinkURL )
    end
+
end
    s = mw.ustring.match(s, "^%[" .. url_prefix .. "(.*)%]") -- Grab all of the text after the URL prefix and before the final square bracket.
+
s = mw.ustring.match(s, "^%[" .. url_prefix .. "(.*)%]") -- Grab all of the text after the URL prefix and before the final square bracket.
    s = mw.ustring.match(s, '^.-(["<> [].*)') or "" -- Grab all of the text after the first URL separator character ("<> ).
+
s = mw.ustring.match(s, '^.-(["<> [].*)') or "" -- Grab all of the text after the first URL separator character ("<> ).
    s = mw.ustring.match(s, "^%s*(%S.*)$") or "" -- If the separating character was a space, trim it off.
+
s = mw.ustring.match(s, "^%s*(%S.*)$") or "" -- If the separating character was a space, trim it off.
   
+
 
    local s_decoded = mw.text.decode(s, true)
+
local s_decoded = mw.text.decode(s, true)
    if mw.ustring.match(s_decoded, "%c") then
+
if mw.ustring.match(s_decoded, "%c") then
        return s
+
return s
    else     
+
else     
        return s_decoded
+
return s_decoded
    end
+
end
 
end
 
end
  
 
+
function p._delink(args)
local function _delink(args)
+
local text = args[1] or ''
    local text = args[1] or ""
+
if whitespaceYesno(args.refs) then
    if args.refs == "yes" then
+
-- Remove any [[Help:Strip markers]] representing ref tags. In most situations  
        -- Remove any [[Help:Strip markers]] representing ref tags. In most situations  
+
-- this is not a good idea - only use it if you know what you are doing!
        -- this is not a good idea - only use it if you know what you are doing!
+
text = mw.ustring.gsub(text, 'UNIQ%w*%-ref%-%d*%-QINU', '')
        text = mw.ustring.gsub(text, "UNIQ%w*%-ref%-%d*%-QINU", "")
+
end
    end
+
if whitespaceYesno(args.comments) == false then
    if not (args.comments == "no") then
+
text = mw.ustring.gsub(text, '<!%-%-.-%-%->', '') -- Remove html comments.
        text = mw.ustring.gsub(text, "<!%-%-.-%-%->", "") -- Remove html comments.
+
end
    end
+
if whitespaceYesno(args.wikilinks) == false then
    if not (args.wikilinks == "no") then
+
text = mw.ustring.gsub(text, '%[%[.-%]%]', p._delinkWikilink) -- De-link wikilinks.
        text = mw.ustring.gsub(text, "%[%[.-%]%]", delinkWikilink) -- De-link wikilinks.
+
end
    end
+
if whitespaceYesno(args.urls) == false then
    if not (args.urls == "no") then
+
text = mw.ustring.gsub(text, '%[.-%]', p._delinkURL) -- De-link URLs.
        text = mw.ustring.gsub(text, "%[.-%]", delinkURL) -- De-link URLs.
+
end
    end
+
if whitespaceYesno(args.whitespace) == false then
    if not (args.whitespace == "no") then
+
-- Replace single new lines with a single space, but leave double new lines
        -- Replace single new lines with a single space, but leave double new lines
+
-- and new lines only containing spaces or tabs before a second new line.
        -- and new lines only containing spaces or tabs before a second new line.
+
text = mw.ustring.gsub(text, '([^\n \t][ \t]*)\n([ \t]*[^\n \t])', '%1 %2')
        text = mw.ustring.gsub(text, "([^\n \t][ \t]*)\n([ \t]*[^\n \t])", "%1 %2")
+
text = mw.ustring.gsub(text, '[ \t]+', ' ') -- Remove extra tabs and spaces.
        text = mw.ustring.gsub(text, "[ \t]+", " ") -- Remove extra tabs and spaces.
+
end
    end
+
return text
    return text
 
 
end
 
end
  
 
function p.delink(frame)
 
function p.delink(frame)
    local args
+
local args = frame:getParent().args
    if frame == mw.getCurrentFrame() then
+
return p._delink(args)
        -- We're being called via #invoke. If the invoking template passed any args, use
 
        -- them. Otherwise, use the args that were passed into the template.
 
        args = frame:getParent().args
 
        for k, v in pairs(frame.args) do
 
            args = frame.args
 
            break
 
        end
 
    else
 
        -- We're being called from another module or from the debug console, so assume
 
        -- the args are passed in directly.
 
        args = frame
 
    end
 
    return _delink(args)
 
 
end
 
end
  
 
return p
 
return p

Revision as of 15:15, 17 April 2014

Documentation for this module may be created at Module:Delink/sandbox/doc

-- This module de-links most wikitext.

local getArgs = require('Module:Arguments').getArgs
local yesno = require('Module:Yesno')

p = {}

local function whitespaceYesno(val)
	-- Like yesno, but trims whitespace from vals and removes blank strings.
	if type(val) == 'string' then
		val = val:match('^%s*(.-)%s*$')
		if val == '' then
			return nil
		end
	end
	return yesno(val)
end

function p._delinkReversePipeTrick(s)
	if mw.ustring.match(s, "^%[%[|.*[|\n]") or s == "[[|]]" then -- Check for newlines or multiple pipes.
		return s
	else
		return mw.ustring.match(s, "%[%[|(.*)%]%]")
	end
end

function p._delinkPipeTrick(s)
	-- s the tile area, without bracket nor pipe
	-- We need to deal with colons, brackets, and commas, per [[Help:Pipe trick]].

	-- First, remove the text before the first colon, if any.
	s = mw.ustring.gsub(s, "^(.-:)", "")

	-- Next up, brackets and commas.
	if mw.ustring.match(s, "%(.-%)$") then -- Brackets trump commas.
		s = mw.ustring.match(s, "(.-) ?%(.-%)$")
	elseif mw.ustring.match(s, ",") then -- If there are no brackets, display only the text before the first comma.
		s = mw.ustring.match(s, "(.-),.*$")
	end
	return s
end

function p._delinkWikilink(s)
	-- s should be a string starting by "[[" and ending by "]]". It should not contain other "]]"

	-- Deal with nested links
	local nested
	nested = "[[" .. mw.ustring.gsub( mw.ustring.sub(s, 3), "%[%[.-%]%]", p._delinkWikilink)
	if nested ~= s then
		return nested
	end

	-- Deal with the reverse pipe trick.
	if mw.ustring.match(s, "^%[%[|") then
		return p._delinkReversePipeTrick(s)
	end

	local decoded = mw.uri.decode(s, "PATH") -- decode percent-encoded entities. Leave underscores and plus signs.
	decoded = mw.text.decode(decoded, true) -- decode HTML entities.

	-- Check for bad titles. To do this we need to find the
	-- title area of the link, i.e. the part before any pipes.
	local titlearea, display = mw.ustring.match(decoded, "^%[%[([^|%]]*)|?(.*)%]%]")

	-- Check for bad characters.
	if mw.ustring.match(titlearea, "[%[%]<>{}%%%c\n]") then
		return s
	end

	-- Check for categories, interwikis, and files.
	local colonprefix = mw.ustring.match(titlearea, "^(.-):") or "" -- Get the text before the first colon.
	local ns = mw.site.namespaces[colonprefix] -- see if this is a known namespace
	if mw.language.isKnownLanguageTag(colonprefix)
		or ( ns and ( ns.canonicalName == "File" or ns.canonicalName == "Category" ) ) then
		return ""
	end

	-- Remove the colon if the link is using the [[Help:Colon trick]].
	if mw.ustring.sub(titlearea, 1, 1) ==  ":" then
		titlearea  = mw.ustring.sub(titlearea, 2)
	end

	-- Deal with links using the [[Help:Pipe trick]].
	if display == "" then
		return p._delinkPipeTrick(titlearea )
	end

	-- Find the display area of the wikilink
	if not display then -- Find if we're dealing with a piped link.
		-- Remove new lines from the display of multiline piped links,
		-- where the pipe is before the first new line.
		titlearea  = mw.ustring.gsub(titlearea, "\n", "")
	end

	return display or titlearea 
end

function p._delinkURL(s)
	-- Assume we have already delinked internal wikilinks, and that
	-- we have been passed some text between two square brackets [foo].

	-- If the text contains a line break it is not formatted as a URL, regardless of other content.
	if mw.ustring.match(s, "\n") then
		return s
	end

	-- Check if the text has a valid URL prefix and at least one valid URL character.
	local valid_url_prefixes = {"//", "http://", "https://", "ftp://", "gopher://", "mailto:", "news:", "irc://"} 
	local url_prefix
	for i,v in ipairs(valid_url_prefixes) do
		if mw.ustring.match(s, '^%[' .. v ..'[^"%s].*%]' ) then
			url_prefix = v
			break
		end
	end

	-- Get display text
	if not url_prefix then
		-- Deal with nested links or send back original string.
		return "[" .. mw.ustring.gsub( mw.ustring.sub(s, 2), "%[.-%]", p._delinkURL )
	end
	s = mw.ustring.match(s, "^%[" .. url_prefix .. "(.*)%]") -- Grab all of the text after the URL prefix and before the final square bracket.
	s = mw.ustring.match(s, '^.-(["<> [].*)') or "" -- Grab all of the text after the first URL separator character ("<> ).
	s = mw.ustring.match(s, "^%s*(%S.*)$") or "" -- If the separating character was a space, trim it off.

	local s_decoded = mw.text.decode(s, true)
	if mw.ustring.match(s_decoded, "%c") then
		return s
	else    
		return s_decoded
	end
end

function p._delink(args)
	local text = args[1] or ''
	if whitespaceYesno(args.refs) then
		-- Remove any [[Help:Strip markers]] representing ref tags. In most situations 
		-- this is not a good idea - only use it if you know what you are doing!
		text = mw.ustring.gsub(text, 'UNIQ%w*%-ref%-%d*%-QINU', '')
	end
	if whitespaceYesno(args.comments) == false then
		text = mw.ustring.gsub(text, '<!%-%-.-%-%->', '') -- Remove html comments.
	end
	if whitespaceYesno(args.wikilinks) == false then
		text = mw.ustring.gsub(text, '%[%[.-%]%]', p._delinkWikilink) -- De-link wikilinks.
	end
	if whitespaceYesno(args.urls) == false then
		text = mw.ustring.gsub(text, '%[.-%]', p._delinkURL) -- De-link URLs.
	end
	if whitespaceYesno(args.whitespace) == false then
		-- Replace single new lines with a single space, but leave double new lines
		-- and new lines only containing spaces or tabs before a second new line.
		text = mw.ustring.gsub(text, '([^\n \t][ \t]*)\n([ \t]*[^\n \t])', '%1 %2')
		text = mw.ustring.gsub(text, '[ \t]+', ' ') -- Remove extra tabs and spaces.
	end
	return text
end

function p.delink(frame)
	local args = frame:getParent().args
	return p._delink(args)
end

return p