377

Do you think it would be worthwhile to provide hints as to what language to use for the syntax highlighting?

Sometimes I find the highlighting on SQL or VB.NET answers is more distracting than helpful; for example:

to pick the two I've been looking at recently.

4

11 Answers 11

385

Note that this question is a bit obsolete, because we now infer prettify language type based on the tags.

See more:
Changes to syntax highlighting

This is now implemented. In addition to tag inference, you can manually specify the language as a hint to Google Code Prettify.

The spec is:

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

    code goes here

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

    code goes here

You may use either a tag or a prettify language code to specify, though prettify language codes are always guaranteed to work regardless of what language the tag happens to be set to.

Use <!-- language-all: lang-or-tag --> to use the given highlighting for all the following code blocks.

practical examples

For those with little time...

<!-- language: lang-js -->
<!-- language: typescript -->
<!-- language: c# -->

Please note they're all in lower case, even though the language may be uppercase (C#) or mixed case (TypeScript).

Suppress any syntax highlighting (prevents poor, misleading guesses)

<!-- language: lang-none -->

Use language-all and your syntax highlighting choice shall be used for all code blocks that follow:

<!-- language-all: lang-html -->

Available Language Hints Moved

Due to several lists of available hints existing throughout Meta, the entire list has been consolidated and moved to the following FAQ:

What is syntax highlighting and how does it work?

17
  • I think the current solution with the tag intelligence is a big improvement to what it was before. Especially VB code will look much better now. The only downside is non-language specific questions, that might get answers in VB.NET.
    – awe
    Commented Jan 26, 2011 at 12:03
  • @awe indeed it will be formatted as a comment, unless you mix language tags and force default inference again.. looks fine to me, see stackoverflow.com/questions/4801650/… Commented Jan 26, 2011 at 12:19
  • Can we get this documented in the help pages? I've been looking around for this for a while and this is the first place I've found it, so if it is in help pages somewhere it's not visible enough.
    – Old Pro
    Commented May 31, 2012 at 2:10
  • @old you somehow missed it, see meta.stackoverflow.com/editing-help#syntax-highlighting -- same on all Stack Exchange sites Commented May 31, 2012 at 5:17
  • @Jeff, my bad. I was looking for something related to "language" and I was looking on Ask Different where it's near the bottom.
    – Old Pro
    Commented May 31, 2012 at 6:53
  • 4
    Great, thanks. But why do all the magic strings start lang-? Commented Apr 12, 2013 at 21:07
  • 1
    Thank you. FYI of all the solutions described here, only the <!-- language-all: lang-tag --> option worked for me. :/
    – user188035
    Commented May 25, 2013 at 20:39
  • Is there any highlighter for DOS/Win shell scripts?
    – Web Devie
    Commented Jul 30, 2013 at 6:57
  • 6
    I wish we had this functionality built-in into post editor on SO. I always end up googling this and copy/pasting from this answer, which is counter-productive. Commented Sep 12, 2013 at 18:35
  • 6
    It's also far too sensitive - <!--language-all: ... --> doesn't work - no space before language. I end up at this post every single time I need this. Very frustrating that it's not in the editor and/or more forgiving
    – Basic
    Commented Nov 4, 2013 at 9:47
  • 9
    OMG I forget this thing every time... I've been back on this page for 145th time. They should bring some UI for language selection.
    – user158239
    Commented May 19, 2014 at 19:16
  • 1
    I suggest using simpler markup like GitHub's triple backtick - language or similar. I also have to google this every time.
    – Zoltán
    Commented Oct 28, 2014 at 15:41
  • 4
    I've googled this question over 10 times now. This syntax is impossible to remember for some reason. Can't remember if its lang or language, can't remember if it's lang-python language-python lang-py python-lang or just python or some other thing. At least I can remember that its <!-- --> and not <-- --!>
    – user357287
    Commented Jan 29, 2020 at 9:27
  • 2
    Where is a list of all the available language tags? Commented Feb 26, 2020 at 22:00
  • 1
    Google Code Pretty is no longer maintained: github.com/googlearchive/code-prettify/commit/…. Since there are still bugs which won't be fixed anymore, any plans to migrate to a different highlighter? Commented Jun 5, 2020 at 15:16
15

One thing that works now, is to have no highlighting (none being better than wrong) by using <pre> code </pre>.

