515

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 ...


34

Yep, these changes are intentional. We had to move some variables around to clean up our CSS. These were matched with the closest re-usable color values we have in our design system. ๐Ÿ‘‹


31

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ยข.


20

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: <...


18

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 ...


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.


14

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.


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

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

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 ...


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

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 ...


11

Which Stack Exchange sites have syntax highlighting enabled? The syntax highlighting feature is enabled on the following sites: Arduino Arduino Meta Ask Different Ask Ubuntu Bioinformatics Bioinformatics Meta Blender CS50 CS50 Meta Chemistry Chemistry Meta Code Golf Code Golf Meta Code Review Computational Science Computer Graphics Computer Graphics Meta ...


11

Kotlin highlighting is now live!


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 ...


10

The PR for adding Elixir support in code-prettify has just been merged. Stack Overflow should be able to build a new version of their prettify-full.js with Elixir support. Does anyone know what would be the process for requesting that?


10

I've just submitted a suggested edit using the old-style code blocks and a language hint: <!-- language: lang-r --> That seems to work: Your last version (revision 3) might not work since you used a capital R; language hints need to be in lower case.


9

I the correct method of specifying language highlighting is : <!-- language: lang-c# --> You were simply missing the lang- in the definition. Check out the formatting sandbox to learn all the methods/tricks with regard to formatting. Hit edit on any post that catches your eye and see how they did it.


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