The mathematica.SE is currently in private beta and will open to the public in a few days. Stack Overflow and related sites use prettify.js, 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.
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.
Basic requirements
Comments
These are entered as(* comment *)
. So anything between these should be highlighted in gray.Strings
These are entered as"string"
(single quotes are not supported), and should be highlighted in pink.Operators/short hand notations
Apart from the standard+, -, *, /, ^, ==
, etc., Mathematica has several other operators and short hand notations. The most commonly encountered ones are:@, @@, @@@, /@, //@, //, ~, /., //., ->, :>, /:, /;, :=, :^=, =., &, |, ||, &&, _, __, ___, ;;, [[, ]], <<, >>, ~~, <>
These, and parenthesis, brackets and braces should all be highlighted in black.
Patterns objects and slots
Pattern objects start with a letter and have either_
or__
or___
attached, like for example,x_
,x__
andx___
. These can also have additional letters following the underscore, asx_abc
, etc. All of these should be highlighted in green.Slots are
#
and##
and can also be followed by an integer as#1
,##4
, etc., and should also be in green.Both of these (pattern objects and slots) are usually terminated by an operator/bracket/shortform from point 3 above.
Functions/variables
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`
in code and should be considered part of the function/variable name. E.g.,abcd`defg
. Dollar signs$
anywhere in a variable name is to be treated just like a letter (i.e., nothing special).
For all of the above, if they appear inside strings, they should be treated as such, i.e. "@~#
should be highlighted in pink.
Additional nice to haves:
- In the pattern objects in point 3 above, if the underscore(s) is followed by a
?
and then some letters, then the part following the_
should be in black. E.g., inx__?abc
, thex__
part must be in green and the?abc
in black. - 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.
Screenshots & code samples:
1. Simple examples
Here's a small example set, with a screenshot at the end showing how it looks in Mathematica:
(*simple pattern objects & operators*)
f[x_, y__] := x Times @@ y
(*pattern objects with chars at the end and strings*)
f[x_String] := x <> "hello@world"
(*pattern objects with ?xxx at the end*)
f[x_?MatrixQ] := x + Transpose@x
<< 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 @@@ # &
2. A real world example
Here's an example from this answer of mine that also indicates my point 2 in the "Additional nice to haves" section, i.e., lowercase stuff being highlighted in blue.
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.
prob = MapIndexed[#1/#2 &,
Accumulate[
EuclideanDistance[{0, 0}, #] < 1 & /@ 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 -> 200], {i, Range[2, 20000, 1]},
ControlType -> Manipulator, SaveDefinitions -> True]
Is this feasible? Too much? Too hard? Impossible?
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.
In recognition of this help, you all have the Mathematica community's eternal gratitude and in addition, I'll award a 500 bounty to each person that contributes significantly to this (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). Implementing the "Additional nice to haves" gets an automatic +500 regardless of previous bounties, 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.
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.