Open GoogleCodeExporter opened 9 years ago
I'd like to be able to syntax highlight mixed html and javascript, too.
Original comment by JoeCoval...@gmail.com
on 7 Dec 2008 at 1:56
I hacked together a way to get javascript script blocks in HTML to be formatted
correctly. I did this by making shBrushXML use shBrushJScript. I would submit
a
diff, but I have other hacks in my copy, so I'll just describe the changes
instead.
First, a few tweaks to jsCore.js. Add this function:
// Cull matches that are inside other matches.
// This process gets rid of highlighted strings inside comments, keywords inside
strings, etc.
// Part of Highlight() implementation exposed for use by complex brush
implementations
dp.sh.Highlighter.prototype.CullNested = function()
{
if(this.hasOwnProperty('culledOnce')){
//rewrite matches array, squishing gaps
//this is required to allow CullNested to be run multiple
//times on the same data
var newMatches = [];
for(var i = 0; i < this.matches.length; i++){
if(this.matches[i])
newMatches[newMatches.length] = this.matches[i];
}
this.matches = newMatches;
}
// sort the matches
this.matches = this.matches.sort(dp.sh.Highlighter.SortCallback);
for(var i = 0; i < this.matches.length; i++){
if(this.IsInside(this.matches[i]))
this.matches[i] = null;
}
this.culledOnce = true
}
Then, in jsCore.js, modify dp.sh.Highlighter.prototype.Highlight to make use of
this
new function:
. . .
// if no matches found, add entire code as plain text
if(this.matches.length == 0)
{
this.AddBit(this.code, null);
this.SwitchToList();
this.div.appendChild(this.bar);
this.div.appendChild(this.ol);
return;
}
this.CullNested();
// Finally, go through the final list of matches and pull the all
// together adding everything in between that isn't a match.
for(var i = 0; i < this.matches.length; i++)
. . .
Modify shBrushXml.js. First, modify the constructor to have the javascript
brush css
class as well:
dp.sh.Brushes.Xml = function()
{
this.CssClass = 'dp-xml dp-c';
. . .
Then, modify the function ProcessRegexList in shBrushXml.js to end like so:
//script blocks <script>(.*)</script>
//we'll mark these with cssClass 'javascript' and then
//reprocess with javascript brush below
regex = new
RegExp('(?:\<|<)script(?:\>|>)((?:.|\\n)*)(?:\<|<)/script(?:\>|>)', 'gm');
while((match = regex.exec(this.code)) != null)
{
if(match[1] == null)
{
continue;
}
push(this.matches, new dp.sh.Match(match[1],
match.index+match[0].indexOf(match[1]),
'javascript'));
}
//Do this once so that javascript blocks cull nested XML
//matches before we find the javascript matches in the javascript block
this.CullNested()
//Replace the javascript block match with matches found by javascript brush
//within the block
for(var i = 0; i < this.matches.length; i ++){
var match = this.matches[i];
if(match && match.css == 'javascript'){
this.matches[i] = null;
var brush = new dp.sh.Brushes.JScript();
brush.matches=[]
brush.code = match.value;
brush.ProcessRegexList();
for(var j = 0; j < brush.matches.length; j++){
var brushMatch = brush.matches[j];
brushMatch.index += match.index;
this.matches[this.matches.length] = brushMatch;
}
}
}
Original comment by JoeCoval...@gmail.com
on 8 Dec 2008 at 4:46
That appears a little jumbled, in case it helps, here's my files. Like I said,
there
are other hacks in here, too.
Original comment by JoeCoval...@gmail.com
on 8 Dec 2008 at 4:48
Attachments:
Original issue reported on code.google.com by
ria...@gmail.com
on 26 Jul 2008 at 11:32