What is meta? ×
Meta Stack Exchange is where users like you discuss bugs, features, and support issues that affect the software powering all 122 Stack Exchange communities.

This question already has an answer here:

I'm the maintainer for Google Code Prettify.

Markup authors can specify the language for source code, but not markdown authors. Are the SO admins interested in a way of doing this?

If so, might the following work

  1. If the first line is "#!" followed by one or more word characters, then make the first line invisible (using CSS hidden:visible) and use those word characters as the file extension.
  2. Otherwise, use the current scheme.

So

#!css
tag.class#id { color: red }

would be recognized as CSS.

In markdown, should any indicator of line numbers survive copy-paste in markdown?

Does any scheme like this need to do work to handle quoting of responses in markdown? Is it common for inline responders to split code sections?

Do users also want to number lines ( http://google-code-prettify.googlecode.com/svn/trunk/README.html#linenums ), so that others can respond with reference to particular lines?

share|improve this question
7  
It's a pity that the shebang with an interpreter name isn't counted as a hint about the language. (An example -- a bash script that could be highlighted accordingly if the shebang line would be taken into account: stackoverflow.com/questions/6748083/… .) I'm used to that Emacs does this... –  imz -- Ivan Zakharyaschev Jul 19 '11 at 14:52
    
@imz: yeah, given the way prettify is supposed to be able to guess languages in easy cases, you'd think that'd be an excellent cue for it... –  SamB Jan 31 '12 at 6:04
    
@Mike, can you please point me to some documentation on how to contribute a new language to Prettify? I'm pascal fan and really would love to see the pascal syntax highlighted here and there. Thanks. :) –  jachguate Nov 6 '12 at 7:23
add comment

marked as duplicate by animuson Aug 24 '13 at 15:23

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

1 Answer

up vote 122 down vote accepted

Syntax Highlighting with “Code Prettify”

This is now implemented. See Stack Overflow’s help on Syntax Highlighting.

Specifying a language for syntax highlighting

Besides tag inferences (a recent change), you can manually specify a language as a hint to Google Code Prettify.

Here’s how:

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

    «code goes here»

«more text»

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

    «code goes here»

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

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?

share|improve this answer
    
@kevin, as there are no tags for "none" and "default": are those still somehow supported? (I also copied your edit to one of Jeff's other posts.) –  Arjan Mar 7 '11 at 19:54
7  
What is the recommended language tag for 'make' or 'makefile'? I guess 'lang-sh' is a reasonable approximation. –  Jonathan Leffler May 16 '11 at 22:26
    
@Jeff, I guess my question is, do I need to bother on my vba questions using the prettify language code lang-vb, or should I just let my vba tag take care of it. And then if the tag does take care of it, does it function better (or more smoother) if I use the prettify code? –  Lance Roberts May 26 '11 at 6:14
    
@lance let the tags carry whenever possible, it's much simpler (and it fixes it for "everyone" when the tags "just work" as much as possible). Use Google Chrome inspector to view the CSS classes on the rendered <pre> to see what is actually finally happening in the post. That's what I do anyway. –  Jeff Atwood May 26 '11 at 6:18
    
I don't understand what's the difference between c and lang-c. –  user unknown Mar 26 '12 at 9:02
4  
@User, c is a tag; lang-c is a Prettify language code. When a question is tagged c, any code in the question automatically gets highlighted with the lang-c language code because the tag is configured (somewhere) to be associated with that language code. The distinction is important for tags that don't have a specific language. For example, the pascal tag is configured with lang-default; there is no lang-pas language code. Manually marking code as lang-pas will leave it unhighlighted, but marking it pascal will invoke the default highlighting rules. –  Rob Kennedy May 25 '12 at 21:53
    
@RobKennedy: In Code-golf, nearly all questions are language agnostic. So there I mark my scala solutions <-- language: lang-scala -->. This shall always work, since there is a prettify tag for scala. Below the bullet list which ends in lang-xml there are more tags, "bsh", "c", "cc", ... - some of them are listed above, some are not. I still don't get the distinction. –  user unknown May 25 '12 at 22:13
1  
That's right, @User. There are some Stack Overflow tags that don't have corresponding Prettify language codes. I mentioned Pascal. If I mark Pascal code with lang-pas, it won't get highlighted; Prettify doesn't currently support that language. If I mark it with pascal, it will get highlighted; Stack Overflow currently associates the lang-default code with that tag. It doesn't get highlighted well, but at least there's something. When Prettify eventually supports Pascal, the we'll re-associated that tag with lang-pas, and all existing Pascal code will get highlighted correctly. –  Rob Kennedy May 26 '12 at 4:01
    
From my experience the segment to be highlighted needs to be formatted as code or no highlighting will occur. If that's the case it should be stated here. –  ThomasS Jul 3 '12 at 8:53
    
Latest release of prettify now looks for comments/processing-instructions of the form <?prettify lang=...> to better interoperate with translators that produce HTML like markdown. (HTML5 specifies that <?...?> is equivalent to <!--?...?-->). –  Mike Samuel Feb 25 '13 at 18:50
    
ok @MikeSamuel I will make sure the devs know! (I am no longer at Stack Exchange) –  Jeff Atwood Feb 25 '13 at 22:42
    
what about xpath? –  Alex Mar 21 '13 at 17:15
2  
Any chance of adding syntax highlighting for more languages, such as Windows command line for batch files or Windows PowerShell? –  Zarepheth Jul 18 '13 at 20:51
    
Why doesn't YAML have a tag? It's in Google Prettify... –  CommaToast Jul 22 '13 at 3:36
    
The prettify language code for c# is lang-csharp but not lang-cs. Using lang-cs causes to turn on the default highlighting and doesn't highlight the using namespace and virtual keywords. –  Sri Harsha Chilakapati Jul 22 '13 at 4:29
show 3 more comments

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