Difference between revisions of "Module:Delink/sandbox"

From blackwiki
Jump to navigation Jump to search
blackwiki>Darklama
(copy from parent to allow changes without breaking original)
 
blackwiki>Darklama
(try another way, most cases covered)
Line 1: Line 1:
 
-- This module de-links most wikitext.
 
-- This module de-links most wikitext.
+
 
p = {}
+
p = {};
+
 
local function delinkReversePipeTrick(s)
+
local function internalLink(s)
     if mw.ustring.match(s, "^%[%[|.*[|\n]") then -- Check for newlines or multiple pipes.
+
     return (mw.ustring.gsub( s, "^([^|]*)(|?)(.*)$", function(l, p, t)
        return s
+
        -- check for malformed links and text
    else
+
        if mw.ustring.match( l, "[<>{}%c\n]" ) or mw.ustring.match( t, "\n" ) then
        return mw.ustring.match(s, "%[%[|(.*)%]%]")
+
            return s;
    end
+
        end
end
+
       
+
        -- remove whitespace from begining and end of link and text
local function delinkPipeTrick(s)
+
        l = mw.text.trim(l);
    local linkarea, display = "", ""
+
         t = mw.text.trim(t);
    -- We need to deal with colons, brackets, and commas, per [[Help:Pipe trick]].
+
       
+
        local file = false;
    -- First, remove the text before the first colon, if any.
+
       
    if mw.ustring.match(s, ":") then
+
         if l == "" then
        s = mw.ustring.match(s, "%[%[.-:(.*)|%]%]")
+
            return s;
    -- If there are no colons, grab all of the text apart from the square brackets and the pipe.
+
        elseif mw.ustring.match( l, "^:" ) then
    else
+
            l = mw.ustring.gsub( l, "^:+", "" );
        s = mw.ustring.match(s, "%[%[(.*)|%]%]")
+
         elseif mw.ustring.match( l, "^[Cc][Aa][Tt][Ee][Gg][Oo][Rr][Yy]:" ) then
    end
+
            return "";
+
         elseif mw.ustring.match( l, "^[Ff][Ii][Ll][Ee]:" ) then
    -- Next up, brackets and commas.
+
            file = true;
    if mw.ustring.match(s, "%(.-%)$") then -- Brackets trump commas.
+
        elseif mw.ustring.match( l, "^[Ii][Mm][Aa][Gg][Ee]:" ) then
         s = mw.ustring.match(s, "(.-) ?%(.-%)$")
+
            file = true;
    elseif mw.ustring.match(s, ",") then -- If there are no brackets, display only the text before the first comma.
+
         end
         s = mw.ustring.match(s, "(.-),.*$")
+
       
    end
+
        if file == true then
    return s
+
            return "";
end
+
        elseif p == "|" and t ~= "" then
+
            return t;
local function delinkWikilink(s)
+
        else
    local result = s
+
            l = mw.ustring.gsub( l, "(#.+)$", function(h)
    -- Deal with the reverse pipe trick.
+
                return mw.ustring.gsub( h, "%.([0-9A-Fa-f][0-9A-Fa-f])", "%%%1" );
    if mw.ustring.match(result, "%[%[|") then
+
            end);
        return delinkReversePipeTrick(result)
+
           
    end
+
            if p == "" and t == "" then
+
                return mw.uri.decode(l, "PATH");
    result = mw.uri.decode(result, "PATH") -- decode percent-encoded entities. Leave underscores and plus signs.
+
            end
    result = mw.text.decode(result, true) -- decode HTML entities.
+
           
+
            -- Need to transform text based on link per [[Help:Pipe trick]].
    -- Check for bad titles. To do this we need to find the
+
            t = mw.ustring.match( l, "#(.+)$" );
    -- title area of the link, i.e. the part before any pipes.
+
            if t ~= nil then
    local titlearea
+
                return mw.uri.decode(t, "PATH");
    if mw.ustring.match(result, "|") then -- Find if we're dealing with a piped link.
+
            end
         titlearea = mw.ustring.match(result, "^%[%[(.-)|.*%]%]")
+
           
    else
+
            t = mw.ustring.gsub( l, "^([^#]*)#$", "%1", 1 );
         titlearea = mw.ustring.match(result, "^%[%[(.-)%]%]")
+
            t = mw.ustring.gsub( t, "(.-) ?%b()$", "%1", 1 );
    end
+
            t = mw.ustring.gsub( t, "^(.-),.*$", "%1", 1 );
    -- Check for bad characters.
+
            t = mw.uri.decode(t, "PATH");
    if mw.ustring.match(titlearea, "[%[%]<>{}%%%c\n]") then
+
            return t;
         return s
+
        end
    end
+
     end, 1));
 
    -- Check for categories, interwikis, and files.
 
    local colonprefix = mw.ustring.match(result, "%[%[(.-):.*%]%]") or "" -- Get the text before the first colon.
 
    if mw.language.isKnownLanguageTag(colonprefix)
 
    or mw.ustring.match(colonprefix, "^[Cc]ategory$")
 
    or mw.ustring.match(colonprefix, "^[Ff]ile$")
 
    or mw.ustring.match(colonprefix, "^[Ii]mage$") then
 
        return ""
 
    end
 
   
 
    -- Remove the colon if the link is using the [[Help:Colon trick]].
 
    if mw.ustring.match(result, "%[%[:") then
 
        result = "[[" .. mw.ustring.match(result, "%[%[:(.*%]%])")
 
    end
 
   
 
    -- Deal with links using the [[Help:Pipe trick]].
 
    if mw.ustring.match(result, "^%[%[[^|]*|%]%]") then
 
        return delinkPipeTrick(result)
 
    end
 
   
 
    -- Find the display area of the wikilink
 
    if mw.ustring.match(result, "|") then -- Find if we're dealing with a piped link.
 
        result = mw.ustring.match(result, "^%[%[.-|(.+)%]%]")
 
        -- Remove new lines from the display of multiline piped links,
 
        -- where the pipe is before the first new line.
 
        result = mw.ustring.gsub(result, "\n", "")
 
    else
 
        result = mw.ustring.match(result, "^%[%[(.-)%]%]")
 
    end
 
      
 
    return result
 
 
