Post Reply 
convert links
Aug. 07, 2007, 04:33 AM
Post: #46
RE: convert links
JJoe Wrote:I'll modify the other filters to use the 'Set some flags...' filter and post them later.
Not polished or tested, they are a start. Expect misses.

Code:
[Patterns]
Name = "Is it safe? Belongs at top! ProxN4.5 only! ReadMe! (test Aug,06,2007)"
Active = TRUE
URL = "$TYPE(htm)"
Limit = 256
Match = "<html*>$SET(htmlTag=1)(^)   Some $TYPE(htm) pages aren't. Check for <html> tag"
        "|<script*>$SET(nscript=1)(^)Script start set nscript flag "
        "|</script>$SET(nscript=)(^) Script end clear nscript flag"
        "|<style$SET(nstyle=1)(^)    Style start set nstyle flag"
        "|</style>$SET(nstyle=)(^)   Style end set nstyle flag"
        "|<$SET(ncode=1)(^)          Could be some code start set ncode flag"
        "|>$SET(ncode=)(^)           Could be some code end set ncode flag"
        "|< a\s*>$SET(nlink=1)(^)    Link start set nlink flag"
        "|</a>$SET(nlink=)(^)        Link end set nlink flag"
        "|$SET(nview=)"
        "$TST(htmlTag=1)"
        "(^$TST(ncode=1))"
        "(^$TST(nlink=1))"
        "(^$TST(nscript=1))"
        "(^$TST(nstyle=1))"
        "$SET(nview=1)(^)            If there is a <html> tag and all the other flags are empty, "
        "                            hope it's safe and set nview to 1."
Replace = "Since nview is being used by filters that make mostly cosmetic changes,"
          "the hope is that this filter will set the variable nview to 1 *only*"
          "when the Proxomitron is examining the characters that are simply displayed by the browser."
          "Not making a desired change is better than breaking the page, imo."
          "If necessary, a site specific filter could correct the mistake "
          "and force the change."
          ""
          "So don't depend on just this filter to 'flag' scripts, css, links, tags, etc."
          "Example: "
          "If another filter removes a script after this filter has set the nscript flag,"
          "nscript will continue to indicate that a script is being examined."
          ""
          "(^) never matches. This filter never matches. It just sets some variables."

Name = "Create links from text (test Aug,06,2007)  ProxN4.5 only!"
Active = TRUE
URL = "$TYPE(htm)"
Limit = 256
Match = "(http(s|)://)\1$TST(nview=1)([^< ]++)\2([.,'")]+(\s|<))\3"
Replace = "<a href="\1\2">\1\2</a>\3"

# This filter tries to make links from text like http://www.proxomitron.info/ .
# There is also Scojo's Convert-Text-URLs-to-Links.zip at
# http://tech.groups.yahoo.com/group/prox-list/files/Filters/_Other/


# Enable only one of the following.
# These filters try to make long lines of text wrap.
# Not polished or tested, they are a start. Expect misses.
# Browser support is iffy.

Name = "Force wrap soft (test Aug,06,2007)  ProxN4.5 only!"
Active = FALSE
URL = "$TYPE(htm)"
Limit = 256
Match = "(([^<>& \r\n\t])\#(\&#38;shy;|\&#38;nbsp;|\&#38;#8203;|<wbr*>)+)+{30}"
        "$TST(nview=1)"
        "$SET(#=<WBR>)"
        "|<pre>$SET(#=<pre wrap style="white-space: pre-wrap; word-wrap: break-word;">)$SET(ncode=)"
Replace = "\@"

# Filter inserts a nonstandard tag that allows browsers that support <WBR> to wrap text.
# Filter adds various options to the <pre> tag that may cause some browsers to wrap text between <pre> tags.
# Copied and pasted text should not inherit the <WBR> tags or wrap.

Name = "Force wrap hard with <WBR>&shy; (test Aug,06,2007)  ProxN4.5 only!"
Active = FALSE
URL = "$TYPE(htm)"
Limit = 256
Match = "(([^<>& \r\n\t])\#(\&#38;shy;|\&#38;nbsp;|\&#38;#8203;|<wbr*>)+)+{20}"
        "$TST(nview=1)"
        "$SET(#=<WBR>&shy;)"
        "|<pre>$SET(#=<pre wrap style="white-space: pre-wrap; word-wrap: break-word;">)$SET(ncode=)"
Replace = "\@"

# Filter inserts <WBR>&shy;. &shy is standard but not supported by all browsers.
# &shy; should cause the browser to wrap the text and display a hyphen at the break.
# The "shy hyphen", &shy;, should not be seen,  if the text does not wrap.
# Filter adds various options to the <pre> tag that may cause some browsers to wrap text between <pre> tags.
# Copied and pasted text will have extra characters!.

Name = "Force wrap blunt (test Aug,06,2007)  ProxN4.5 only!"
Active = FALSE
URL = "$TYPE(htm)"
Limit = 256
Match = "(([^<>\r\n]|\&#38;[^; ]+{3,5};+)\#)++{90,100}\s"
        "$TST(nview=1)"
Replace = "\@<br>"

# Filter attempts to add breaks (<br>), at a space, every 90 to 100 characters.
# Play with the numbers, if the results don't look good.
# Copied and pasted text does inherit the wrap caused by <br>.

# Have fun.

Have fun.
Add Thank You Quote this message in a reply
Jan. 02, 2009, 02:47 AM
Post: #47
RE: convert links
Currently the opera browser people are using this script. Would it be faster than what is currently implemented?

