WolframLanguage-Google-Prettify

Wolfram Language support for the Google Code Prettify

This is the official code highlighter for Mathematica.stackexchange.com and the Wolfram Community. It provides the syntax specification for the Wolfram Language required to highlight code with the Google code prettifier. Here is a highlighted example from StackExchange:

Sample Image

Usage

To use it with your installation of google-code-prettify, you have to add the minified JavaScript language definition lang-mma.min.js from the JSHighlighter directory to your code-prettify installation. Another way is to load the Wolfram Language highlighter after code-prettify in your HTML code. The JSHighlighter directory contains an index.html that shows how it works. Further information can be found in the google-code-prettify repository.

Building and Development

The Wolfram Language highlighting script is created from the JSHighlighter/lang-mma-TEMPLATE.js template by inserting regexes for Mathematica built-in symbols and named characters. This can be done entirely from within Mathematica by using the provided package:

PacletDirectoryAdd["/YourPath/WolframLanguage-Google-Prettify/WLSource/SymbolInformation"];
<< SymbolInformation`

buildJavaScriptTrieRegex[]
(* or *)
buildJavaScriptAlternativeRegex[]

It’s vital to leave the SymbolInformation package in the repository directory (aka don’t install it) because it relies on the relative path to find the correct JSHighlighter directory. Alternatively, you can provide the path to lang-mma-TEMPLATE.js as an argument to the two functions. The difference between the two building methods is

It’s up to you, which version you want to use and I have set up two performance test-pages which display all 7000 Mathematica symbols and 7000 dictionary words (as negative examples) to profile the matching speed. The vast majority of the time is not spent in the matching though and if my not entirely mistaken with my non-existent JavaScript profiling knowledge, only 4.7ms of 1483ms is spent in the actual regex:

Profiling

Surprisingly, the (:?Names|NamespaceBox|NamespaceBoxOptions) alternative pattern is 1ms faster. This difference is not noticeable and since the lang-mma.min.js Trie version (88kB) is much smaller than the alternative pattern lang-mma-alternative-regex.min.js (127kB), I would suggest to use the Trie version. However, if readability counts because you want to check easily if a symbol is included in the regex, then the alternative pattern version is to be preferred. You can perform your own performance tests by using

Repository Structure

The SymbolInformation package exposes some additional functions. You can e.g. create a Trie regex from a list of words:

l = Names["Plot*"];
createTrieRegex[l]

(* "Plot(?:3(?:D|Matrix)|Division|Joined|L(?:a(?:bel(?:s)?|yout)|egends)|Markers|Points|R(?:ange(?:Clip(?:PlanesStyle|ping)|Padding)?|egion)|Style|Theme)?" *)