How to escape < and > inside <pre> tags

HtmlEscapingMarkupBlogger

Html Problem Overview


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?

Html Solutions


Solution 1 - Html

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

Solution 2 - Html

<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;

Solution 3 - Html

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

Solution 4 - Html

How about:

&lt; and &gt;

Hope this helps?

Solution 5 - Html

&lt; and &gt; respectively

Solution 6 - Html

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;

Solution 7 - Html

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;

Solution 8 - Html

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionuriniView Question on Stackoverflow
Solution 1 - HtmlJohn SheehanView Answer on Stackoverflow
Solution 2 - HtmlChris Marasti-GeorgView Answer on Stackoverflow
Solution 3 - HtmlcrashmstrView Answer on Stackoverflow
Solution 4 - HtmltoolkitView Answer on Stackoverflow
Solution 5 - HtmlckpwongView Answer on Stackoverflow
Solution 6 - HtmlakdomView Answer on Stackoverflow
Solution 7 - HtmlOwenPView Answer on Stackoverflow
Solution 8 - HtmlPanicBusView Answer on Stackoverflow