Code:
// ==UserScript==
// @name Linkify text files
// @author Arve Bersvendsen
// @namespace http://userjs.org/
// @version 1.4.1
// @description  Improves display of text files by making
//            http/https/ftp URLs in text documents
//            clickable.
// @ujs:category browser: enhancements
// @ujs:published 2005-10-25 21:12
// @ujs:modified 2005-10-25 22:13
// @ujs:documentation http://userjs.org/scripts/browser/enhancements/linkify-text-files
// @ujs:download http://userjs.org/scripts/download/browser/enhancements/linkify-txt.js
// @include *
// ==/UserScript==

/*
* Copyright © 2005 by Arve Bersvendsen
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
* USA
*/


// Enable the script if we're displaying txt, css, js, and nfo files
if (window.location.PRXhref.match(/\.(txt|js|css|nfo)$/i) ){
  // Cannot check directly on top, since that will throw errors
  // for all inline frames living on a different server; AdSense ads
  document.addEventListener('load',function(e){

    //option - show a "reformatting" notice - set to false to disable
    var showNotice = true;
    
    // Test to see that we're actually using this
    // on documents Opera are displaying as text
    if( (document.body) && (document.body.childNodes.length == 1) && (document.body.firstChild.nodeName == "PRE")){
      var self=this;

      this.toggleNumbers = function(){
        if (document.getElementById('ol').className ==  "not_numbered"){
          document.getElementById('ol').className = "numbered";
        } else {
          document.getElementById('ol').className = "not_numbered";
        }
      }
      
      this.highlightRow = function(){
        var li, lists =document.getElementsByTagName("li");
        var num = prompt("Go to line:")-1;
        if (lists[num]) {
          for (var i = 0; li = lists[i]; i++) {
            li.removeAttribute("class");
          }
          lists[num].setAttribute("class","highlight");
          window.scrollTo(0,lists[num].offsetTop);
          // force Reflow:
          document.body.className = document.body.className;
        }
      }

      String.prototype.rStrip = function(){
        var endval = "";
        var link = arguments[0];
        // try to deal with ampersands, quotes and similar in a nice fashion
        if (link.substr(-6).indexOf("&quot;") != -1) {
          endval = "&quot;"
          link = link.substring(0,link.length-6);
        } else if (link.substr(-4).indexOf("&gt;") != -1) {
          endval = "&gt;"
          link = link.substring(0,link.length-4);
        } else if (link.substr(-1).match(/[,.!?\)\']$/)) {
          endval = link.substr(-1);
          if (link.substr(-7,6).indexOf("&quot;") != -1) {
            endval = "&quot;"+endval;
            link = link.substring(0,link.length-7);
          } else if (link.substr(-5,4).indexOf("&gt;") != -1) {
            endval = "&gt;"+endval;
            link = link.substring(0,link.length-5);
          } else {
            link = link.substring(0,link.length-1);
          }
        }
      return "<a href=\""+link+"\">"+link+"</a>"+endval;
      }
      
      // get original white-space. Used for a terrible hack to prevent
      // blank lines on copy/paste operations
      var whitespace = getComputedStyle(document.getElementsByTagName('pre')[0]).whiteSpace;
      // Append style sheet
      var style = (
         "pre,ol,li { white-space: normal;}\n"
       + "span { white-space: "+whitespace+"; color: black; }\n"
       + "span,ol,li {line-height:inherit; }\n"
       + "li { color: #999; }\n"
       + "span { color: #000; display:inline}\n"
       + "ol.not_numbered { margin: 0; padding: 0 }\n"
       + "ol.not_numbered li { display: block}\n"
       + "ol.numbered li { display: list-item}\n"
       + "li.highlight { background: #eee;}"
      );
      var s = document.createElement("style");
      var s_content = document.createTextNode(style);
      s.appendChild(s_content);
      document.documentElement.appendChild(s);
      
      // Append clickable wave
      var mode = document.createElement("a");
      mode.style="position: fixed; top: 0; right: 0; padding: 0.2em; border: solid black; border-width: 0 0 1px 1px;";
      mode.addEventListener("click",self.toggleNumbers,false);
      s_content = document.createTextNode("\u21D4");
      mode.appendChild(s_content);
      document.body.appendChild(mode);

      // Append reformatting notice
      if( showNotice ) {
        var showNotice = document.createElement("div");
        showNotice.style="position: fixed; top: 50%; left: 50%; margin: -1em 0 0 -6em; padding: 0.2em; border: 1px solid black; background-color: #ddd;";
        showNotice.appendChild(document.createTextNode('Reformatting and finding links.'));
        showNotice.appendChild(document.createElement('br'));
        showNotice.appendChild(document.createTextNode('Please wait ...'));
        document.body.appendChild(showNotice);
      }

      // Search for http/https/ftp URLs and make them clickable
      // Wrap a line around every line

      var urlRegex = /\b(((https?|ftp|irc|telnet|nntp|gopher|file):\/\/|(mailto|news|data):)[^\s\"<>\{\}\'\(\)]*)/g;
      var doc = document.getElementsByTagName('pre')[0];
      
      doc.innerHTML = "<ol id=\"ol\" class=\"not_numbered\">"+doc.innerHTML.replace(/\%0C/g,"
").replace(/([^\r\n]*)(\r?\n|\r)/gi,"<li><span>$1 </span></li>").replace(urlRegex,RegExp.$1.rStrip)+"</ol>";

      if( showNotice ) { showNotice.parentNode.removeChild(showNotice); }

      // Listen to the "l" key to toggle line number visibility
      document.onkeypress=function(e){
        switch (e.keyCode) {
          case 103:
            self.highlightRow();
            break;
          case 108:
            self.toggleNumbers();
            break;
          default:
            break;
        }
      }
    }
  },false)
}
Add Thank You Quote this message in a reply
Post Reply 


Forum Jump: