Konrad
Konrad

Reputation: 40947

Converting C++ code to HTML safe

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?

Upvotes: 1

Views: 1878

Answers (7)

The Brawny Man
The Brawny Man

Reputation: 661

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

<pre>
    if (foo <= bar) {
        do_something();
    }
</pre>

Upvotes: 0

bk1e
bk1e

Reputation: 24328

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.

Upvotes: -1

tzot
tzot

Reputation: 95921

List of characters that need escaping:

  • < (less-than sign)
  • & (ampersand)
  • [ (opening square bracket)

Upvotes: 0

Chris M.
Chris M.

Reputation: 651

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>

Upvotes: 0

oglester
oglester

Reputation: 6655

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.

Upvotes: 0

moonshadow
moonshadow

Reputation: 89065

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

Upvotes: 1

Dario Solera
Dario Solera

Reputation: 5804

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

Upvotes: 2

Related Questions