urini
urini

Reputation: 33049

How to escape < and > inside <pre> tags

I'm trying to write a blog post which includes a code segment inside a <pre> tag. The code segment includes a generic type and uses <> to define that type. This is what the segment looks like:

<pre>
    PrimeCalc calc = new PrimeCalc();
    Func<int, int> del = calc.GetNextPrime;
</pre>

The resulting HTML removes the <> and ends up like this:

PrimeCalc calc = new PrimeCalc();
Func del = calc.GetNextPrime;

How do I escape the <> so they show up in the HTML?

Upvotes: 105

Views: 97494

Answers (8)

Chris Marasti-Georg
Chris Marasti-Georg

Reputation: 34640

<pre>&gt;</pre>

renders as:

>

So you want:

<pre>
    PrimeCalc calc = new PrimeCalc();
    Func&lt;int, int&gt; del = calc.GetNextPrime;
</pre>

which turns out like:

    PrimeCalc calc = new PrimeCalc();
    Func<int, int> del = calc.GetNextPrime;

Upvotes: 24

PanicBus
PanicBus

Reputation: 576

A better way to do is not to have to worry about the character codes at all. Just wrap all your code inside the <pre> tags with the following

<pre>
${fn:escapeXml('
  <!-- all your code -->
')};
</pre>

You'll need to have jQuery enabled for it to work, tho.

Upvotes: -6

crashmstr
crashmstr

Reputation: 28563

Use &lt; and &gt; to do < and > inside html.

Upvotes: 18

ckpwong
ckpwong

Reputation: 2129

&lt; and &gt; respectively

Upvotes: 8

John Sheehan
John Sheehan

Reputation: 78104

<pre>
    PrimeCalc calc = new PrimeCalc();
    Func&lt;int, int&gt; del = calc.GetNextPrime;
</pre>

Upvotes: 126

akdom
akdom

Reputation: 33149

What rp said, just replace the greater-than(>) and less-than(<) symbols with their html entity equivalent. Here's an example:

<pre>
    PrimeCalc calc = new PrimeCalc();
    Func&lt;int, int&gt; del = calc.GetNextPrime;
</pre>

This should appear as (this time using exactly the same without the prepended spaces for markdown):

    PrimeCalc calc = new PrimeCalc();
    Func<int, int> del = calc.GetNextPrime;

Upvotes: 3

toolkit
toolkit

Reputation: 50227

How about:

&lt; and &gt;

Hope this helps?

Upvotes: 7

OwenP
OwenP

Reputation: 25378

It's probably something specific to your blog software, but you might want to give the following strings a try (remove the underscore character): &_lt; &_gt;

Upvotes: -1

Related Questions