VolleyBall Player
VolleyBall Player

Reputation: 710

How to display formatted code in webpage

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

Answers (5)

ajdatosys
ajdatosys

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 :

  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

Upvotes: 3

Ai47
Ai47

Reputation: 41

    <blockquote>
             //.....Code
    <blockquote>

Upvotes: -5

John Gietzen
John Gietzen

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

Rafe Kettler
Rafe Kettler

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

Beytan Kurt
Beytan Kurt

Reputation: 2263

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

Upvotes: 4

Related Questions