Syntax Highlight Brush for F#
Following on from Scott Hanselman's post about the SyntaxHighlighter script, here's the secret sauce I needed to get it to work.
And here's a first stab at an F# brush, which is in use for this post.
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
// Offered under the WTFPL -- http://sam.zoy.org/wtfpl/ | |
// scratching the surface of http://research.microsoft.com/fsharp/manual/spec2.aspx#_Toc207785562 | |
// omits reserved-ident-formats, reserved-symbolic-sequence, quote-op-*, symbolic-op, ... | |
dp.sh.Brushes.FSharp = function() | |
{ | |
var keywords = 'abstract and as assert base begin class default delegate do done ' + | |
'downcast downto elif else end exception extern false finally for '+ | |
'fun function if in inherit inline interface internal lazy let ' + | |
'match member module mutable namespace new null of open or '+ | |
'override private public rec return static struct then to '+ | |
'true try type upcast use val void when while with yield'; | |
var ocaml = 'asr land lor lsl lsr lxor mod sig'; | |
var reserved ='atomic break checked component const constraint constructor '+ | |
'continue eager event external fixed functor global include '+ | |
'method mixin object parallel process protected pure '+ | |
'sealed tailcall trait virtual volatile'; | |
var symbolic = 'let! use! do! yield! return! \\| -> <- \\. : \\( \\) \\[ \\] \\[< >\\] \\[\\| \\|\\] '+ | |
'\\{ \\} \' # :\\?> :\\? ; ;; :> := _ \\.\\. ::'; | |
this.regexList = [ | |
{ regex: dp.sh.RegexLib.SingleLineCComments, css: 'comment' }, | |
{ regex: new RegExp('\\(\\*[\\s\\S]*?\\*\\)', 'gm'), css: 'comment' }, | |
{ regex: dp.sh.RegexLib.DoubleQuotedString, css: 'string' }, // strings | |
{ regex: new RegExp('^\\s*#.*', 'gm'), css: 'preprocessor' }, // preprocessor tags like #light | |
{ regex: new RegExp(this.GetKeywords(keywords), 'g'), css: 'keyword' }, // f# keyword | |
{ regex: new RegExp(this.GetKeywords(ocaml), 'g'), css: 'ocaml' }, // caml keyword | |
{ regex: new RegExp(this.GetKeywords(reserved), 'g'), css: 'reserved' }, // reserved keyword | |
{ regex: new RegExp(this.GetKeywords(symbolic), 'g'), css: 'symbolic' } // symbolic keyword | |
]; | |
this.CssClass = 'dp-fs'; | |
this.Style = '.dp-fs .ocaml { color: #d00; } ' + | |
'.dp-fs .reserved { color: #d00; font-style: italic;} '+ | |
'.dp-fs .symbolic { color: #00d; font-weight: bold;} '; | |
} | |
dp.sh.Brushes.FSharp.prototype = new dp.sh.Highlighter(); | |
dp.sh.Brushes.FSharp.Aliases = ['f#', 'f-sharp', 'fsharp']; |
The symbolic class is not yet working; ocaml and reserved do work, and there's more to add, as noted in the introductory comment, if I can get past the symbolic hurdle.
2 comments :
It is nice to see an F# highlighter around, great work!
I'd love to add your brush to my long list of SH brushes but it needs to be 2.0 compliant for that and the brush syntax has slightly changed. Is is possible for you to update your brush?
The new syntax is explained in this handy custom development guide. The new syntaxhighlighter has since moved to its own location where you can find version 2.0. The old location that you reference in your post will not be updated any further.
-- Abel --
Moving to 2.0 is one of the many things on my "must get around to it" list. As life permits, then it'll happen.
Post a Comment