Wednesday, October 01, 2008

shBrushBatch for Google SyntaxHighlighter

I really like highlighting my code in this blog. Ever since finding the Google SyntaxHighlighter Widget at FaziBear's, I've been going through all my old code, and cleaning up my posts.

I read a nice article at WaltCo Tech about extending it to incorporate other brushes for WordPress. He also seems to do what I do, but a bit better when it comes to explaining things: WriteItDownNowBeforeYouForgetHowYouDidThatOldMan

I'm really not that old, but I do tend to stick my fingers in many things, and coding tends to be one of them. At times, I'll find myself trying in vain to use a function from VB in PHP, or something of that nature. One of the hazards of not being fluent maybe?

In any case, I've created my own brush for DOS Batch scripts:

dp.sh.Brushes.Batch = function()
{
    var builtins =  'APPEND ATTRIB CD CHDIR CHKDSK CHOICE CLS COPY DEL ERASE DELTREE ' +
                    'DIR EXIT FC COMP FDISK FIND FORMAT FSUTIL HELP JOIN ' +
                    'LABEL LOADFIX MK MKDIR MEM MEMMAKER MORE MOVE MSD PCPARK ' +
                    'PRINT RD RMDIR REN SCANDISK SHARE SORT SUBST SYS ' +
                    'TIME DATE TREE TRUENAME TYPE UNDELETE VER XCOPY';

    var keywords =  'DO ELSE FOR IN CALL CHOICE GOTO SHIFT PAUSE ERRORLEVEL ' +
      'IF NOT EXIST LFNFOR START SETLOCAL ENDLOCAL ECHO SET';

    this.regexList = [
 {regex: new RegExp('REM.*$', 'gm'), css: 'comment'},
 {regex: new RegExp('::.*$', 'gm'), css: 'comment'},
 {regex: dp.sh.RegexLib.DoubleQuotedString, css: 'string'},
 {regex: dp.sh.RegexLib.SingleQuotedString, css: 'string'},
 {regex: new RegExp('[()[\\]{}]', 'g'), css: 'delim'},
 {regex: new RegExp('%\\w+%', 'g'), css: 'vars'},
 {regex: new RegExp('%%\\w+', 'g'), css: 'vars'},
 {regex: new RegExp('\\w+=', 'g'), css: 'vars'},
 {regex: new RegExp('@\\w+', 'g'), css: 'keyword'},
 {regex: new RegExp(':\\w+', 'g'), css: 'keyword'},
 {regex: new RegExp('\\s/\\w+', 'g'), css: 'flag'},
 {regex: new RegExp(this.GetKeywords(builtins), 'gm'), css: 'builtin'},
 {regex: new RegExp(this.GetKeywords(keywords), 'gm'), css: 'keyword'}
 ];

    this.CssClass = 'dp-batch';

    this.Style =    '.dp-batch .builtin {color: maroon; font-weight: bold;}' +
                    '.dp-batch .comment {color: gray;}' +
                    '.dp-batch .delim {font-weight: bold;}' +
                    '.dp-batch .flag {color: green;}' +
                    '.dp-batch .string {color: red;}' +
                    '.dp-batch .vars {color: blue;font-weight: bold;}';

}

dp.sh.Brushes.Batch.prototype = new dp.sh.Highlighter();
dp.sh.Brushes.Batch.Aliases = ['batch', 'dos'];

I'm really quite pleased with the results, and hope you find it useful!

4 comments:

Andrew said...

Excellent!
Thank you putting this together and for posting it ;o)
Andrew, UK

Abel Braaksma said...

I compiled together a list of brushes for Alex's Gorbatchev's SyntaxHighlighter version 2.0. The Google Highlighter you refer to is version 1.5 of the same, but is unmaintained now.

Your code will work fine with v1.5, but you'll need to change it to work with v2.0. On my overview of SH Brushes, you can find another 2.0 version of the BATCH / CMD highlighter. It isn't mine so I can't comment on the quality as compared to yours.

If you can provide comments on how to improve it or if you can update yours to 2.0, I'd be happy to include it on the list.

-- Abel --

Anonymous said...

Any script for JSP pages, by the way; otherwise, this nice tool makes code looks sexy

thanks

lohan+ said...

thanks for posting your script. i added every dos builtin i could find, and fixed it so it would work with 2.0 and not care about the case of the keywords and builtins.

here's a link:
https://sites.google.com/site/lohanplus/syntaxhighlighter/shBrushBatch.js?attredirects=0&d=1