Reputation: 710
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. :)
Upvotes: 16
Views: 29506
Reputation: 33
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 :
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.
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
Upvotes: 3
Reputation: 49534
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/
Upvotes: 29
Reputation: 76945
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.
Upvotes: 9