Jump to:

17452 Posts in 4473 Topics by 1971 members

Archive

SilverStripe Forums » Archive » why is the blog summary paragraph injecting line breaks?

Our old forums are still available as a read-only archive.

Moderators: martimiz, Sean, biapar, Willr, Ingo, simon_w

Page: 1 2
Go to End
Author Topic: 5378 Views
  • iandouglas736
    Avatar
    Community Member
    8 Posts

    why is the blog summary paragraph injecting line breaks? Link to this post

    Hey all, happy new year.

    I've been pouring through SilverStripe's code for a couple of hours tonight, trying to figure out where/why this is happening, and so far coming up short:

    It appears that whatever pulls the first paragraph of a blog entry is also injecting line breaks ("<br />") every 50-60 characters before echoing it to the screen. I've been trying a series of grep searches trying to determine where/why this is occurring in the code, but not having much luck yet.

    At first glance, it appears that whatever is building $ParagraphSummary within /blog/Includes/BlogSummary.ss is the culprit. I've been trying to work backwards through the code to see where it's pulling the blog content and determining what the "first paragraph" content is, and going from there, and thought maybe someone would already know where this is happening.

    In themes where the blog summary page gives you more than 350px to show your summary paragraphs, this injection really messes up your layout. Viewing the full blog article obviously doesn't inject these line breaks, so I'm curious (a) why this was done for the summary only, and (b) where to "fix" this.

    If I figure it out before someone can point me in the right direction, I'll post a follow-up.

    Thanks,
    Ian

  • iandouglas736
    Avatar
    Community Member
    8 Posts

    Re: why is the blog summary paragraph injecting line breaks? Link to this post

    Found it...

    blog/code/BlogEntry.php contains a function called "ParagraphSummary()" which calls $content->FirstParagraph() as a parameter to BBCodeParser() on line 88.

    sapphire/core/model/fieldtypes/Text.php contains the FirstParagraph() method and calls Convert::xml2raw() at line 166.

    sapphire/core/Convert.php contains the xml2raw() method, which passes the blog content's text (which was already converted through a raw2xml() call in the ParagraphSummary() code) back through to html2raw($val), passing a single parameter (just the text).

    The html2raw() method within the Convert.php script has a $wordWrap parameter defaulting to 60 characters ... which is passed to the native PHP function called wordwrap(). So this is the culprit, since the xml2raw() call doesn't pass any other parameters to html2raw().

    Any thoughts on "fixing" this?

  • redking
    Avatar
    43 Posts

    Re: why is the blog summary paragraph injecting line breaks? Link to this post

    Hey iandouglas736,

    I am having the same problem with line breaks occurring with the blog summary.
    Were you able to modify the html2raw() method at all to prevent this from happening?
    If so, please share.

    Also, did you by chance have the problem of not being able to use the editor to format blog postilngs? For some reason I am experiencing this problem. It only allows me to use BBCode to format.

    Thanks!

  • iandouglas736
    Avatar
    Community Member
    8 Posts

    Re: why is the blog summary paragraph injecting line breaks? Link to this post

    My 'fix' is kind of an ugly hack, but works great:

    core/Convert.php, around line 81:
    change

    static function xml2raw($val) {

    to

    static function xml2raw($val,$blogsummaryhack=false) {

    Then around line 90, I changed

    return self::html2raw($val);

    to this:

    if ($blogsummaryhack == true) {
    return self::html2raw($val,false,1000) ;
    } else {
    return self::html2raw($val);
    }

    Then in sapphire/core/model/fieldtypes/Text.php around line 162, you should find the FirstParagraph() method, where I changed line 166 from

    $data = Convert::xml2raw( $this->value);

    to

    $data = Convert::xml2raw( $this->value, true );

    The usual disclaimers apply: this worked for me, it may totally trash your setup, so use at your own risk.

    I'm sure there's a more elegant way to determine if the FirstParagraph method is being called by the blog plugin and handle it a little nicer, but this worked for me.

  • Scott_001
    Avatar
    Community Member
    1 Post

    Re: why is the blog summary paragraph injecting line breaks? Link to this post

    Firstly, thanks for the fix, Ian. It works great.

    This problem seems to occur when using BBCode in the first paragraph, as a long link, for example, can be enough to push the line length over the 60 character limit.

    Is there any word on an official fix? The reason I ask is because when posting an offending article on the demo site, the problem doesn't occur. However, when publishing the same text on my own blog (even running 2.2.2rc2) the issue still exists.

  • Hamish
    Avatar
    Community Member
    712 Posts

    Re: why is the blog summary paragraph injecting line breaks? Link to this post

    BUMP

    This still isn't officially fixed. I put my own hack into Track:

    http://open.silverstripe.com/ticket/2217#comment:1

    In BlogEntry.php change this:

    function ParagraphSummary(){

       $content = new Text('Content');
       $content->value = Convert::raw2xml($this->Content);
       $parser = new BBCodeParser($content->FirstParagraph());
       return $parser->parse();

    }

    to:

    function ParagraphSummary(){

       $content = new Text('Content');
       $content->value = Convert::raw2xml($this->Content);
       $firstParagraph = preg_replace( '/((\n)|(\r))/', '', $content->FirstParagraph());
       $parser = new BBCodeParser($firstParagraph);
       return $parser->parse();

    }

    Seriously, this must be one of the more popular modules and it's broken...

  • Hamish
    Avatar
    Community Member
    712 Posts

    Re: why is the blog summary paragraph injecting line breaks? Link to this post

    Actually, looking at the trunk - looks like alot of work has gone into the blog module many months ago that hasn't yet made a release... any idea when that's happening?

  • bennybtl
    Avatar
    Community Member
    12 Posts

    Re: why is the blog summary paragraph injecting line breaks? Link to this post

    thanks for the fix Hamish. You're missing a space on the replace, though (or am I wrong):

    preg_replace( '/((\n)|(\r))/', 'SPACE', $content->FirstParagraph());

    should be:

    function ParagraphSummary(){

    $content = new Text('Content');
    $content->value = Convert::raw2xml($this->Content);
    $firstParagraph = preg_replace( '/((\n)|(\r))/', ' ', $content->FirstParagraph());
    $parser = new BBCodeParser($firstParagraph);
    return $parser->parse();

    }

    5378 Views
Page: 1 2
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.