463

What is syntax highlighting? Syntax highlighting allows code in posts to be highlighted based on the language it's written in, to make it easier to read. How does it work? Stack Exchange does not have its own syntax highlighting engine. It uses Google Code Prettify, and is not necessarily using the latest release of that library. Therefore, any bugs and ...


34

Back in May 2012, I started a project hosted on GitHub that implements MATLAB syntax highlighting for google-code-prettify (released under the MIT License). See here for the initial announcement, plus there was some discussion in the MATLAB chat room. It comes in two flavors, one that detects and stylize all core MATLAB functions (plus functions from select ...


30

The results of my testing were disappointing - we will not go ahead with highlight.js as a syntax highlighter for Stack Overflow and our other sites. It is not too big It is - I generated a custom set of languages that mirrors the exact set we currently support with prettify. Uncompressed it is 57kb, compression takes it down to 22kb - compared with the ...


21

What I think SO needs is: A button next to the button on the editor toolbar. Clicking the button would insert: <!-- language: x --> The caret would be automatically placed where the x is above, and a JS-driven Intellisense-style dropdown list would populate with some choices, something like this: Typing lang-c would present: My 2¢.


19

I have now dragged in the lang-r stuff and set it against the r tag; there may be some small delay while the cache expires etc, but you should be able to explicitly specify it via: <!-- language: lang-r --> Once the cache has cleared (i.e. when it picks it up implicitly from r) you should also be able to specify it (when there is ambiguity) via: <...


17

You just needed a blank line after the <!-- language: lang-py -->. :)


16

And why would Highlight.js be better than Google Code Prettify, considering that you admit yourself that you just heard of Highlight.js and therefore are probably unaware of its advantages/limitations/etc.? You mention something about it looking "promising", but what does that mean? Moreover, why do we need something that only looks promising when we have ...


16

The default pascal highlighter coming from the prettify project is bogus. Based on it, I've made a new Delphi Google prettify highlighter that, not being perfect, address the following issues: The backslash is not treated as an escape character in strings, so for example S := 'C:\' + Path now correctly ends the string. The three flavors of comments are ...


16

No, there isn't, partially since "pseudocode" is ill defined and can be of many different formats. There are no plans to include anything that cannot be properly defined.


16

The highlighting hint only works when it's placed immediately before the code block. When the code block is inside a list or blockquote, the hint also needs to be inside that list or blockquote. However, it looks like our system doesn't properly parse the hint if it's the very first thing in the blockquote. So this will parse out the hint and incorrectly ...


15

You will need [one blank line] before [four blanks], like this: <!-- language: lang-cs --> [four blanks]using System.Windows.Controls; [more indented code]


14

That's a great idea; I've added it to the Markdown help page (will be visible after the next deploy).


14

As announced in 2010 and specified on the Markdown Help Page, you have to either to tag with php or use one of the prettify language codes (<!-- language: lang-php -->) before the code. E.g. <!-- language: php --> // This is some PHP phpinfo(); https://github.com/google/code-prettify


14

Is there syntax highlighting for Java stack traces? Yes and no. You can add colour to your stack trace via the features of Google Prettify's standard Java syntax highlighting, which is what Stack Exchange uses. You'll get class and package colour added to your trace but no specific functionality that handles to traces and formats them for reading. An ...


13

Matlab syntax support is rolling in. This is basic syntax support without keywords, as these add a lot to the size of the highlighter and do not gzip well at all. Still - comments should be comments and basic highlighting should work as expected.


13

Currently only moderators can change it. It is read-only text to everyone else, present solely so users can figure out which highlighter is being used for a given tag. There is no mechanism in place whatsoever for "suggesting" it be changed outside of simply creating a new support request on Meta requesting it be changed for a given tag. There is no ...


12

Syntax highlighting is dependent on the language. By default that is chosen from the tags that are on the question. However, the question doesn't have any languages tagged. So there's no syntax highlighting. I've edited your post to manually enable C++ syntax highlighting by adding this: <!-- language: lang-cpp --> More Information: Syntax ...


12

This is by design. The backticks are for inline code, usually a single "word". You use it for stuff like root and abstract and enum and live(), etc. It will not appear on its own line unless you force it to. A whole line of code is better put in the block codeblock--it gets its own line and is syntax highlighted as well.


11

The language comment shouldn't be indented and you should put a blank line between the comment and the code <!-- language: lang-sql --> Select * from Foo where Foo.bar == "baz" This should yield Select * from Foo where Foo.bar == "baz" The highlight is pretty subtle though.


11

There's no syntax highlighting because the question is lacking relevant tags. Tags are used as default syntax highlighting hints. For example, if the question is tagged javascript, then the code will be treated as if it were JavaScript for highlighting purposes. This is the case with the question for which your answer is correctly highlighted, but is not ...


11

Apparently not; I've added the python tag to the question and the code highlighting is now working. I've also removed the python-2.7 tag from that question though, it is not the right tag for the question. For future reference, you can also mark your code blocks with an explicit code hint: <!-- language: lang-py --> See How do I format my code ...


10

Do you mean highlighting like this? If so, use the backtick - enclose the desired words, thus `word` becomes word.


10

For your convenience, here's a quick and dirty userscript that changes the code comments color to green (only on Stack Overflow): Green Comments it does nothing more than simply adding this global style: code span.com { color: green; }


10

The syntax highlighter uses the default one-size-fits-all Java version, since the question is tagged java; it doesn't know that this particular snippet is XML. If you specify the language by adding <!-- language: lang-xml --> before the code block, it works fine: <property name="filterInvocationDefinitionSource"> <value> ...


10

You can't explicitly specify multiple languages for highlighting in a single code block. However, if you use lang-html, prettify is smart enough to detect JavaScript in <script> tags and CSS in <style> tags, and highlight them accordingly: <div id="fb-root"></div> <script> var facebookLang = "en_US"; (function (d, s, ...


10

The syntax is <!-- language: lang-css -->. Note the language: part there, see How do I format my code blocks?. I've fixed that for you. Note that normally your suggested edit stood the risk of being rejected for being too minor. Minor, quick syntax-formatting fix-only edits put an unnecessary strain on the suggested edits system, as 2 additional ...


10

The answer to this is pretty simple. There is no syntax highlighting in the comments. At most you can wrap your code in the usual back-ticks to give it somewhat of a different appearance, but that's about it. You should not be posting substantial amounts of code in the comments anyway. They are not meant for that. Nor are they meant for extensive discussion ...


10

Officially, it would be lang-m (it's based on the file extension), but the prettifier then uses the same handler as used for C and C++: registerLangHandler(sourceDecorator({ 'keywords': CPP_KEYWORDS, 'hashComments': true, 'cStyleComments': true, 'types': C_TYPES }), ['c', 'cc', 'cpp', 'cxx', 'cyc', 'm']); so you may as ...


10

There are many programming languages out there. It would be hard to keep track of all to know that a question contains a language tag or not. And not all questions need a language tag. If you asked about deployment or need help on a regular expression. If a question misses a language tag you can ask the OP for it. (I do it all the time in the SQL area ...


Only top voted, non community-wiki answers of a minimum length are eligible