156

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?

3
  • 11
    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... Commented Jul 19, 2011 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
    Commented Jan 31, 2012 at 6:04
  • 3
    @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
    Commented Nov 6, 2012 at 7:23

1 Answer 1

168

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?

18
  • 1
    @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
    Commented Mar 7, 2011 at 19:54
  • 10
    What is the recommended language tag for 'make' or 'makefile'? I guess 'lang-sh' is a reasonable approximation. Commented May 16, 2011 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? Commented May 26, 2011 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. Commented May 26, 2011 at 6:18
  • 1
    I don't understand what's the difference between c and lang-c. Commented Mar 26, 2012 at 9:02
  • 5
    @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. Commented May 25, 2012 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. Commented May 25, 2012 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. Commented May 26, 2012 at 4:01
  • 1
    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. Commented Jul 3, 2012 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 <!--?...?-->). Commented Feb 25, 2013 at 18:50
  • ok @MikeSamuel I will make sure the devs know! (I am no longer at Stack Exchange) Commented Feb 25, 2013 at 22:42
  • 7
    Any chance of adding syntax highlighting for more languages, such as Windows command line for batch files or Windows PowerShell?
    – Zarepheth
    Commented Jul 18, 2013 at 20:51
  • 1
    Why doesn't YAML have a tag? It's in Google Prettify...
    – CommaToast
    Commented Jul 22, 2013 at 3:36
  • 1
    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. Commented Jul 22, 2013 at 4:29
  • 1
    I've created the tag tag-syntax-association for requesting association be made between tags and their corresponding language name (something only moderators can do)
    – Eric
    Commented Aug 16, 2013 at 10:53

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