Postie 1.4.2 Stripping Leading Whitespace from Markdown Posts

Update – This still applies to Postie 1.4.3 released 12/12/2011.

Recently I’ve been trying to reconcile the best way of adding and maintaining content on my blog, partly with a view to how my clients are able to maintain there own content. The state of WYSIWYG editors for websites is truely appalling, unless I’m massively missing something that is out there. I’ve yet to come across anything that doesn’t completely mess up anything but the most trivial markup, or trivialise the editing capabilities to the point of being pointless.

One solution is to prevent the editor from using any sort of fancy controls whilst still allowing the user to concentrate on quality markup. One way to achieve this is to use Markdown. This does away with WYSIWYG editing, and provides the user with a plain text editor. Formatting is added through plain text conventions, such as putting # in front of headings, and * asterisks in front of list items. The sort of thing we used to do before WYSIWYG editors event existed.

I also use the Postie plugin for WordPress so that I can easily send posts to the blog when I’m out and about, or for that matter lying in bed late at night and wanting to post something. In fact, this post is being written in Writer for Mac which is great for just getting on with writing, and supports highlighting of certain Markdown syntax.

The problem I’ve discovered is that Postie 1.4.2 (current release at time of writing) is stripping leading whitespace from lines in the emails that it processes. I couldn’t initially work out if it was Postie, or part of WordPresses own text filtering that was the culprit, but after enabling Debug in Postie, and working through the code, I found a bug in the remove_signature() function. This is supposed to identify embedded signatures in emails and strip anything that comes after, but in the process of looking for the signature, it strips whitespace from the start of all lines in the email. As a result, code blocks in Markdown (normally indented by 4 spaces or a tab) are no longer indented and as a result don’t get recognised by the Markdown processor.

I’ve included the original and fixed versions of the function below. The offending trim() when assigning to $line looks like it was left over from a previous detection method, and is in effect repeated in the preg_match() call, so no longer needed. Because the trimmed line was then put back into the revised content, it was stripped out erroneously.

I’ve mailed Rob Felty with the details so hopefully he’ll include it in future releases.

/* Original Function */

function remove_signature( $content, $filterList ) {
    if (empty($filterList))
        return $content;
    $arrcontent = explode("n", $content);
    $strcontent = '';
    $pattern='/^('  . implode( '|', $filterList ) . ')/';
    for ($i = 0; $i<=count($arrcontent); $i++) {
        $line = trim($arrcontent[$i]);
        $nextline = $arrcontent[$i+1];
        if (preg_match($pattern,trim($line))) {
            //if (!strpos(trim($line), $pattern)==0) {
            //print("<p>Found in $line");
            break;
        }
        $strcontent .= $line ."n";
    }
    return $strcontent;
}

/* Fixed Function */

function remove_signature( $content, $filterList ) {
    if (empty($filterList))
        return $content;
    $arrcontent = explode("n", $content);
    $strcontent = '';
    $pattern='/^('  . implode( '|', $filterList ) . ')/';
    for ($i = 0; $i<=count($arrcontent); $i++) {
        $line = $arrcontent[$i];
        if (preg_match($pattern,trim($line))) {
            break;
        }
        $strcontent .= $line ."n";
    }
    return $strcontent;
}




4 thoughts on “Postie 1.4.2 Stripping Leading Whitespace from Markdown Posts

  1. Johnny

    Hi, I appreciate your effort with postie. I am trying out your code fix. I had a question also for you. How do you have postie configured with your WP site? For me it seems to be activating a lot of wp-cron processes thereby going over my useage limit.

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *