Create a gist now

Instantly share code, notes, and snippets.

/*
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);
};
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment