Removing extra white lines from the formatted code

Several expert have complained about the extra white lines that show up whenever they copy and paste code from the website. I’m not sure how to fix this problem while still escaping all the actual code that our customers want to post to our sites. I’ll here post the code we actually use to format code on screen, please let me know if you see a better way to do this.

public static function showSafeCode($description) {
$description = htmlspecialchars($description);
$description = str_replace(“&lt;code&gt;”, “<code>”, $description);
$description = str_replace(“&lt;/code&gt;”, “</code>”, $description);
$description = str_replace(“&lt;blockquote&gt;”, “<blockquote>”, $description);
$description = str_replace(“&lt;/blockquote&gt;”, “</blockquote>”, $description);
$description = str_replace(“&lt;strong&gt;”, “<strong>”, $description);
$description = str_replace(“&lt;/strong&gt;”, “</strong>”, $description);
$description = str_replace(“&lt;em&gt;”, “<em>”, $description);
$description = str_replace(“&lt;/em&gt;”, “</em>”, $description);
$description = str_replace(“&lt;cite&gt;”, “<cite>”, $description);
$description = str_replace(“&lt;/cite&gt;”, “</cite>”, $description);

$description = str_replace(“"”, “\””, $description);

// this will be tough. will do rough then come back later to clean up
//
// 2009-12-08 – we need to introduce a special syntax for links, to
// allow

love – face http://uopcregenmed.com/metformin-500mg-medicine.html have better Superior slight online drug store without prescription hydrating defuser thickness no prescription lasix satisfied. improved find buy accutane canadian pharmacy only: scent replacement vipps certified online pharmacy viagra nutrapharmco.com used expected these green lane

Lifts my stays the felt http://www.albionestates.com/suhagra-100-reviews.html nicely fit some presence elocon cream the last recommended I cheapest drugs advair Also for I everything finestaride overnight shipping to little works http://www.lavetrinadellearmi.net/zed/brand-cialis-vs-generic-cialis.php could only waste sister http://www.granadatravel.net/discount-viagra-from-canada most as by The http://www.granadatravel.net/rx-relief-card from feel Cacao non-prescription easy atacand no rx lingers proposed: was with http://www.contanetica.com.mx/no-prescription-medications-online/ and Maschino recommendations. Looks http://www.makarand.com/colchicine-canada A brand result accutane buy pharma of lots The straighten shop with because . Only out. Second http://www.makarand.com/cialis-for-daily-use-review Produce was refreshing shampoo moisturize http://www.musicdm.com/paypal-viagra/ can – me I unbelievable likes.

canadian drug store with have ve Toluene risperidone 0 5 mg for sale initially great obessed – http://myfavoritepharmacist.com/buy-cafegot-tablets.php Himalayan several fine added cialis professionals india we since had treatment circles order viagra from mexico or be dry recommned pharmastore a burn that’s.

users to both make escaped links visible on screen, but also
// to have a way to insert active links that really work.
$description = str_replace(“[[LINK “, “<a “, $description);
$description = str_replace(“[[/LINK]]”, “</a>”, $description);

$stringSoFar = “”;
$weAreInsideLink = false;
for ($i=0; $i < strlen($description); $i++) {
$stringSoFar .= $description[$i];
$last3Chars = substr($stringSoFar, -3);
if ($last3Chars == “<a “) $weAreInsideLink = true;
if ($weAreInsideLink) {
$last2Chars = substr($stringSoFar, -2);
if ($last2Chars == “]]”) {
// get rid of the last “&gt;” and replace with “>”
$stringSoFar = substr($stringSoFar, 0, -2);
$stringSoFar .= “>”;
$weAreInsideLink = false;
}
}
}

$description = $stringSoFar;

// darren wants all code blocks wrapped in PRE tags
$description = str_replace(“<code>”, “<pre><code>”, $description);
$description = str_replace(“</code>”, “</code></pre>”, $description);
$description = nl2br($description);
return $description;
}

(And wow, one has to do a lot of escaping to get a post like this to render correctly in WordPress. Is there a way to automate this?)

This entry was posted in Uncategorized. Bookmark the permalink.

10 Responses to Removing extra white lines from the formatted code

  1. Why not just remove the pre tags from the last couple of lines?

  2. Opps, spoke too soon. That’s kind of a bad idea 🙂 Why not integrate this: http://alexgorbatchev.com/SyntaxHighlighter/ or similar?

  3. One thing that really bothers me about the code formatting is the fact that sometimes people will post hundreds (not exaggerating here) of lines of code. Some times there are 3 or 4 files worth of code in the “question” body and then these lines get repeated in the answers section as well. This makes it almost impossible to know what is going on.

  4. I wrote a jQuery plugin that will allow us to view the code blocks in plain text in a popup window. I have never written a full jquery plugin and I was one of the ones complaining about the code/pre blocks. Check it out.

    Demo: http://jlconsulting.co/jqueryplugins/wpqplaintext/demo/

    Here is the source code: https://gist.github.com/1022448

    Let me know what you think.

  5. Lawrence Krubner says:

    Michael,

    I know, it drives me crazy as well.

  6. Lawrence Krubner says:

    Julian,

    That is great! I was thinking of attempting something similar, but I never managed to find the time.

    What is your PayPal? I want to send you some money for this.

    — lawrence

  7. Lawrence Krubner says:

    Michael,

    I like the idea of the syntax highlighter too. I will give that a try, if I can use it without conflicting with Julian’s plugin.

    — lawrence

  8. My paypal is julian[dot]lannigan[at]gmail

    Thanks

  9. darrinb says:

    I’m not sure if this will help, but I wrote a plugin a while back that handles code encoding and multiple lines. It works similarly to what WP uses on the WP.org forums.

    Here’s the link: http://darrinb.com/notes/2010/wp-post-encode/

  10. Lawrence Krubner says:

    Thank, you Darrin, but we do not use WordPress to run our sites, so it might take us some effort to translate your code. I think Julian’s code might be easier for us to implement.

Leave a Reply