10
  • 1
    agreed - this is worth pointing out
    – ShuggyCoUk
    Commented Jul 2, 2009 at 18:19
  • 4
    Has the added downside that you have to escape any </> in there.
    – Joey
    Commented Oct 13, 2009 at 16:04
  • 1
    And how does one escape an <?
    – MPelletier
    Commented Feb 21, 2010 at 22:10
  • 3
    @MPelletier: Use the entity "lt" (with the usual "&" prefix and ";" suffix). (Not just writing it, because I'm not sure how comments are escaped.)
    – Richard
    Commented Feb 22, 2010 at 9:48
  • But wouldn't that just show up as "& lt ;" in your code, since pre stands for preformatted?
    – MPelletier
    Commented Mar 20, 2010 at 20:11
  • 1
    OK, so yes, that shows up as <. Then how could one print &lt; for just that?
    – MPelletier
    Commented Mar 21, 2010 at 5:08
  • 1
    @MPelletier: You use the entity amp to create a &.
    – Richard
    Commented Mar 21, 2010 at 9:20
  • 1
    You have to use <PRE><code>code goes here</code></PRE> (yes, uppercase <PRE>) if you want syntax highlighting. You need to escape < to &lt;, > to &gt; and & to &amp;.
    – user153011
    Commented Mar 7, 2011 at 11:28
  • 8
    Note that it appears that <!-- language: none --> is now the preferred mechanism to disable syntax highlighting for a preformatted block of text.
    – Phrogz
    Commented Mar 31, 2011 at 22:32
  • @nyuszika7h Thank you so much for <PRE><code>. I've been looking everywhere for basic highlighting! May be worthy of its own answer because the recommended technique only works in some circumstances. Much obliged.
    – tresf
    Commented Nov 28, 2018 at 21:15
14

There are problems with code-prettify,

this [^\s>/] == "example" # is highlighted as a regex, not a comment, 1 / 2

..but it mostly does a really good job

There's no good solution to this, Markdown does not provide a way to specify what language a code-block is (see here), so StackOverflow would need to implement their own way.

In the past I've modified a markdown parser to look for a special first-line, something like..

{{language:python}}
import os
print os.listdir()

The usability problem then is, there's no way to know which languages are supported, or how I should specify say, Visual Basic .NET - should it be visualbasicnet, visual-basic.net, vb.net vbnet, etc..?

I suppose this could be overcome by making the code-block button ask for a language (or maybe add second specific-language-code button), but then the site isn't using Markdown anymore, it's using StackOverflows own non-standard version..

5
  • 20
    If you were wanting to be really flash, you could see what languages the question was tagged as, and use this as a heuristic. People with sufficient rep could then do any association for new tags, (it may also encourage more consistent tagging) - still wouldn't always get it right for the "I'm trying to convert this perl to vb.net" type of question, mind you... Commented Jul 2, 2009 at 15:45
  • 3
    I like the use the tags idea. it would cover most right
    – ShuggyCoUk
    Commented Jul 2, 2009 at 18:18
  • 2
    I agree that making SO stop being markdown would be bad. that said if you could embded it as something that is legit html...? Even then I prefer Rowland's idea
    – ShuggyCoUk
    Commented Jul 2, 2009 at 18:19
  • @dbr - Not knowing what language could also be solved with documentation. Commented Oct 22, 2010 at 7:48
  • marked down for defeatist attitude.
    – SamB
    Commented Nov 20, 2010 at 23:49
12

Last I know, the site used a whitelist approach to strip the unsupported tags or unsupported attributes on tags.
So I think it would be easy to at least let us provide the class attribute for the pre tag (right now, it doesn't). That way, we can manually set the language for the syntax highlighting like this :

<pre class="prettyprint lang-sql"><code>
--sql comment here
</code></pre>

To answer the question ; Yes, it would definitely be worthwhile to provide hints as to what language to use for the syntax highlighting. I mean take a look at ask.sqlservercentral.com or ask.sqlteam.com, both of them are stackexchange sites focused on SQL Server. But none of the code examples are highlighted correctly.

2
  • Shouldn't the class attribute go on the code tag? I'm assuming that's what you meant.
    – Jon Seigel
    Commented Mar 10, 2010 at 16:31
  • @Jon Seigel, It works both on pre tag and code tag.
    – çağdaş
    Commented Mar 14, 2010 at 0:05
6

A workaround for VB is to add an additional apostrophe ' at the end of every comment line, so that the daft syntax highlighter thinks you've terminated your string.

Dear MarkDown
  'Unterminated 
  Why Is Half My Code Red, It Is Not A String
End Moan

Becomes

Dear MarkDown
  'Terminated '
  This Is How It Should Look
End Moan

I sometimes edit other peoples' posts to add the apostrophes.


EDIT based on Joel's suggestion to use ''# which is even better:

Dear MarkDown
  ''# Terminated 
  This Is How It Should Look
End Moan
4
  • 5
    A better workaround is to use ''# to indicate comments. This avoids need to worry about escaping quotes in the comment itself and actually colors the text like a comment rather than a string literal. Commented May 18, 2010 at 16:15
  • 1
    @JoelCoehoorn — This is ugly. Commented Jun 21, 2014 at 14:28
  • @MarkJ — You degrade other people’s code to make the buggy syntax highlighter work ?? Commented Jun 21, 2014 at 14:30
  • @JoelCoehoorn — Altering the code to circumvent the bugs of the syntax highlighter. I had never seen that. Commented Jun 21, 2014 at 14:31
5

As I've already said in another answer, this could be helped if the language could be inferred from the tags. That way, this fix would work retroactively, and the work flow wouldn't change.

I know that prettify does have a way to specify the language, it would be only a matter of using that.

2
  • Yes, that is also a possibility. However, this would not help for posts containing snippets in several languages (e.g. SQL + PHP, or JavaScript + Perl, or C + Lisp). It also seems to be a much more involved code change than adding to the HTML whitelist.
    – Svante
    Commented Mar 10, 2010 at 21:11
  • 3
    @Svante - it would still help those if the list of possibles is filtered down to those tags. Then for example you might have a php and sql question, but at least it's not gonna try to color the sql like C. Commented May 18, 2010 at 16:17
5

Scavenger's answer is a good idea, but # might be something that is valid in the code like a pre-processor command in C++ that can be part of the actual code sample.

I suggest to have something that is specified before the code block in a special way. The code block is indented with 4 spaces, so the language specifier could be indented 3 spaces (and only mean something when appearing immediately before a code block). This follows the simplicity in the markup rules that already exists.

   VB.NET
     Dim myVariable As String 'Here is the first code line
     myVariable = "Hello world"

5

Adding in an option for very light highlighting that would essentially do little more than colour brackets and isolated literals, treat '' and "" as text delimiters and nothing else would be useful for a vast number of languages currently unsupported.

Functional example code look like crap with the current highlighter as do punctuation-light languages like SQL. Perl is just hilarious.

Actually thinking about it more you wouldn't even want the text stuff. It is too costly a hit to readability when it fails (thinking generic type parameters in F# for example).

Essentially nothing that requires unbounded state and never goes across a new line should function very well. Something that is unbounded but the bounds are highly common like whitespace, new lines, any non-word character, etc. have a high probability of not messing up many languages.

1
  • 1
    Was going to point out that in some languages ' is not a string delimiter, and -- isn't always "decrement". Can't say I've seen many Perl questions on SO (Guess everyone still uses perlmonks), but I'd fear how it would handle regexp delimiters... Commented Jun 30, 2009 at 12:38
3

The cleanest syntax I think would be something like this:

# Python:

import this
this.eat()

That is, a language-specific comment mark, space, language identifier on the first line. Of course, "Py", "Python 2.6" and misspellings like "Pyton" should work as well.

4
  • Not sure how this would work with inline code samples? Commented Mar 31, 2010 at 7:40
  • 3
    It wouldn't. Shouldn't inline code should be short enough to grasp without coloring anyway? Commented Apr 20, 2010 at 8:04
  • 2
    As it is, inline code samples are not highlighted anyway (just styled with monospace font and grey background...)
    – awe
    Commented Sep 21, 2010 at 8:24
  • 6
    I am of the firm belief that misspellings should not work.
    – Svante
    Commented Sep 29, 2010 at 14:24
3

Another workaround for VB comments is to include two apostrophes and a pound symbol at the beginning of the line. That way, comments aren't highlighted as text and they stand out better.

''# You're commenting!
Dim c As New Comment()

see this Meta question.

1

Has anyone seen how terribly this general syntax works for MATLAB? Look at the last piece of code in this question. MATLAB uses apostrophe (') as a matrix transposition operator. As a result, everything in code following it is highlighted as a string in brown.

Comments in MATLAB start with a % and those never get highlighted either. Perhaps we could at least make separate HTML tags for certain languages like <asm></asm> or <matlab></matlab>. This would be very helpful, because posting a non-ugly MATLAB question or answer takes ages of reformatting your original code. I personally volunteer to write the code to implement this, just tell me what to do.

1

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