sonota (owner)

Revisions

gist: 228735 Download_button fork
public
Public Clone URL: git://gist.github.com/228735.git
Embed All Files: show embed
ez-hilite-lisp.js #
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
/*
The MIT License
 
Copyright (c) 2009 sonota <yosiot8753@gmail.com>
 
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
 
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
 
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
 
(function(){
   var ezHiliteLisp = function(){
     this.BEGIN = 0;
     this.END = 1;
 
     // double as stylesheet class name
     this.typePlain = "plain";
     this.typeComment = "lisp_comment";
     this.typeDoubleQuote = "lisp_double_quote";
     this.typeParenthesis = "lisp_parenthesis";
     this.typeSymbol = "lisp_symbol";
 
     this.coarseTokenize = function(_str){
       this.str = _str.replace("\r\n", "\n");
       this.result = [];
       this.buf = "";
       this.current_type = this.typePlain;
       var out_of_quote_double = true;
       var out_of_comment_single = true;
       var length = null;
 
       while( this.str.length > 0){
         //console.log([this.str.substr(0,1), out_of_comment_single], this.buf);
 
         if( this.str.match(/^(")/)
             && out_of_quote_double && out_of_comment_single)
         {
           // begin double quote
 
           length = RegExp.$1.length;
           this.shift_token(length, this.typePlain, this.typeDoubleQuote, this.BEGIN);
           out_of_quote_double = false;
 
         }else if( this.str.match(/^(")/)
                   && !(out_of_quote_double) && out_of_comment_single)
         {
           // end double quote
           
           length = RegExp.$1.length;
           this.shift_token(length, this.typeDoubleQuote, this.typePlain, this.END);
           out_of_quote_double = true;
 
         }else if( this.str.match(/^(;)/)
                   && out_of_quote_double && out_of_comment_single)
         {
           // begin single line comment
           
           length = RegExp.$1.length;
           this.shift_token(length, this.typePlain, this.typeComment, this.BEGIN);
           out_of_comment_single = false;
           
         }else if(this.str.match(/^(\n)/)
                  && out_of_quote_double && !(out_of_comment_single))
         {
           // end single line comment
 
           length = RegExp.$1.length;
           this.shift_token(length, this.typeComment, this.typePlain, this.END);
           out_of_comment_single = true;
           
         }else if(this.str.match( /^\\/)){
           this.shiftNChar(2);
           
         }else{
           this.shiftNChar(1);
         }
       }
 
       if( (this.buf + this.str).length > 0 ){
         this.result.push({
                            type: this.current_type,
                            string: this.buf + this.str
                          });
       }
       
       return this.result;
     };
 
 
     this.shift_token = function(length, _type, _current_type, flag){
       if( flag == this.BEGIN ){
         if( this.buf.length > 0 ){
           this.result.push({
                              type: _type,
                              string: this.buf
                            });
         }
         this.buf = "";
         this.shiftNChar(length);
       }else if( flag == this.END ){
         if( this.buf.length > 0 ){
           this.result.push({
                              type: _type,
                              string: this.buf + this.str.substr(0, length)
                            });
         }
         this.buf = "";
         this.str = this.str.substr(length, this.str.length-1);
       }else{
         throw('invalid argument: "flag"');
       }
 
       this.current_type = _current_type;
     };
 
 
     this.shiftNChar = function(n){
       this.buf += this.str.substr(0, n);
       this.str = this.str.substr(n, this.str.length-1);
     };
 
 
     this.format = function(code){
       var coarseTokens = this.coarseTokenize(code);
 
       var result = "";
       var type = null;
       var token = null;
       for(var a=0; a<coarseTokens.length; a++){
         token = coarseTokens[a];
         //console.log([ token["type"], token["string"] ]);
         if(token["type"] == this.typePlain){
           for(var b=0; b<token["string"].length; b++){
             // markup only parenthesis
             if(token["string"][b].match(/^\(|\)/)){
               result += '<span class="' + this.typeParenthesis + '">' + token["string"][b] + '</span>';
             }else if(token["string"][b].match(/^['\.]/)){
               result += '<span class="' + this.typeSymbol + '">' + token["string"][b] + '</span>';
             }else{
               result += token["string"][b];
             }
           }
         }else{
           result += '<span class="' + token["type"] + '">';
           result += token["string"];
           result += '</span>';
         }
       }
       return result;
     };
 
     
     this.$class = function(root, className){
       var elems = root.getElementsByTagName("*");
       var result = [];
       for(var a in elems){
         try{
           if(elems[a].getAttribute("class").match(className)){
             result.push(elems[a]);
           }
         }catch(e){
           // console.log(e);
         }
       }
       return result;
     };
 
 
     this.unescapeHtml = function(htmlCode){
       htmlCode = htmlCode.replace(/&lt;/g, "<");
       htmlCode = htmlCode.replace(/&gt;/g, ">");
       htmlCode = htmlCode.replace(/&amp;/g, "&");
       return htmlCode;
     };
 
 
     this.main = function(){
       var preList = this.$class(document, "ez-hilite-lisp");
       if(preList.length == 0){ return; }
       
       for(var a in preList){
         preList[a].innerHTML = this.format( this.unescapeHtml(preList[a].innerHTML) );
       }
     };
   };
 
 
   var ezHiliteLispMain = function(){
     if(document.getElementsByTagName("pre").length == 0){
       return;
     }
 
     (new ezHiliteLisp()).main();
   };
 
 
   if(navigator.userAgent.indexOf("MSIE") == -1){
     window.addEventListener("load", ezHiliteLispMain, true);
   };
 })();