Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

Archive /

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

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

why is the blog summary paragraph injecting line breaks?


Go to End
Reply


11 Posts   5529 Views

Avatar
iandouglas736

Community Member, 8 Posts

2 January 2008 at 10:02pm

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

Avatar
iandouglas736

Community Member, 8 Posts

2 January 2008 at 10:56pm

Edited: 02/01/2008 10:57pm

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?

Avatar
redking

43 Posts

25 January 2008 at 11:14am

Edited: 25/01/2008 12:04pm

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!

Avatar
iandouglas736

Community Member, 8 Posts

25 January 2008 at 12:22pm

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.

Avatar
Scott_001

Community Member, 1 Post

3 April 2008 at 5:46am

Edited: 03/04/2008 5:47am

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.

Avatar
Hamish

Community Member, 712 Posts

11 July 2008 at 10:33am

BUMP

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

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

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...

Avatar
Hamish

Community Member, 712 Posts

11 July 2008 at 12:17pm

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?

Avatar
bennybtl

Community Member, 12 Posts

11 July 2008 at 3:14pm

Edited: 11/07/2008 3:14pm

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();

}

Go to Top