Created
June 9, 2012 19:58
-
-
Save photofroggy/2902356 to your computer and use it in GitHub Desktop.
new regex parsing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// This stuff determines how certain tablumps are handled. | |
// lumps[tag] => [ chunks, callback ] | |
this.lumps = { | |
'&a\t': [ | |
2, function( data ) { | |
return '<a target="_blank" href="'+data[0]+'" title="'+data[1]+'">'; | |
} | |
] | |
}; | |
/* Parse tablumps! | |
* This implementation hopefully only uses simple string operations. | |
*/ | |
sparse: function( data ) { | |
if( !data ) | |
return ''; | |
for( tag in this.lumps ) { | |
while( (i = data.indexOf( tag )) > -1 ) { | |
info = this.crop( data, tag, i, this.lumps[tag][0] ); | |
data = info[0] + this.lumps[tag][1]( info[1] ) + info[2]; | |
} | |
} | |
data = data.replace(this.simpler[0], this.simpler[1]); | |
return data; | |
}, | |
/* Crop a tablump! | |
* When we have found a tablump in a string, we want to replace it with | |
* valid HTML. Before we do this we must extract the tablump from the | |
* string, and also extract the information contained in the tablump. | |
* | |
* This method simply starts at the beginning of the tablump, and | |
* extracts ``limit`` bits of data from the tablump. This is used in | |
* conjunction with the ``lumps`` object in this class. | |
*/ | |
crop: function( data, tag, index, limit ) { | |
start = data.substring(0, index); | |
rest = data.substring(index + tag.length); | |
bits = []; | |
for( i = limit; i > 0; i-- ) { | |
find = rest.indexOf('\t'); | |
if( find == -1 ) | |
break; | |
bits.push( rest.substring(0, find) ); | |
rest = rest.substring(find + 1); | |
} | |
return [start, bits, rest]; | |
}, |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment