views:

144

answers:

3

I'm writing some documentation that will occasionally include C# or C++ code snippets.

In Visual Studio, the Edit/Copy command includes syntax coloring. You can see this if you paste the text into, e.g., Word or Outlook. In fact, if you use the "Paste Special..." command, you can see that it's "Rich Text (RTF)".

However, when pasting this text into an HTML file in Visual Studio, none of the colouring (or even formatting) is preserved.

Any ideas? I'm looking specifically for something that works locally (preferably in Visual Studio), not JavaScript-based solutions (such as SyntaxHighlighter).

+1  A: 

You can use existing C to HTML syntax highlighter tools for converting your snippets into HTML and copy&pasting from there. Here's a list of such tools (not exhaustive and in no particular order):

  • GNU Enscript
  • GNU Emacs with htmlize.el module allows you to dump a highlighted Emacs buffer as HTML

There might also exist web services that allow you to copy&paste C code into a form and get highlighted HTML back.

Nurpax
+3  A: 

There's an addin called CopySourceAsHtml which does a pretty neat job. I've used it a few times, but I use a Javascript to do the highlighting on my blog these days (so it's "plaintext readable").

Steven Robbins
If it was for my blog, I'd probably use JavaScript. Unfortunately, it's not (at least, not yet).
Roger Lipscombe
Copying style information along with content is IMHO a bad idea. You should keep the style separate from the data. For example, what if you wanted to change the colour of the keywords later on? If it's embedded in amongst the code you will have hell of a game changing it all.
Gary Willoughby
Yeah, I just noticed that: it's an implementation detail of CopySourceAsHTML; I'd rather it used <span> tags with classes, so that I could play with the styles later.
Roger Lipscombe
I'm sure it's done that way so it can avoid any dependencies.. otherwise you'd have to reference a style sheet, rather than just paste in the code.
Steven Robbins
And if you did want to play with the styles, you could keep a project with your snippets in, change VS.Net colouring, and paste them back in.. not perfect, but better than doing it by hand for each one :)
Steven Robbins
It turns out that it's done that way because it gets Visual Studio to copy the selection to the clipboard as RTF. Then it converts the RTF to HTML. By that point, the semantics have been lost. All that's left is the formatting styles.
Roger Lipscombe
A: 

I use this for highlighting code on webpages. It's simple to use and easy to extend.

http://code.google.com/p/syntaxhighlighter/

It also supports C#.

Gary Willoughby