58
votes

locked for the history - please post bugs and feature-requests separately.

-Shog9

We're changing the way syntax highlighting is done on the Stack Exchange engine.

As you probably know, we use Google Code Prettify for automatic syntax highlighting.

Since the beginning, we've had kind of a boolean setting per website:

  1. code blocks are always automatically highlighted (Stack Overflow, programmers.se, etc)
  2. code blocks are never highlighted (Super User, photo.se, bicycles.se, etc)

We are now moving to a more tag-based syntax highlighting method. There are one of three possible syntax highlighting hints associated with each tag:

  1. specific language hint -- tell prettify to use that language
  2. default language hint -- let prettify infer a language
  3. null -- no hint, no syntax highlighting

(all specific languages must be in the set that prettify supports, obviously)

bsh, c, cc, cpp, cs, csh, cyc, cv, htm, html, java, js, m, mxml, perl, pl, pm, py, rb, sh, xhtml, xml, xsl.

extensions available are

apollo, css, go, hs, lisp, lua, ml, proto, scala, sql, vb, vhdl, wiki, yaml

Thus:

  • If a question has no tags with a default or specific language hint, no syntax highlighting will be performed.

  • If a question has at most one tag with a specific language hint, plus any number of default or null hints, then syntax highlighting will be performed only for that specific language.

  • If a question has two tags that both define specific languages, it uses default and lets prettify infer as it always has.

  • As long as a question has one or more tags with default language hints, but none with a specific language hint, it uses default and let prettify infer as it always has.

The default tag syntax highlighting hint is null. So the question is, which tags should have the default and specific language hints? I put my initial pass through the first 6 pages of tags here:

http://pastebin.com/AWMtu5rK

If you feel there should be changes, post your diffs as answers.

(and yes, there are plans to allow an explicit override syntax but for now we want to try to get the defaults mostly correct before worrying about the edge conditions)

5
  • Note that two language tags with competing highlighted must be treated as equivalent to none. Please tell me that c highlighting and c++ highlighting are different. I'd love another reason to convince the language ambivalent masses not to conflate the two. Jan 30, 2011 at 20:55
  • @dmckee "If a question has two tags that both define specific languages, it uses default and lets prettify infer as it always has." Jan 31, 2011 at 3:21
  • they are a way to add language d for highlighting? Aug 16, 2011 at 13:11
  • @bio only if it is supported by Google Prettify Aug 16, 2011 at 13:39
  • is objective-c on there or are we supposed to use c/c++ highlighting? Dec 20, 2011 at 15:37

23 Answers 23

14
votes

Maybe this sort of thing could be attached to the tagwiki system?

1
  • 3
    diamond moderators can now change syntax highlighting on a per-tag per-site basis. We're also looking at an in-source override syntax soon. Feb 21, 2011 at 22:27
13
votes

One potential change: when an answer is posted, remember the default language at the time.

It would be really nasty to post an answer which was formatted appropriately, only to have someone remove a tag and turn the answer into badly-formatted (or even unformatted?) mush.

EDIT: Of course, edits make this even more complicated... should an edit take on the new default or not?

6
  • 1
    I think we will take a "wait and see what happens in practice" attitude here. However, I do suspect that some of the syntax highlighting tag rules will result in better tagging, which is a net good for the community. Dec 13, 2010 at 7:39
  • 1
    @Jeff: Wait and see sounds good to me... but I suspect it may also result in worse tagging, in that it may encourage the practice of tagging a question with the language the code is given in, even if the question has nothing to do with that language per se. For example, a question on how StringBuilder works shouldn't (IMO) really be tagged "c#" unless it's asking about the C# language itself. It's really a .NET question, just phrased using C#. Anyway, enough speculation....
    – Jon Skeet
    Dec 13, 2010 at 7:48
  • but .net applies the "default" syntax highlighting per the rules.. pastebin.com/AWMtu5rK Dec 13, 2010 at 7:51
  • @Jeff: Yes, indeed... but my point is that people may start tagging questions with the language by default. I guess VB would have been a better example here...
    – Jon Skeet
    Dec 13, 2010 at 7:53
  • To support @Jon's point: that's exactly what I just did (meta.stackexchange.com/questions/101307/…), even though I know that the question is not about C# per se, but it was the easiest solution.
    – M4N
    Aug 8, 2011 at 9:53
  • On the other hand, there are so many questions tagged C# which are not about the language, or questions tagged ASP.NET which really are about web forms and not the ASP.NET framework...
    – M4N
    Aug 8, 2011 at 9:54
