10

Here on mathematica.stackexchange we will most of the time have to deal with Mathematica code, but it clearly happens too, that someone posts Java-code for JLink or C-code for MathLink, etc. just look at $InstallationDirectory/SystemFiles/Links to get an idea how many language bindings we have.

Google-code-prettify which is used for highlighting can deal with many languages, but I don't know how good the automatic code recognition works. I checked prettify.js and don't think the automatic detection can handle custom language plugins. Our Mathematica-extension is a custom made language-extension to google-prettify, which means we will have to tell at html-basis, that code posted here is lang-mma. When the language is fixed, meaning when you make a code-block here it is replace by the editor with something like

<pre class="prettyprint lang-mma" id="mma">
(* Your code here *)
</pre>

every code-block will be highlighted as Mathematica. This seems to be pretty much the same behavior which all SO sites have. You can inspect this by using Chrome and browsing a randomly chosen Java or C++ question, right click on the gray area inside a code block and chose Inspect Element. You see that the html-tags are always language specific. If you now draft an answer in the C++ question and post Java-code (use final or instanceOf to make it crystal clear) and inspect the element in the preview, you see it's still C++. So no language detection.

Question: Would it be possible and feasible to highlight code on mathematica.SE always as Mathematica-code, but to have the opportunity to give a hint to the code-environment if the language is something else?

A quick search on meta.SO brought these two related threads to daylight:

There are several more questions about this, but they are tagged prettify which suggests, that they talk about the development of prettify and not the inclusion in SO, am I right?

12

I think you are looking for this answer.

On StackOverflow at least one can specify the language of a code block like this:

<!-- language: lang-or-tag-here -->

    code goes here

<!-- language: lang-c -->

    int main() {

        return 0;    
    }

This works fine on Mathematica.SE currently:

Mathematica graphics

  • +1 Really nice. That was what I hoped for. – halirutan Feb 7 '12 at 19:32
  • BTW, is there a way to reduce the redundancy here? After "language:" it is clear that there comes a language name, there's absolutely no point in the "lang-" prefix following it. – celtschk May 27 '12 at 9:40
  • 1
    @celtschk What comes after language is the file-name of the JavaScript which used for highlighting. So it only looks like a redundancy ;-) – halirutan Dec 14 '12 at 5:24

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .