4

Possible Duplicate:
Which tools and technologies build the Stack Exchange Network?
What syntax highlighting library is used on Stack Overflow?

How does Stack Overflow handle its syntax highlighting? Example:

String[] files = Directory.GetFiles();
int count = files.Length;

Syntax highlighting is nice. But I don't see any formatting mumbo jumbo in the page source. Only <pre> and <code> tags. How is it done?

8
  • ...and is there any way to tell it about the language when it's wrong?
    – Anton Kovalenko
    Jan 21 '13 at 21:07
  • It's probably a library, so I think you'd better look at the linked script (or the resources tab in the inspector). There are plenty of libraries handling syntax highlighting, so why would you reinvent the wheel?
    – 11684
    Jan 21 '13 at 21:08
  • @AntonKovalenko there is! Just add <!-- language: [desired langiage] --> above your code, as stated in the formatting help.
    – 11684
    Jan 21 '13 at 21:10
  • The code is highlighted using Javascript. That's why it looks all clean when you look at the source, but in fact, it's just about to be transformed into a "mess" of <span>s.
    – zneak
    Jan 21 '13 at 21:11
  • Well, now this is interesting. I get the mess of spans when I Inspect Element in Chrome. If I view source, I get clean output with only pre and code.
    – Gigi
    Jan 21 '13 at 21:16
  • 1
    @Gigi that is because the 'source' is the response from the server, the inspect element reflects the actual state of the elements, after it has been processed by JS etc.
    – 11684
    Jan 21 '13 at 21:17
  • Thanks for clarifying. I'm not very familiar with JavaScript. What kind of code would you need to do something of the sort, transforming one kind of content to another? Any function calls in particular?
    – Gigi
    Jan 21 '13 at 21:19
  • And you could study how the Prettify project works from the source code. Jan 21 '13 at 21:29
2

This is answered here - see the section of the answer labelled "Syntax Highlighting", it even includes a link ;)

2
  • +1, although you might add in your answer it is done with Prettify, because at first sight it might seem you misunderstood the question and are clarifying how you can tell Stackoverflow to highlight your syntax. (I think you should read more than 'first sight', but I just want to save you from unnecessary down votes.)
    – 11684
    Jan 21 '13 at 21:13
  • @11684 agreed - that's what I thought at first. Maybe Kolink might want to add some more detail for the internet users out there who are curious and may thus easily satisfy their curiousity.
    – Gigi
    Jan 21 '13 at 21:15

This site is temporarily in read only mode and not accepting new answers.

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