title: Can prettify.js be extended to support Mathematica?
body: <p>The <a href="http://mathematica.stackexchange.com">mathematica.SE</a> is currently in private beta and will open to the public in a few days. Stack Overflow and related sites use <a href="http://code.google.com/p/google-code-prettify/" rel="nofollow noreferrer">prettify.js</a>, however Mathematica is not a supported language. It would be pretty awesome to have a custom highlighting script for our site, and I request the JavaScript and CSS community's help in developing a such a script and the accompanying CSS.</p>
  
  <p>I've listed below a few basic requirements such that it captures most of the features of Mathematica's default highlighting scheme (ignoring stuff that only the internal parser would know). I've also named the colours generically – hexadecimal colour codes can be picked from the screenshots I've provided (further below). I've also added code samples to accompany the screenshots so that folks can test it out.</p>
  
  <h2>Basic requirements</h2>
  
  <ol>
  <li><p><strong>Comments</strong><br>
  These are entered as <code>(* comment *)</code>. So anything between these should be highlighted in gray.</p></li>
  <li><p><strong>Strings</strong><br>
  These are entered as <code>"string"</code>(single quotes are not supported), and should be highlighted in pink.</p></li>
  <li><p><strong>Operators/short hand notations</strong><br>
  Apart from the standard <code>+, -, *, /, ^, ==</code>, etc., Mathematica has several other operators and short hand notations. The most commonly encountered ones are:</p>
  
  <pre><code>@, @@, @@@, /@, //@, //, ~, /., //., -&gt;, :&gt;, /:, /;, :=, :^=, =., 
  &amp;, |, ||, &amp;&amp;, _, __, ___, ;;, [[, ]], &lt;&lt;, &gt;&gt;, ~~, &lt;&gt;
  </code></pre>
  
  <p>These, and parenthesis, brackets and braces should all be highlighted in black. </p></li>
  <li><p><strong>Patterns objects and slots</strong><br>
  Pattern objects start with a letter and have either <code>_</code> or <code>__</code> or <code>___</code> attached, like for example, <code>x_</code>, <code>x__</code> and <code>x___</code>. These can also have additional letters following the underscore, as <code>x_abc</code>, etc. All of these should be highlighted in green. </p>
  
  <p>Slots are <code>#</code> and <code>##</code> and can also be followed by an integer as <code>#1</code>, <code>##4</code>, etc., and should also be in green.</p>
  
  <p>Both of these (pattern objects and slots) are usually terminated by an operator/bracket/shortform from point 3 above.</p></li>
  <li><p><strong>Functions/variables</strong><br>
  Functions and variables is a rather loose terminology here, but serves for the purposes of this post. Anything not falling in the above 4 can be highlighted in black. Mathematica often uses backticks <code>`</code> in code and should be considered part of the function/variable name. E.g., <code>abcd`defg</code>. Dollar signs <code>$</code> anywhere in a variable name is to be treated just like a letter (i.e., nothing special).</p></li>
  </ol>
  
  <p>For all of the above, if they appear inside strings, they should be treated as such, i.e. <code>"@~#</code> should be highlighted in pink.</p>
  
  <h2>Additional nice to haves:</h2>
  
  <ol>
  <li>In the pattern objects in point 3 above, if the underscore(s) is followed by a <code>?</code> and then some letters, then the part following the <code>_</code> should be in black. E.g., in <code>x__?abc</code>, the <code>x__</code> part must be in green and the <code>?abc</code> in black. </li>
  <li>if a function/variable starts with a capital letter, then it is highlighted in black. If it starts with a small letter, it is highlighted in blue. Internally, this differentiates built-in functions vs. user defined functions. However, the mathematica community (pretty much everywhere) sticks to this naming convention fairly well, so distinguishing the two would serve some purpose.</li>
  </ol>
  
  <h2>Screenshots &amp; code samples:</h2>
  
  <h3>1. Simple examples</h3>
  
  <p>Here's a small example set, with a screenshot at the end showing how it looks in Mathematica:</p>
  
  <pre><code>(*simple pattern objects &amp; operators*)
  f[x_, y__] := x Times @@ y  
  
  (*pattern objects with chars at the end and strings*)
  
  f[x_String] := x &lt;&gt; "hello@world" 
  
  (*pattern objects with ?xxx at the end*)
  
  f[x_?MatrixQ] := x + Transpose@x
  
  &lt;&lt; Combinatorica` (*example with backticks and inline comment*)
  
  (*Slightly more complicated example with a mix of stuff*)
  
  Developer`PartitionMap[Total, Range@1000, 3][[3 ;; -3]]~Partition~2 //
    Times @@@ # &amp;
  </code></pre>
  
  <p><img src="https://i.stack.imgur.com/q90D6.png" alt="enter image description here"></p>
  
  <h3>2. A real world example</h3>
  
  <p>Here's an example from <a href="https://stackoverflow.com/a/8578448/616736">this answer of mine</a> that also indicates my point 2 in the "Additional nice to haves" section, i.e., lowercase stuff being highlighted in blue. </p>
  
  <p>Also, you might notice some of the variables highlighted in orange – I purposefully didn't include that as a requirement, as I think that's going to be a lot harder to do without a parser that knows Mathematica.</p>
  
  <pre><code>prob = MapIndexed[#1/#2 &amp;, 
      Accumulate[
       EuclideanDistance[{0, 0}, #] &lt; 1 &amp; /@ arrows // Boole]]~N~4;
  
  Manipulate[
   Graphics[{White, Rectangle[{-5, -5}, {5, 5}], Red, Disk[{0, 0}, 1], 
     Black, Point[arrows[[;; i]]], 
     Text[Style[First@prob[[i]], Bold, 18, "Helvetica"], {-4.5, 4.5}]}, 
    ImageSize -&gt; 200], {i, Range[2, 20000, 1]}, 
   ControlType -&gt; Manipulator, SaveDefinitions -&gt; True]
  </code></pre>
  
  <p><img src="https://i.stack.imgur.com/2uiRn.png" alt="enter image description here"></p>
  
  <h2>Is this feasible? Too much? Too hard? Impossible?</h2>
  
  <p>Quite frankly, I don't know the answer to any of those. I just listed some basic features that everyone on mathematica.SE would love to have and some additional stuff that would be a cherry on the top. However, do let me know if these are too difficult to implement. We can work out a smaller subset of features. </p>
  
  <p>In recognition of this help, you all have the Mathematica community's eternal gratitude and in addition, <strong>I'll award a 500 bounty to each person that contributes significantly to this</strong> (if it's done in parts by different folks) – I'll rely on your votes/comments/output on the answers to decide what's significant (perhaps more than one bounty to one person if they do all the work). <strong>Implementing the "Additional nice to haves" gets an automatic +500 regardless of previous bounties</strong>, so you can also build upon the work of others even if you don't do the first half. I might also periodically place smaller bounties to attract users who might not have seen this question, so if you happen to earn those bounties, they'll be in addition to the "bounty to reward an existing answer" which will be decided towards the end. </p>
  
  <p>Lastly, I'm not in a hurry. So please take your time with this question. The bounty is always an option until it is implemented by SE (or if it has been determined that existing answers satisfy the requirements completely). Ideally, I'm hoping to get this implemented 2/3rs of our way into the beta, which is 2 months from now.</p>