Saturday, October 03, 2009

SyntaxHighlighter 2.0 Brushes for F# and Erlang

Updating the Erlang brush

// WTFPL licensed
SyntaxHighlighter.brushes.Erlang = function() {
var keywords = 'after begin case catch cond end fun if let of query receive when '+
'define record export import include include_lib ifdef ifndef else endif undef '+
'apply attribute call do in letrec module primop try';
this.values = new Object();
this.values.regex = new RegExp('[^\\w"\'](_?[A-Z]\\w*)', 'g');
this.values.exec = function(value) { m = this.regex.exec(value); if (m && m.length) {m[0] = m[0].substring(1, m[0].length); ++m.index;} return m;}
this.regexList = [
{ regex: new RegExp('%.*$', 'gm'), css: 'comments' }, // one line comments
{ regex: SyntaxHighlighter.regexLib.doubleQuotedString, css: 'string' }, // strings
{ regex: SyntaxHighlighter.regexLib.singleQuotedString, css: 'plain' }, // strings
{ regex: new RegExp(this.getKeywords(keywords), 'gm'), css: 'keyword' }, // Erlang keyword
{ regex: new RegExp('\\?\\w*', 'gm'), css: 'erlmacros'},
{ regex: this.values, css: 'erlvalues'},
{ regex: new RegExp('\\w+/\\d+', 'gm'), css: 'functions'}
];
}
SyntaxHighlighter.brushes.Erlang.prototype = new SyntaxHighlighter.Highlighter();
SyntaxHighlighter.brushes.Erlang.aliases = ['erlang', 'erl'];
view raw gistfile1.js hosted with ❤ by GitHub

This also required the following in the CSS theme

.syntaxhighlighter .erlvalues,
.syntaxhighlighter .erlvalues a
{ color: #c88; font-weight: bold;}
.syntaxhighlighter .erlmacros,
.syntaxhighlighter .erlmacros a
{ color: gray; font-weight: bold;}
view raw gistfile1.css hosted with ❤ by GitHub

And for F#

// WTFPL licensed
// scratching the surface of research microsoft com/fsharp/manual/spec2.aspx#_Toc207785562
// omits reserved-ident-formats, reserved-symbolic-sequence, quote-op-*, symbolic-op, ...
SyntaxHighlighter.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: SyntaxHighlighter.regexLib.singleLineCComments, css: 'comments' }, // one line comments
{ regex: new RegExp('\\(\\*[\\s\\S]*?\\*\\)', 'gm'), css: 'comments' }, // multiline comments
{ regex: SyntaxHighlighter.regexLib.doubleQuotedString, css: 'string' }, // strings
{ regex: new RegExp('^\\s*#.*', 'gm'), css: 'preprocessor' }, // preprocessor tags like #light
{ regex: new RegExp(this.getKeywords(keywords), 'gm'), css: 'keyword' }, // f# keyword
//{ regex: new RegExp(this.getKeywords(ocaml), 'gm'), css: 'color1' }, // caml keyword
//{ regex: new RegExp(this.getKeywords(reserved), 'gm'), css: 'color2' }, // reserved keyword
//{ regex: new RegExp(this.getKeywords(symbolic), 'gm'), css: 'color3' } // symbolic keyword
];
}
SyntaxHighlighter.brushes.FSharp.prototype = new SyntaxHighlighter.Highlighter();
SyntaxHighlighter.brushes.FSharp.aliases = ['f#', 'f-sharp', 'fsharp'];
view raw gistfile1.js hosted with ❤ by GitHub

which is as incomplete as it always was; but doesn't use any extra CSS.


Later -- another brush for F# that was published at about the same time can be found at Pense-Moi.

1 comment :

Abel Braaksma said...

I've added both brushes to the overview of overview of SH 2.0 brushes. Thanks for the update and great to have a genuine F# brush on the list now :)