views:

802

answers:

7

I decided to try http://www.screwturn.eu/ wiki as a code snippet storage utility. So far I am very impressed, but what irkes me is that when I copy paste my code that I want to save, '<'s and '[' (http://en.wikipedia.org/wiki/Character_encodings_in_HTML#Character_references) invariably screw up the output as the wiki interprets them as either wiki or HTML tags.

Does anyone know a way around this? Or failing that, know of a simple utility that would take C++ code and convert it to HTML safe code?

+2  A: 

You can use the @@...@@ tag to escape the code and automatically wrap it in PRE tags.

Dario Solera
Can you elaborate a bit further?
Konrad
+1  A: 

Surround your code in <nowiki> .. </nowiki> tags.

moonshadow
nowiki helps but does not solve the problem entirely
Konrad
A: 

I don't know of utilities, but I'm sure you could write a very simple app that does a find/replace. To display angle brackets, you just need to replace them with &gt; and &lt; respectively. As for the square brackets, that is a wiki specific problem with the markdown methinks.

Greg Ogle
A: 

have you tried wrapping your code in html pre or code tags before pasting? both allow any special characters (such as '<') to be used without being interpreted as html. pre also honors the formatting of the contents.

example

if (foo
The Brawny Man
A: 

Dario Solera wrote "You can use the @@...@@ tag to escape the code and automatically wrap it in PRE tags."

If you don't want it wrapped just use: <esc></esc>

Chris M.
A: 

List of characters that need escaping:

  • < (less-than sign)
  • & (ampersand)
  • [ (opening square bracket)
ΤΖΩΤΖΙΟΥ
A: 

To post C++ code on a web page, you should convert it to valid HTML first, which will usually require the use of HTML character entities, as others have noted. This is not limited to replacing < and > with &lt; and &gt;. Consider the following code:

unsigned int maskedValue = value&mask;

Uh-oh, does the HTML DTD contain an entity called &mask;? Better replace & with &amp; as well.

Going in an alternate direction, you can get rid of [ and ] by replacing them with the trigraphs ??( and ??). In C++, trigraphs and digraphs are sequences of characters that can be used to represent specific characters that are not available in all character sets. They are unlikely to be recognized by most C++ programmers though.

bk1e