Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Google Prettify to display HTML

for Google Prettify to display HTML code sample properly, you should replace all the < with &lt; and all the > with &gt;.

How do you automate that process using JavaScript only ?

like image 345
Brett Avatar asked May 04 '12 02:05

Brett


1 Answers

If you put your code inside an <xmp> element, you don't need to escape HTML special characters as shown in the tests

 <h1>HTML using XMP</h1>
 <xmp class="prettyprint" id="htmlXmp"
 ><html>
   <head>
     <title>Fibonacci number</title>
   </head>
   <body>
     <noscript>
       <dl>
         <dt>Fibonacci numbers</dt>
         <dd>1</dd>
         <dd>1</dd>
         <dd>2</dd>
         <dd>3</dd>
         <dd>5</dd>
         <dd>8</dd>
         &hellip;
       </dl>
     </noscript>

     <script type="text/javascript"><!--
 function fib(n) {
   var a = 1, b = 1;
   var tmp;
   while (--n >= 0) {
     tmp = a;
     a += b;
     b = tmp;
   }
   return a;
 }

 document.writeln(fib(10));
 // -->
     </script>
   </body>
 </html>
 </xmp>
like image 84
Mike Samuel Avatar answered Oct 05 '22 23:10

Mike Samuel