Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Try Vanilla Forums Cloud product

Ready to contribute?

Amazing! Sign our contributors' agreement and then join us on GitHub.

Update for critical security issue in PHPMailer included in release Vanilla 2.3.1
Vanilla 1 is no longer supported or maintained. If you need a copy, you can get it here.


This discussion is related to the AutoP addon.


  • AutoP is still messing with PRE and CODE content, so I'm trying to fix it up properly, however, with the code below I get a PHP error complaining about undefined functions. My limited PHP tells me the function is defined, but I'm obviously wrong, so wuld someone with an ounce (or more!) of skill, please tell me where I'm being a muppet? Thanks.

    <?php /* Extension Name: AutoP Extension Url: Description: Automatically formats paragraphs and new lines using valid XHTML. Version: 1.0.4 Author: Luke "[-Stash-]" Scammell Author Url: */ class AutoPFormatter extends StringFormatter { function Parse($String, $Object, $FormatPurpose) { if ($FormatPurpose == FORMAT_STRING_FOR_DISPLAY) { $String = $this->wpautop($String); $String = $this->AutoPFix($String); } return $String; } function clean_pre($text) { $text = str_replace('<br />', '', $text); $text = str_replace('<p>', "\n", $text); $text = str_replace('</p>', '', $text); return $text; } function wpautop($pee, $br = 1) { $pee = $pee . "\n"; // just to make things a little easier, pad the end $pee = preg_replace('|<br />\s*<br />|', "\n\n", $pee); // Space things out a little $allblocks = '(?:table|thead|tfoot|caption|colgroup|tbody|tr|td|th|div|dl|dd|dt|ul|ol|li|pre|code|select|form|map|area|blockquote|cite|address|math|style|script|object|embed|input|param|p|h[1-6])'; $pee = preg_replace('!(<' . $allblocks . '[^>]*>)!', "\n$1", $pee); $pee = preg_replace('!(</' . $allblocks . '>)!', "$1\n\n", $pee); $pee = str_replace(array("\r\n", "\r"), "\n", $pee); // cross-platform newlines $pee = preg_replace("/\n\n+/", "\n\n", $pee); // take care of duplicates $pee = preg_replace('/\n?(.+?)(?:\n\s*\n|\z)/s', "<p>$1</p>\n", $pee); // make paragraphs, including one at the end $pee = preg_replace('|<p>\s*?</p>|', '', $pee); // under certain strange conditions it could create a P of entirely whitespace $pee = preg_replace('!<p>([^<]+)\s*?(</(?:div|address|form)[^>]*>)!', "<p>$1</p>$2", $pee); $pee = preg_replace( '|<p>|', "$1<p>", $pee ); $pee = preg_replace('!<p>\s*(</?' . $allblocks . '[^>]*>)\s*</p>!', "$1", $pee); // don't pee all over a tag $pee = preg_replace("|<p>(<li.+?)</p>|", "$1", $pee); // problem with nested lists $pee = preg_replace('|<p><blockquote([^>]*)>|i', "<blockquote$1><p>", $pee); $pee = str_replace('</p></div></blockquote></p>', '</p></p></div></blockquote>', $pee); $pee = preg_replace('!<p>\s*(</?' . $allblocks . '[^>]*>)!', "$1", $pee); $pee = preg_replace('!(</?' . $allblocks . '[^>]*>)\s*</p>!', "$1", $pee); if ($br) { $pee = preg_replace('/<(script|style).*?<\/\\1>/se', 'str_replace("\n", "<WPPreserveNewline />", "\\0")', $pee); $pee = preg_replace('|(?<!<br />)\s*\n|', "<br />\n", $pee); // optionally make line breaks $pee = str_replace('<WPPreserveNewline />', "\n", $pee); } $pee = preg_replace('!(</?' . $allblocks . '[^>]*>)\s*<br />!', "$1", $pee); $pee = preg_replace('!<br />(\s*</?(?:p|li|div|dl|dd|dt|th|pre|code|cite|td|ul|ol)[^>]*>)!', '$1', $pee); if (strpos($pee, '<pre') !== false) $pee = preg_replace('!(<pre.*?>)(.*?)</pre>!ise', " stripslashes('$1') . stripslashes(clean_pre('$2')) . '</pre>' ", $pee); $pee = preg_replace( "|\n</p>$|", '</p>', $pee ); return $pee; } // These fix the few instances in Vanilla that AutoP corrupts. function AutoPFix ($String) { $String = str_replace( array( '<blockquote><p><cite>', '</cite><br />' ), array( '<blockquote><cite>', '<cite>' ), $String); return $String; } } // Instantiate the formatter and add it to the context object's string manipulator $AutoPFormatter = $Context->ObjectFactory->NewObject($Context, "AutoPFormatter"); $Context->StringManipulator->AddGlobalManipulator("AutoPFormatter", $AutoPFormatter); ?>
  • Bug here: if (strpos($pee, '<pre') !== false) $pee = preg_replace('!(<pre.*?>)(.*?)</pre>!ise', " stripslashes('$1') . stripslashes(clean_pre('$2')) . '</pre>' ", $pee);

    Your quotes are off. Single quotes are evaluated as literal strings, double quotes will insert the variable in the string. This will probably work better: if (strpos($pee, '<pre')) $pee = preg_replace('!(<pre.*?>)(.*?)</pre>!ise', stripslashes("$1") . stripslashes(clean_pre("$2")) . '</pre>', $pee);
    You could probably even do just $1 instead of "$1", but I will leave them in for consistency sake.
  • MarkMark Vanilla Staff
    carefully crafted HTML gets d to death

    I have no idea what that means or what this extension does.
  • I guess its not a total fix--I need to brush up on my regexps.

    Also note that if you have a commented line with a ?> contained in it... php reads it as an end of the PHP execution. Spent about an hour puzzling over that one.

    Mark: HTML characters got parsed out. I guess this extension is a more semantic line breaker--at a basic level its <p></p> instead of <br />'s
  • Awesome plugin for us SEO freaks!

    FYI here's a pseudo-fix for using this with PreviewPost:

    Add to your vanilla.css:
    #CommentBody_Preview br { display: none; } #CommentBody_Preview p br { display: block; }

    It basically hides any extraneous breaks that PreviewPost generates.
  • Stanzi1791
    The problem I'm having with AutoP is that it adds <p> around the content of a quote like this:
    (Don't pay attention to the content of the quotes, I had to quote something.. ;))

    Without those extra <p>s it looks like this:

    So what I meant, is that the added <p>s add extra white space to the top and bottom of the [quote] block, which I don't really like. It's not really a big problem, but wouldn't it be easier to not have to install another extension to fix the new lines? I know there's a function in php called nl2br() which converts new lines to breaks, maybe that could be of some help?

    Your [bbcode] tag idea sounds good, but if you were to implement that, what would be the use of the [code] tag? Wouldn't it just be similar to the [quote] tag then?
    Hmm, that extra white space around quotes... Lemme have a think on that. There are ways of fixing it, but basically it's caused by the CITE tag being changed to a BLOCK instead of an INLINE element, which it is by default in HTML.

    I just wish I could figure out why PHP thinks the clean_pre function's undefined - bah!
  • I've fixed it - I was using a [b] instead of a [cite] tag so that was causing the problem.
    Sorry for the trouble, it's working fine now. :)
  • With some amazing help from AlexL I now have a working version of this extension where CODE and PRE tags retain their correct spacing! Please feel free to bug me a little in a couple of days if I haven't released anything by then.

    Thanks ever-so-much AlexL :D
  • Version number changed from 1.0.3 to v5.
  • Uploaded version v5 of AutoP.
  • When a comment is postet, you can only select one

    with your cursor (ff2 win xp sp2). This is really strange, is there a way to fix it?

  • I'm not sure I understand the problem. Does this go away when you turn AutoP off? I've got no strange selection behaviour on my XP SP2 with fx2, so I'll need a little more info I'm afraid.
  • uuuh the <p> didn't appear in my las comment

    well, that appeared when you tried to select more than one paragraph, you couldn't select the nexr one. I'll try it again this evening an make a video or something
  • If this happens this evening, along with the video, please will you send me (whispered privately if you need to) a URL so I can see if I can reproduce it myself? Thanks.
  • okay. took some time until now. the error mentioned before doesn't appear anymore :)
  • Was probably fixed in the last update and some strange caching was going on... anyway, I'm glad it's fixed for you now :)
  • I found the rendering in IE 6 gets screwed up with this turned on. It seems to shift everything to the left slightly, so much that it cuts out entire words. I've got long page with 60 threads on it, and it only starts noticing towards the bottom.


    I now have a thread with 2 posts on it that are unhappy. Quotes seem to be unhappy. I'm happy to let anyone look!
  • Spode: that leftward shift is called magik creeping text:
  • Yeah that fix seems to sort it out thanks WallPhone. Any ideas why it wasn't occurring without AutoP though? I'm not sure what AutoP would be doing to affect it.
  • I was going to look into that, but unfortunately my development machine developed a case of a blown power supply...

    I was able to see it happening on Spode's blog, but hadn't yet started to track it down.
  • Just looking Spode's stylesheet code, it appears this update would fix Spode's issue also. I think it had something to do with what was contained in the quote blocks, since the testing thread only showed the issue when there was an unordered list inside. I expect that something similar happens when it has P tags instead of BRs.
  • Interesting (but irritating). Since the fix is effectively a holly hack, would it not be wise to add it to an IE conditional comment?
  • It should be OK on its own... I'm not aware of any side-effects of the holly hack on other browsers, but I'll make sure when my new power brick arrives tomorrow.
This discussion has been closed.