end
 
end
  
local function delinkURL(s)
+
local function externalLink(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 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.
+
     return (mw.ustring.gsub(mw.text.trim(s), "^([^ \t]*)[ \t]*(.*)$", function(l, t)
    local valid_url_prefixes = {"//", "http://", "https://", "ftp://", "gopher://", "mailto:", "news:", "irc://"}
+
        t = mw.text.trim(t);
    local url_prefix
+
       
    for i,v in ipairs(valid_url_prefixes) do
+
         if t == "" then
         if mw.ustring.match(s, '^%[' .. v ..'[^"%s].*%]' ) then
+
            return s;
             url_prefix = v
+
        elseif mw.ustring.sub( l, 1, 2 ) == "//" then
            break
+
             l = "http:" .. l;
 
         end
 
         end
    end
+
       
   
+
         l = mw.uri.new( l );
    -- Get display text
+
       
    if not url_prefix then
+
        if l.protocol == nil or l.host == nil or mw.ustring.match( l.host, "%." ) == nil then
         return s
+
            return s;
    end
+
        else
    s = mw.ustring.match(s, "^%[" .. url_prefix .. "(.*)%]") -- Grab all of the text after the URL prefix and before the final square bracket.
+
            return t;
    s = mw.ustring.match(s, '^.-(["<> ].*)') or "" -- Grab all of the text after the first URL separator character ("<> ).
+
         end
    s = mw.ustring.match(s, "^%s*(%S.*)$") or "" -- If the separating character was a space, trim it off.
+
     end, 1));
   
 
    s_decoded = mw.text.decode(s, true)
 
    if mw.ustring.match(s_decoded, "%c") then
 
        return s
 
    else  
 
         return s_decoded
 
     end
 
 
end
 
end
  
local function delinkLinkClass(s, pattern, delinkFunction)
+
local function delinkLinkClass(s, pattern, delinker)
     if not type(s) == "string" then
+
     if type(s) ~= "string" then
         error("Attempt to de-link non-string input.", 2)
+
         error("Attempt to de-link non-string input.", 2);
     end
+
     elseif type(pattern) ~= "string" then
    if not ( type(pattern) == "string" and mw.ustring.sub(pattern, 1, 1) == "^" ) then
+
         error('Patterns must begin a string.', 2);
         error('Invalid pattern detected. Patterns must begin with "^".', 2)
 
 
     end
 
     end
 
     -- Iterate over the text string, and replace any matched text. using the  
 
     -- Iterate over the text string, and replace any matched text. using the  
     -- delink function. We need to iterate character by character rather
+
     -- delink function. We need to iterate one by one otherwise  nested links
     -- than just use gsub, otherwise nested links aren't detected properly.
+
     -- aren't detected properly.
     local result = ""
+
     local result, f, e, b, a = {};
     while mw.ustring.len(s) > 0 do
+
   
         -- Replace text using one iteration of gsub.
+
     repeat
         s = mw.ustring.gsub(s, pattern, delinkFunction, 1)
+
        f, e = mw.ustring.find(s, pattern);
         -- Append the left-most character to the result string.
+
       
         result = result .. mw.ustring.sub(s, 1, 1)
+
         if f == nil then
         s = mw.ustring.sub(s, 2, -1)
+
            table.insert( result, s );
     end
+
            break;
     return result
+
        end
 +
       
 +
         if f > 1 then
 +
            b = mw.ustring.gsub( mw.ustring.sub(s, 1, f-1), "(%[+)$", mw.text.nowiki, 1 );
 +
        end
 +
          
 +
         if e ~= nil then
 +
            a = mw.ustring.gsub( mw.ustring.sub(s, e+1), "^(%]+)", mw.text.nowiki, 1 );
 +
        else
 +
            a = "";
 +
        end
 +
       
 +
        -- Replace text using one iteration.
 +
         s = mw.ustring.gsub( mw.ustring.sub(s, f, e), pattern, delinker, 1 );
 +
       
 +
        -- Append to result and start after last result.
 +
        table.insert( result, b );
 +
        table.insert( result, s );
 +
        s = a;
 +
    until f == nil or s == "";
 +
      
 +
     return table.concat( result );
 
end
 
end
  
Line 150: Line 129:
 
         text = mw.ustring.gsub(text, "UNIQ%w*%-ref%-%d*%-QINU", "")
 
         text = mw.ustring.gsub(text, "UNIQ%w*%-ref%-%d*%-QINU", "")
 
     end
 
     end
     if not (args.comments == "no") then
+
     if args.comments ~= "no" then
 
         text = mw.ustring.gsub(text, "<!%-%-.-%-%->", "") -- Remove html comments.
 
         text = mw.ustring.gsub(text, "<!%-%-.-%-%->", "") -- Remove html comments.
 
     end
 
     end
     if not (args.wikilinks == "no") then
+
     if args.wikilinks ~= "no" then
         text = delinkLinkClass(text, "^%[%[.-%]%]", delinkWikilink) -- De-link wikilinks.
+
         text = delinkLinkClass(text, "%[%[([^%[%]]*)%]%]", internalLink) -- De-link wikilinks.
 
     end
 
     end
     if not (args.urls == "no") then
+
     if args.urls ~= "no" then
         text = delinkLinkClass(text, "^%[.-%]", delinkURL) -- De-link URLs.
+
         text = delinkLinkClass(text, "%[([^%[%]]*)%]", externalLink) -- De-link URLs.
 
     end
 
     end
     if not (args.whitespace == "no") then
+
     if 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.
Line 173: Line 152:
 
         -- We're being called via #invoke. If the invoking template passed any args, use
 
         -- 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.
 
         -- them. Otherwise, use the args that were passed into the template.
        args = frame:getParent().args
 
 
         for k, v in pairs(frame.args) do
 
         for k, v in pairs(frame.args) do
             args = frame.args
+
             args = frame.args;
             break
+
             break;
 
         end
 
         end
 +
        if args == nil then args = frame:getParent().args; end
 
     else
 
     else
 
         -- We're being called from another module or from the debug console, so assume
 
         -- We're being called from another module or from the debug console, so assume
 
         -- the args are passed in directly.
 
         -- the args are passed in directly.
         args = frame
+
         args = frame;
 
     end
 
     end
     return _delink(args)
+
   
 +
     return _delink(args);
 
end
 
end
  
 
return p
 
return p

Revision as of 21:03, 26 April 2013

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

-- This module de-links most wikitext.

p = {};

local function internalLink(s)
    return (mw.ustring.gsub( s, "^([^|]*)(|?)(.*)$", function(l, p, t)
        -- check for malformed links and text
        if mw.ustring.match( l, "[<>{}%c\n]" ) or mw.ustring.match( t, "\n" ) then
            return s;
        end
        
        -- remove whitespace from begining and end of link and text
        l = mw.text.trim(l);
        t = mw.text.trim(t);
        
        local file = false;
        
        if l == "" then
            return s;
        elseif mw.ustring.match( l, "^:" ) then
            l = mw.ustring.gsub( l, "^:+", "" );
        elseif mw.ustring.match( l, "^[Cc][Aa][Tt][Ee][Gg][Oo][Rr][Yy]:" ) then
            return "";
        elseif mw.ustring.match( l, "^[Ff][Ii][Ll][Ee]:" ) then
            file = true;
        elseif mw.ustring.match( l, "^[Ii][Mm][Aa][Gg][Ee]:" ) then
            file = true;
        end
        
        if file == true then
            return "";
        elseif p == "|" and t ~= "" then
            return t;
        else
            l = mw.ustring.gsub( l, "(#.+)$", function(h)
                return mw.ustring.gsub( h, "%.([0-9A-Fa-f][0-9A-Fa-f])", "%%%1" );
            end);
            
            if p == "" and t == "" then
                return mw.uri.decode(l, "PATH");
            end
            
            -- Need to transform text based on link per [[Help:Pipe trick]].
            t = mw.ustring.match( l, "#(.+)$" );
            if t ~= nil then
                return mw.uri.decode(t, "PATH");
            end
            
            t = mw.ustring.gsub( l, "^([^#]*)#$", "%1", 1 );
            t = mw.ustring.gsub( t, "(.-) ?%b()$", "%1", 1 );
            t = mw.ustring.gsub( t, "^(.-),.*$", "%1", 1 );
            t = mw.uri.decode(t, "PATH");
            return t;
        end
    end, 1));
end

local function externalLink(s)
    -- 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
    
    return (mw.ustring.gsub(mw.text.trim(s), "^([^ \t]*)[ \t]*(.*)$", function(l, t)
        t = mw.text.trim(t);
        
        if t == "" then
            return s;
        elseif mw.ustring.sub( l, 1, 2 ) == "//" then
            l = "http:" .. l;
        end
        
        l = mw.uri.new( l );
        
        if l.protocol == nil or l.host == nil or mw.ustring.match( l.host, "%." ) == nil then
            return s;
        else
            return t;
        end
    end, 1));
end

local function delinkLinkClass(s, pattern, delinker)
    if type(s) ~= "string" then
        error("Attempt to de-link non-string input.", 2);
    elseif type(pattern) ~= "string" then
        error('Patterns must begin a string.', 2);
    end
    -- Iterate over the text string, and replace any matched text. using the 
    -- delink function. We need to iterate one by one otherwise  nested links
    -- aren't detected properly.
    local result, f, e, b, a = {};
    
    repeat
        f, e = mw.ustring.find(s, pattern);
        
        if f == nil then
            table.insert( result, s );
            break;
        end
        
        if f > 1 then
            b = mw.ustring.gsub( mw.ustring.sub(s, 1, f-1), "(%[+)$", mw.text.nowiki, 1 );
        end
        
        if e ~= nil then
            a = mw.ustring.gsub( mw.ustring.sub(s, e+1), "^(%]+)", mw.text.nowiki, 1 );
        else
            a = "";
        end
        
        -- Replace text using one iteration.
        s = mw.ustring.gsub( mw.ustring.sub(s, f, e), pattern, delinker, 1 );
        
        -- Append to result and start after last result.
        table.insert( result, b );
        table.insert( result, s );
        s = a;
    until f == nil or s == "";
    
    return table.concat( result );
end

local function _delink(args)
    local text = args[1] or ""
    if args.refs == "yes" 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 args.comments ~= "no" then
        text = mw.ustring.gsub(text, "<!%-%-.-%-%->", "") -- Remove html comments.
    end
    if args.wikilinks ~= "no" then
        text = delinkLinkClass(text, "%[%[([^%[%]]*)%]%]", internalLink) -- De-link wikilinks.
    end
    if args.urls ~= "no" then
        text = delinkLinkClass(text, "%[([^%[%]]*)%]", externalLink) -- De-link URLs.
    end
    if args.whitespace ~= "no" 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
    if frame == mw.getCurrentFrame() then
        -- 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.
        for k, v in pairs(frame.args) do
            args = frame.args;
            break;
        end
        if args == nil then args = frame:getParent().args; 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

return p