Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C#/.NET library for source code formatting, like the one used by Stack Overflow?

I am writing a command line tool to convert Markdown text to html output, which seems easy enough.

However, I am wondering how to get nice syntax coloring for embedded code blocks, like the one used by Stack Overflow.

Does anyone know either:

  • What library StackOverflow is using
  • or if there's a library out there that I can easily reuse?

Basically it would need to have some of the same "intelligence" found in the one that Stack Overflow uses, by basically doing a best-attempt at figuring out the language in use to pick the right keywords.

Basically, what I want is for my own program to handle a block like the following:

if (a == 0) return true;
if (a == 1) return false; // fall-back

Markdown Sharp, the library I'm using, by default outputs the above as a simple pre/code html block, with no syntax coloring.

I'd like the same type of handling as the formatting on Stack Overflow does, the above contains blue "return" keywords for example.

Or, hmm, after checking the source of this Stack Overflow page after adding the code example, I notice that it too is formatted like a simple pre/code block. Is it pure javascript-magic at works here, so perhaps there's no such library?

If there's no library that will automagically determine a possible language by the keywords used, is there one that would work if I explicitly told it the language? Since this is "my" markdown-commandline-tool, I can easily add syntax if I need to.

like image 726
Lasse V. Karlsen Avatar asked Feb 27 '23 17:02

Lasse V. Karlsen


1 Answers

Oops - I completely missed the fact that you're already using Markdown Sharp - but that is the Markdown processor Stack Overflow uses.

However, it also uses Google Code Prettify - and that's the bit that provides the colors. Is that the bit you were missing?

like image 195
Jon Skeet Avatar answered Apr 26 '23 03:04

Jon Skeet