Guest User

Live BBCode Preview

a guest
Feb 24th, 2012
42
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // ABBC Live Compiler for http://dis.4chan.org/
  2. // by Tablecat <http://tablecat.co.cc>
  3.  
  4. var init = function() {
  5.     var forms = document.forms;
  6.     for (var i = 0; i < forms.length; i++) {
  7.         if (forms[i].action === 'http://dis.4chan.org/derefer') continue;
  8.         var preview = document.createElement('div');
  9.         preview.className = 'preview';
  10.         preview.style.display = 'none';
  11.         forms[i].appendChild(preview);
  12.         var textarea = forms[i].getElementsByTagName('textarea')[0];
  13.         textarea.onchange = textarea.onkeyup = function() {
  14.             var comment = this.value;
  15.             var thread = this.parentNode.parentNode.parentNode.parentNode.parentNode.id.value;
  16.             var script = window.location.href.replace(/^http:\/\/dis\.4chan\.org\/([^\.]+)/, '$1');
  17.             comment = markup(clean_string(comment), thread, script)[0];
  18.             this.parentNode.parentNode.parentNode.parentNode.parentNode.lastChild.style.display = comment ? 'inline-block' : 'none';
  19.             this.parentNode.parentNode.parentNode.parentNode.parentNode.lastChild.innerHTML = comment;
  20.         }
  21.     }
  22. };
  23.  
  24. var clean_string = function(str) {
  25.     str = str.replace(/[\x00-\x08\x0B-\x1F\x7F]/g, "");
  26.     str = str.replace(/^(\n|\x20)+|[\s]+$/g, "");
  27.     str = str.replace(/&/g, "&amp;");
  28.     str = str.replace(/</g, "&lt;");
  29.     str = str.replace(/>/g, "&gt;");
  30.     str = str.replace(/\"/g, "&quot;");
  31.     return str;
  32. };
  33.  
  34. var markup = function(str, thread, script) {
  35.     var tagList = [ "#" , "code" , "b" , "i" , "o" , "u" , "s" , "aa" , "m" , "spoiler" , "sup" , "sub" ];
  36.     var malformed = 0;
  37.     var quoteRe = /&gt;&gt;([0-9\-\,l]+)/g;
  38.     str = str.replace(quoteRe, '<a href="read/'+script+thread+'/$1">&gt;&gt;$1</a>');
  39.     // super tags
  40.     for (var i = 0; i < tagList.length; i++) {
  41.         var tag = tagList[i],
  42.         re = new RegExp("(^|[\\s\\S]*?)\\["+tag+"\\]([\\s\\S]*?)\\[\\/"+tag+"\\]($|[\\s\\S]*)", "g");
  43.         while ( str.match(re) ) {
  44.             if (tag === "#")
  45.                 str = str.replace(re, "$1"+escape_tag(RegExp.$2)+"$3");
  46.             else if (tag === "code")
  47.                 str = str.replace(re, "$1<code>"+escape_tag(RegExp.$2)+"</code>$3");
  48.             else
  49.                 break;
  50.         }
  51.     }
  52.     // quote
  53.     var pattern = new RegExp("(\\n|^)&gt; (.*?)(\\n|$)", "g");
  54.     while ( str.match(pattern) )
  55.         str = str.replace(pattern, "$1<span class=\"quote\">$2</span>$3");
  56.     // regular tags
  57.     for (var i = 0; i < tagList.length; i++) {
  58.         var tag = tagList[i],
  59.             re = new RegExp("(^|[\\s\\S]*)\\["+tag+"\\]([\\s\\S]*?)\\[/"+tag+"\\]($|[\\s\\S]*)", "g");
  60.         while ( str.match(re) ) {
  61.             var before = str.replace(re, "$1"),
  62.             content = str.replace(re, "$2"),
  63.             after = str.replace(re, "$3");
  64.             if (fail_check(content, tagList)) {
  65.                 str = before + "\x1FB"+tag+"\x1FC" + escape_tag(content) + "\x1FB/"+tag+"\x1FC" + after;
  66.                 malformed++;
  67.             }
  68.             else if (tag.match("^(b|i|s|u|sup|sub)$"))
  69.                 str = before + '<' + tag + '>' + content + '</' + tag + '>' + after;
  70.             else if (tag === "m")
  71.                 str = before + '<tt>' + content + '</tt>' + after;
  72.             else if (tag === "rem")
  73.                 str = before + after;
  74.             else if (tag.match("^(spoiler|aa|o)$"))
  75.                 str = before + '<span class="' + tag + '">' + content + '</span>' + after;
  76.             else
  77.                 break;
  78.         }
  79.     }
  80.     str = str.replace(/\n/g, "<br>");
  81.     str = '<blockquote style="border:1px dashed #000;padding:0.5em;background:#EFEFEF">'+str+'</blockquote>';
  82.     //if (malformed > 0) str += '<div style="color:red"><b>Warning: Malformed markup, '+malformed+' error(s)</b></div>';
  83.     return [unescape_tag(str), malformed];
  84. };
  85.  
  86. var escape_tag = function(str) {
  87.     str = str.replace(/&gt;/g, "\x1FA");
  88.     str = str.replace(/\[/g, "\x1FB");
  89.     str = str.replace(/\]/g, "\x1FC");
  90.     return str;
  91. };
  92.  
  93. var unescape_tag = function(str) {
  94.     str = str.replace(/\x1FA/g, "&gt;");
  95.     str = str.replace(/\x1FB/g, "[");
  96.     str = str.replace(/\x1FC/g, "]");
  97.     return str;
  98. };
  99.  
  100. var fail_check = function(str, tagList) {
  101.     for (var i = 0; i < tagList.length; i++) {
  102.         var tag = tagList[i],
  103.             reup = new RegExp("(.*)\\["+tag+"\\](.*)","g"),
  104.             redwn = new RegExp("(.*)\\[/"+tag+"\\](.*)","g"),
  105.             count = 0;
  106.         while ( str.match(reup) ) {
  107.             str = str.replace(reup, "$1$2");
  108.             count++;
  109.         }
  110.         while ( str.match(redwn) ) {
  111.             str = str.replace(redwn, "$1$2");
  112.             count--;
  113.         }
  114.         if (count !== 0) return true;
  115.     }
  116.     return false;
  117. };
  118.  
  119. if (window.location.href.search('^http://dis.4chan.org/[^\.]+') != -1) {
  120.     window.onload = function() { init(); };
  121. }
Advertisement
Add Comment
Please, Sign In to add comment