etbe - Russell Coker

10 Jun

putting HTML codes and other special characters into a blog entry

I often want to write blog posts about HTML code and about source code in various languages. One problem I have is that the characters I want to use have special meanings (EG < and >), another is that I indent source code to make it readable and I don’t want the spaces trimmed from the start of lines.

I initially wrote a simple Perl script to replace characters such as < with HTML codes. I then had to extend it to escaping quote characters because Wordpress tries to get smart and change quotes in a way that might look nice when dealing with plain text, but is just a pain when dealing with code.

The next problem I had is that when I used the <PRE> tag around some text to preserve the white-space Wordpress would double-space the text (IE insert a blank line between every two lines of code). This was annoying when reading it and in some situations would change the meaning of the code! The solution I have found to these problems is to use the below script and not use the <PRE> tag. Also I tried using the <CODE> tag, but it made no difference to the end result as far as I could see.

The below script is what I am currently using. It is working well with shell scripts, HTML, and XML so far.

Update: The way that -- is munged by Wordpress to is something that I find particularly annoying. I already had this in the script but forgot to mention it in the post.

#!/usr/bin/perl

while(<STDIN>)
{
  s/&/&amp;/g;
  s/</&lt;/g;
  s/>/&gt;/g;
  s/"/&#34;/g;
  s/'/&#39;/g;
  s/`/&#96;/g;
  s/--/&#45;-/g;
  s/  /&nbsp; /g;
  print;
}

4 Responses to “putting HTML codes and other special characters into a blog entry”

  1. 1
    Julien Says:

    Hi,

    I use wp-syntax plugin on my website for displaying code. and I am very happy with it.
    I have applied a small patch to allow use of the <PRE>h; without specifying language:

    — wp-content/plugins/wp-syntax.php.orig 2007-04-13 21:59:16.000000000 +0200
    +++ wp-content/plugins/wp-syntax.php 2007-04-13 21:59:32.000000000 +0200
    @@ -100,7 +100,7 @@
    function wp_syntax_before_filter($content)
    {
    return preg_replace_callback(
    - “/\s*(.*)\s*/siU”,
    + “/\s*(.*)\s*/siU”,
    “wp_syntax_substitute”,
    $content
    );

    You can see some examples on my website.

    Cheers,
    Julien

  2. 2
    Julien Says:

    hmm… the patch is available on http://kirya.net/~julien/wp-syntax.diff as it looks quite strange in my previous comment ;-)

  3. 3
    therning.org/ magnus » Blog Archive » Russel: putting HTML/XML in blog entries Says:

    [...] Russel, I’m glad I’m using WP’s markdown plugin. I’ve never experienced any of the problems you are having relating to <pre> tags or <code>. It just works! [...]

  4. 4
    javascript Says:

    html special characters —
    http://html-lesson.blogspot.com/2008/06/special-characters.html

Leave a Reply

© 2008 etbe - Russell Coker | Entries (RSS) and Comments (RSS)

wordpress logo
Close
E-mail It