nbell12 / google-code-prettify

Automatically exported from code.google.com/p/google-code-prettify
Apache License 2.0
0 stars 0 forks source link

Allow to run prettyprint multiple times #40

Closed GoogleCodeExporter closed 8 years ago

GoogleCodeExporter commented 8 years ago
What steps will reproduce the problem?
1. Run prettyprint() twice: the 2nd run causes previous rendered <pre> or 
<code> blocks to 
loose there formatting. 
2.
3.
(Please include HTML, not just your source code)

What is the expected output?  What do you see instead?

I would expect prettify to skip the blocks which already have been formatted.
This is especially useful when running prettify after eg ajax calls.

What version are you using?  On what browser?
vs 31 Aug 2007

Please provide any additional information below.

Suggeted implementation::
I changed prettify.js to add a 'prettified' class to each block when rendered.

         //mark the element as converted
         cs.className = cs.className+' prettified';

Then added a simply check of this classname when entereing the doWork loop

      /* added classname 'prettified' to avoid double processing */
      if (cs.className 
       && cs.className.indexOf('prettyprint') >= 0 
       && cs.className.indexOf('prettified') == -1 ) {

Original issue reported on code.google.com by dirk.fre...@gmail.com on 2 Jun 2008 at 7:18

GoogleCodeExporter commented 8 years ago
Pages which load code blocks dynamically using, e.g. jQuery, could really 
benefit
from this (though i would recommend changing the class name to something less 
likely
to conflict with client code, such as "google-code-prettified").

Original comment by sgbeal@googlemail.com on 21 Feb 2009 at 11:46

GoogleCodeExporter commented 8 years ago
Quote: "Google Prettify supports another API, prettyPrintOne(), which allows 
you to pass in a string to be 
prettified. "
http://www.codingthewheel.com/archives/syntax-highlighting-stackoverflow-google-
prettify

With this information I implemented the following in jQuery.

var my_html = '<html><head></head><body class="testing"><p id="hello">Hello 
World</p></body></html>';
$('#code').append(<pre class="prettyprint"></pre>');
$('#code > pre:last').text(my_html).html(prettyPrintOne($('#code > 
pre:last').html()));
// note: .text() calls the DOM method .createTextNode(), which replaces special 
characters with their HTML 
entity equivalents (such as <  for <).

Original comment by i.chris....@gmail.com on 25 May 2010 at 7:05

GoogleCodeExporter commented 8 years ago
In reference to comment #2, This is a solution for most, but not all cases. I 
have to agree with comment #1 about a non-conflicting CSS class. An example 
case where #2 would be ineffictive at the least, involves getting back the 
following snippet of HTML:

<div class='something'>blah blah blah</div><pre><code 
class='prettyprint'>SomeCode</code></pre><span>Some more crap</span>

Not that it couldn't be done with #2's version, but it becomes more difficult. 
It would be far easier if prettyprint could be run multiple times.

Original comment by plagueth...@gmail.com on 22 Apr 2012 at 10:06

GoogleCodeExporter commented 8 years ago
I actually fixed this but did not notice that this duplicate bug persisted.

Original comment by mikesamuel@gmail.com on 23 Apr 2012 at 4:18

GoogleCodeExporter commented 8 years ago
Fixed at http://code.google.com/p/google-code-prettify/source/detail?r=204#

Original comment by mikesamuel@gmail.com on 23 Apr 2012 at 4:19

GoogleCodeExporter commented 8 years ago
Simple fix remove class "prettyprinted" from the tag before calling 
prettyPrint()

Original comment by gagandee...@gmail.com on 4 Jan 2014 at 10:48