6
votes

(Separate from my "general change" as they're different proposals.)

[iphone] = default
[c#-3.0] = default
[android] = default
[jquery] = default
[hibernate] = default

should be (IMO):

[iphone] = lang-c // Assume Objective-C for the most part
[c#-3.0] = lang-cs
[android] = lang-java
[jquery] = lang-js
[hibernate] = lang-java

(There may be more, but I need to go to work...)

3
  • 1
    this would be risky on "web soup" languages which tend to contain HTML, CSS, JavaScript, and XML pretty regularly -- if a language is specified it forces that language and will not detect any others. Generally we should only specify the language hint on a tag when the presence of that tag pretty reliably indicates that ONLY that language will ever be in any of those questions, at least 90% of the time.. Dec 13, 2010 at 6:44
  • 2
    @Jeff: That makes sense... and is a natural problem where (say) XML can come in just about any kind of question.
    – Jon Skeet
    Dec 13, 2010 at 6:57
  • 3
    [iphone] should be Objective-C, as prettify supports Objective-C. Specifically, lang-m.
    – user23743
    Jan 27, 2011 at 10:58
5
votes

This causes a potential problem with at least the WordPress Answers site, as explained in this meta question. Basically, because the whole WordPress Answers site is about WordPress, the [wordpress] tag is almost never used, and thus, prettify will rarely be invoked, unless a specific language tag is also used. However, because WordPress is written in PHP, the [php] tag is also commonly left off of questions, so it's not uncommon to have a question with code in it that does not have a code-specific tag and does not have the [wordpress] tag.

Is it possible to cause all questions on WordPress Answers to invoke the "default" behavior, where prettify infers a language?

5
votes

c++0x should be highlighted as c++

[c++0x] = lang-c
5
votes

Is it possible to have "related tags" feature that when certain tag is set on question, the syntax highlight will take into account additional tags that are always related?

For example, when jQuery tag is set, it's always good idea to also highlight "pure" JavaScript as well, so the highlighter should take into account both tags. Same with HTML, very often JavaScript/jQuery questions also involve HTML and such code is posted as well but not rendered nicely unless the html tag is explicitly included.

It's possible to manually edit each question and add those tags but would be nicer to have it done "behind the scenes" without the actual tags added.

5
  • 1
    this is what the "default" tag status already does, see above Dec 16, 2010 at 8:56
  • @Jeff thanks, so this covers the jQuery/JavaScript issue but it would still be nice to have HTML highlighted when there is only javascript tag, as many times questions involving JavaScript also contain HTML code without really talking about HTML. Dec 16, 2010 at 9:00
  • you misunderstand; "default" means it can infer ANY language including HTML. Just not 100% reliably. Dec 16, 2010 at 9:45
  • 3
    @Jeff but javascript tag is not under the default category, so if a question contains only that tag and also have HTML code, the HTML code won't be highlighted correctly. For example this question stackoverflow.com/questions/4458119/… had only javascript tag so the HTML posted wasn't formatted well, only after I added html tag it was highlighted properly. Dec 16, 2010 at 9:51
  • @Jeff, see also the screen capture at Inconsistent highlighting of HTML block. It's a bit cumbersome to always require the html tag as well, when tagging javascript or jquery. As I think that default often works fine for JavaScript, maybe set javascript and jquery to use default rather than lang-js?
    – Arjan
    Jan 11, 2011 at 13:58
5
votes

Pascal code (still) isn't getting highlighted, but should be.

[pascal] = default
4
votes
[cl] = lang-lisp
[el] = lang-lisp

This seems wrong. If I look at question tagged with [cl] or [el], they have nothing to do with lisp. You probably meant [common-lisp] and [emacs-lisp]/[elisp] here (btw the latter two should be made synonyms, but that's a different issue).

Additionally [scheme] and [clojure] should also be lang-lisp and [ml] should be lang-ml.

I also recommend setting [j], [k] and [apl] to null because those languages look horrible with default highlighting.

0
4
votes
[node.js] = lang-js

See, for example, How to make an HTTP POST request in node.js?, which is tagged but not and gets no highlighting.

1
  • Good catch. Never noticed that.
    – jcolebrand
    May 28, 2011 at 2:34
3
votes

I have a question about this statement:

If a question has two tags that both define specific languages, it uses default and lets prettify infer as it always has.

So does that means that, for instance, I have two tags that are linked to the same specific language, say [tagA] -> lang-a and [tagA-v3] -> lang-a, does it fulfill the above condition, triggering default mode? Won't that be counter-productive?

If the above does not happen (ie. Default mode only gets triggered when two different languages are implied by the tags), then shouldn't we link libraries written in a specific language to that language? jQuery -> JavaScript comes to mind of course, but there certainly are many others. I'm asking this because there are quite a number of questions tagged but not .


Also, it appears as there's no such thing as lang-as for ActionScript. The tag is currently linked to [lang-js], which may or may not highlight correctly, since the two languages are based off different versions of the ECMAScript specifications.

3
  • questions tagged [jquery] tend to have a lot of html and other syntax-highlightable cruft in them. If we force a language this means every jquery question with a HTML snippet in it will NOT syntax highlight that HTML snippet. Dec 13, 2010 at 4:55
  • @JeffAtwood I see. Thanks for the clarification
    – Yi Jiang
    Dec 13, 2010 at 5:02
  • 1
    But, @Jeff, wouldn't the same apply to javascript itself? Currently that tag does set a specific language and hence currently requires the html tag to avoid HTML erroneously being highlighted as JavaScript. (See link with screen capture I added to Shadow Wizard's answer.)
    – Arjan
    Jan 11, 2011 at 14:07
3
votes
  • [sqlalchemy] = lang-py
  • [webpy] = lang-py
  • [cherrypy] = lang-py
  • [py2exe] = default
  • [beautifulsoup] = lang-py or default as it might have some HTML
  • [numpy] = lang-py
  • [wxpython] = default (it could be XML as well)
  • [boo] = lang-py (it's not the same language, but it's close enough. Only thing, there are not too many questions.)
  • [scriptaculous] = lang-js
  • [turbogears] = lang-py
3
  • dispute sql alchemy, and hinting it to python would break an sql queries posted as part of the question Dec 13, 2010 at 22:00
  • before I take action on any of these I'd like to see specific examples of where a question with these tags has bad syntax highlighting Dec 14, 2010 at 4:15
  • @Jeff Atwood - stackoverflow.com/questions/4116658 is tagged [numpy], and has no highlighting.
    – mtrw
    Feb 3, 2011 at 16:11
3
votes

Jeff, I wonder if it would be possible to have an unobtrusive display of the language which has been chosen? That would help get the tags right.

I am concerned that people will choose the tags based on the language. As I have stated before, a question should not be tagged "C#" simply because that's the language being used.

1
  • at the very least initially show the unobtrusive display, with a reminder to reevaluate in 6-8 weeks ;)
    – jcolebrand
    Dec 18, 2010 at 0:42
3
votes

Those tags needs default syntax highlighting, not null (also not xml nor java). They contain posts with a mix of Java code and XML-like markup. Both needs to be highlighted as nicely as it did before this change.

, , , , , and .

0
2
votes

is there a, or do we have a need for, a [lang-pseudo] for when you're trying to explain something in pseudocode? (often for my needs [lang-c] would be fine ;] )

Also, does this say what happens to answers formatting? What if I'm discussing [asp.net] with someone and the question is tagged c# but they really need to update the aspx instead of the cs and so I put some HTML in my answer in the codeblock, then how does it filter that my code is HTML and not C#?

Am I overthinking this?

1
2
votes

None of the shell scripting languages have highlighting anymore

[bash] = lang-bsh
[shell] = lang-sh
[sh] = lang-sh
[csh] = lang-csh
[awk] = default
1
  • There is scripting language BeanShell, which is pretty much Java code, and the interpreter is called bsh. Is bsh a common abbrev. for bash? Feb 5, 2012 at 22:59
2
votes

Octave code should be highlighted like Matlab.

[octave] = default
2
votes

Well it appears the language inference has been pulling its weight thus far. There is however a minor issue here.

I experimented with the question Webclient.UploadFile which is tagged tagged c#, .net, vb.net and webclient.

When I tried answering the question with VB.NET code [which is perfectly valid based on the OP's tags] the code I entered was formatted as C#! Quite unbelievable!

Well I did not really answer the question. I wanted to test the code inference system and from the answer preview I got, it failed to infer the language properly.

I believe including a way to explicitly state which language should be used to highlight code entered, especially on the answerer's side, would be very much appreciated.

3
  • There's a [feature-request] for this, go support that
    – Yi Jiang
    Feb 7, 2011 at 8:24
  • @Yi Jiang: Can you please give me the link to that [feature-request] question so I go and support it? Feb 7, 2011 at 13:18
  • Hmmm, it seems like I'm wrong, meta.stackexchange.com/questions/981/… isn't the correct [feature-request]. In this case, it might be good to open a new question.
    – Yi Jiang
    Feb 7, 2011 at 13:23
2
votes

There is an inconsistency between the preview and the final result with respect to multiple language blocks.

E.g: I specify a language <!-- language: lang-cs --> right in front of this code block:

var test = ThisIsACSharpObject;

Then I have some text, and then again a code-block:

var test2 = ThisIsAnotherCSharpObject;

In the preview (on Stack Overflow proper), both blocks are formatted like they should as C# code, but when I post this, only the first one is formatted right. I have to specifically add the language tag in front of all the different codeblocks for them to be rendered right after posting.

(Apparently this doesn't work on meta... I suppose the code formatting thing is not enabled here ==> I think it should be enabled, as how can we otherwise reproduce problems concerning this here?)

Edit: Ow hey, apparently, here on meta this has a different behavior: I never see the code formatting in the preview while editing, but after posting, it is actually formatted (wrongly in my opinion, as only the first code block is formatted right), so we can actually reproduce this problem here... as you can see.

1
  • no guarantees whatsoever that preview will match in this case May 9, 2011 at 21:33
2
votes

Questions tagged often mention Python code, so I propose

[mercurial] = lang-py
1
vote

I miss the old formatting, which while imperfect at least didn't suck enough for me to complain about. So here's one I'd like to see:

[tcl] = default
1
  • (Better would be if there was Tcl highlighting in prettify, but that would require real work…) Dec 13, 2010 at 14:45
1
vote

The and questions that doesn't have a specific language tag should use default highlighting. Maybe even all LINQ tags?

particularly:

[linq]             = default
[linq-to-sql]      = default
[linq-to-entities] = default
[linq-to-xml]      = default
[linq-to-objects]  = default
1
vote

Most of our GWT questions/answers currently show unhighlighted code.

It probably needs default highlighting, because the questions very often include a combination of Java, HTML, CSS, XML and JavaScript code blocks, without being specific questions for these tags. (Especially, GWT is not Java, it just looks like Java code, and adding a Java tag often results in answers that work in Java, but not in GWT.)

[gwt] = default
0
votes

QTP scripts are based on VBScript

[qtp] = lang-vb

You must log in to answer this question.

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