16

I am trying to write a blog and I want to display c# code in a neat formatted way. Is there a way to it. I like to do it exactly the way stack overflow does including color. :)

Paul Roub
  • 36,322
  • 27
  • 84
  • 93
VolleyBall Player
  • 710
  • 3
  • 11
  • 27

5 Answers5

29

You have quite a few options here, but I would recommend against using a JavaScript formatter. If a user has scripts disabled, you still want the code to look good.

If you are using Visual Studio, the Productivity Power Tools add-in has a "copy HTML" option:
http://blogs.msdn.com/b/kirillosenkov/archive/2010/06/07/copy-code-in-html-format-with-visual-studio-2010.aspx

Jon Skeet provides a code formatter for public use:
http://csharpindepth.com/CodeFormatterTool.aspx

If you are using PHP, GeSHi is a good server-side option:
http://qbnz.com/highlighter/

I believe that stack-overflow uses google-code-prettify:
http://code.google.com/p/google-code-prettify/

SyntaxHighlighter is another good client-side solution:
http://alexgorbatchev.com/SyntaxHighlighter/

John Gietzen
  • 48,783
  • 32
  • 145
  • 190
9

SO uses google-code-prettify, which is Javascript + CSS. You should get all the documentation you need on that site.

Also, if you don't mind using PHP, there's GeSHi. But client-side is probably better, so I'd go with the code prettifier.

Rafe Kettler
  • 75,757
  • 21
  • 156
  • 151
4

you can use third-party if you want: Syntax Highlighter

Beytan Kurt
  • 2,203
  • 4
  • 34
  • 57
3

The Syntax Highlighter and google-code-prettify are the client-side solutions, its probably better because its more versatile to be applied in any web pages ( .php | .aspx | .jsp | .html ). Its downfall is that :

  1. Initially when the page is loaded, formatted code is displayed in raw format. It takes up some time for the javascript to build the syntax highlighting of the code.

  2. Doesn't work if the javascript is disabled.

Solution: Pre-build the syntax formatted code. Syntax Highlight Generator is an open source Online Tool to pre-build your code. This tool is built over Syntax Highlighter v3.0 library.

Syntax Highlight Generator's configuration: codeworkout.blogspot.com/2014/07/online-generator-tool-for-code-syntax_18.html

ajdatosys
  • 33
  • 4
-5
    <blockquote>
             //.....Code
    <blockquote>
Ai47
  • 41
  • 3