Advertisement
nzisaacnz

syntax highlighting

Jan 14th, 2015
246
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
HTML 5 3.54 KB | None | 0 0
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4.     <style>
  5.         #in,#out
  6.         {
  7.         display:block;
  8.         position:absolute;
  9.         width:95%;
  10.         height:95%;
  11.         white-space:pre;
  12.         font-family:monospace;
  13.         word-wrap:break-word;
  14.         background-color:rgba(0,0,0,0);
  15.         padding: 0;
  16.         margin:0;
  17.         resize: none;
  18.         border:0px solid;
  19.         letter-spacing:0.0px;
  20.         font-size: 12px;
  21.         }
  22.         #in
  23.         {
  24.         color:rgba(0,0,0,1);
  25.         }
  26.         #out
  27.         {
  28.         overflow:auto;
  29.         pointer-events:none;
  30.         }
  31.         #res
  32.         {
  33.          display:none;
  34.         }
  35.     </style>
  36.     <script>
  37.         window.onerror = function(event)
  38.         {
  39.  
  40.     Android.Shout(event);
  41.         }
  42.  
  43.         String.prototype.hashCode = function() {
  44.           var hash = 0, i, chr, len;
  45.           if (this.length == 0) return hash;
  46.           for (i = 0, len = this.length; i < len; i++) {
  47.            chr   = this.charCodeAt(i);
  48.            hash  = ((hash << 5) - hash) + chr;
  49.            hash |= 0; // Convert to 32bit integer
  50.          }
  51.          return hash;
  52.        };
  53.    </script>
  54.     <script>
  55.  
  56.   var reserved=/^(abstract|arguments|boolean|break|byte|case|catch|char|class|const|continue|debugger|default|delete|do|double|else|enum|eval|export|extends|false|final|finally|float|for|function|goto|if|implements|import|in|instanceof|int|interface|let|long|native|new|null|package|private|protected|public|return|short|static|super|switch|synchronized|this|throw|throws|transient|true|try|typeof|var|void|volatile|while|with|yield)$/;
  57.  
  58.  
  59.   var H = {};
  60.   var i=0;
  61.  
  62.   function $(id)
  63.   {
  64.     return document.getElementById(id);
  65.   }
  66.  
  67.   function highlight(text)
  68.   {
  69.     if(H[text])return H[text];
  70.     else
  71.     {
  72.       return H[text] = "hsl("+(text.hashCode()%360)+",70%,50%)";
  73.     }
  74.   }
  75.  
  76.   function Rehighlight(event){
  77.       $("out").innerHTML = $("in").value
  78.       .replace(/&([a-z]+)(;|$)/g,"&amp;$1$2")
  79.       .replace(/</g,"&lt;")
  80.      .replace(/>/g,"&gt;")
  81.       .replace(/([^a-z0-9$_]|^)([a-z0-9$_]+?)(?=[^a-z0-9$_]|$)/gi,function(a,b,c,d){
  82.  
  83.         var h = "";
  84.         if(/^&[a-z]+(;|$)/i.test(a))
  85.         {
  86.           return a;
  87.         }
  88.         if(/^[0-9]/.test(c))
  89.         {
  90.           h = "#AAAAFF";
  91.         }
  92.         else if(reserved.test(c))
  93.         {
  94.           h = "#999";
  95.         }
  96.         else
  97.         {
  98.           h = highlight(c);
  99.         }
  100.  
  101.  
  102.         return b+"<span style='color:"+h+";'>"+c+"</span>";
  103.       })
  104.       .replace(/\t/g,"<span style='letter-spacing:1.3px;'>&#09;</span>");
  105.  
  106.  
  107.       $("in").addEventListener("scroll",function(event){
  108.         $("out").scrollTop = $("in").scrollTop;
  109.         $("in").scrollTop = $("out").scrollTop;
  110.       },false);
  111.  
  112.       $("in").addEventListener("resize",function(event){
  113.         $("out").style.width = $("in").style.width;
  114.         $("out").style.height = $("in").style.height;
  115.       },false);
  116.     }
  117.  
  118.  
  119.   window.addEventListener("load",function(event){
  120.     $("in").addEventListener("input",Rehighlight,false);
  121.     switch(location.hash)
  122.     {
  123.         case "#init":
  124.             $('in').value = "function Initialise()\n{\n//access resources via Resource.id\n\n}";
  125.         break;
  126.         case "#draw":
  127.             $('in').value = "function GameLoop(step,canvas,context)\n{\n//access resources via Resource.id\n\n}";
  128.         break;
  129.     }
  130.     Rehighlight(event);
  131.   },false);
  132.   </script>
  133. </head>
  134. <body>
  135. <textarea id="in"></textarea>
  136. <div id="out"></div>
  137. <div id="res"></div>
  138. </body></html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement