Wrapping Text Output In Paragraph Tags

Discussion forum for MOD Writers regarding MOD Development.
motumbo
Registered User
Posts: 20
Joined: Wed Aug 08, 2007 2:48 pm

Wrapping Text Output In Paragraph Tags

Post by motumbo »

I'm new to PHPBB3. I was surprised to see that the text output in posts isn't wrapped in paragraph tags. Paragraphs are faked using line breaks. Semantic markup dictates that paragraphs should be wrapped in paragraph tags. I know many forums use line breaks to fake paragraphs. But I don't like them. It's a peeve of mine.

So, I was wondering how I could best go about modifying PHPBB 3 to wrap post text output in paragraph tags. Would it be best to wrap the text in paragraph tags before it is first inserted into the database (meaning that some HTML <p></p> would end up in the DB)? Or would it be best to do a preg_replace on newlines when the data is coming out of the database for display in the post? This can be done. But I'm not sure about what surprises may be sitting in the DB (like tables, for instance).

And once the best method is determined, would anyone know which files I would need to modify? It would save me a lot of time if someone knew and told me.

Thanks for your help and advice.
User avatar
Brf
Support Team Member
Support Team Member
Posts: 53599
Joined: Tue May 10, 2005 7:47 pm
Location: {postrow.POSTER_FROM}

Re: Wrapping Text Output In Paragraph Tags

Post by Brf »

The data is stored in the database as line-breaks, which are replaced with <br />'s when output. You could probably replace them with </p><p> if that is what you want.
motumbo
Registered User
Posts: 20
Joined: Wed Aug 08, 2007 2:48 pm

Re: Wrapping Text Output In Paragraph Tags

Post by motumbo »

I was thinking something along the lines of this:

Code: Select all

<?php

$text = "

This is the first paragraph. The goal of this script is to wrap all paragraphs in HTML paragraph tags instead of faking them using linebreaks.  Semantic markup dictates that paragraph tags be used to represent paragraphs.  The other benefit is that when text is wrapped in paragraph tags, it makes it possible to style them, if desired.  Margins and padding can be applied to give your paragraphs the exact look you desire them to have.

The nice feature about this script is that it preserves single newlines and converts them to html linebreak tags (br).
Sometimes you want a newline within a paragraph like this.
Sometimes you don't.
This preserves newlines while wrapping text ending with double newlines in paragraph tags.
Here is another newline within the same paragraph.  Multiple newlines follow this to form a new paragraph.








Of course, I found this script and just modified it for my use.  I am no expert in PHP or phpBB3, so I am not sure if I will run into other problems.  That is why I am asking the nice people at the phpbb.com/community if something like this could be used in phpBB3 to give the paragraphs the semantic markup they deserve.  

So, if there is anybody out there reading this who is knowledgeable about phpBB3, perhaps you could take a few seconds to look at this script and tell me if there are any reasons why this would not work in phpBB3.  phpBB3 is a complex piece of software with thousands of lines of code.  It would take many hours for someone like me to find out where this code should be placed in phpBB3 and spot any potential conflicts.



Thank you for your help.

Sincerely,

motumbo

";

// first, lets trim starting/trailing whitespace

$text = trim($text);

// temporarily replace two or more consecutive newlines
// into SOH characters (not used in normal text)

$text = preg_replace('~(\r\n|\n){2,}|$~', "\001", $text);

// convert remaining (i.e. single) newlines into html br's

$text = nl2br($text);

// finally, replace SOH chars with paragraphs

$text = preg_replace('/(.*?)\001/s', '<p class="post_paragraph">$1</p>' . "\n", $text); 

// test

//header("Content-type: text/plain");
echo '
<html> 
<body>
' .

$text .

'</body>
</html>';
?>
I tested this on Windows and Linux. It seems to work just fine. But I do not know the following:

* If there would be any possible conflicts in phpBB3
* In which phpBB3 file I would add this code
* The best manner with which to do this

It will only take a few seconds to test my code.

Any suggestions and advice are appreciated.
User avatar
Brf
Support Team Member
Support Team Member
Posts: 53599
Joined: Tue May 10, 2005 7:47 pm
Location: {postrow.POSTER_FROM}

Re: Wrapping Text Output In Paragraph Tags

Post by Brf »

I believe it is this function in functions_content.php that does it:

Code: Select all

function bbcode_nl2br($text)
{
	// custom BBCodes might contain carriage returns so they
	// are not converted into <br /> so now revert that
	$text = str_replace(array("\n", "\r"), array('<br />', "\n"), $text);
	return $text;
}
You could replace it with something like this:

Code: Select all

function bbcode_nl2br($text)
{
	// custom BBCodes might contain carriage returns so they
	// are not converted into <br /> so now revert that
	$text = '<p>' . str_replace(array("\n", "\r"), array('</p><p>', "\n"), $text) . '</p>';
	return $text;
}
motumbo
Registered User
Posts: 20
Joined: Wed Aug 08, 2007 2:48 pm

Re: Wrapping Text Output In Paragraph Tags

Post by motumbo »

Thanks for pointing me to functions_content.php. I'll bet what I need to do is in there. But I don't think it is the bbcode_nl2br() function that I need to do it in. I'm not looking to do anything with BB code. I want to take the text in the message and wrap the paragraphs in <p> tags before display.

I'm thinking I might be looking at decode_message(). Or possibly even generate_text_for_display().

But if I decide to do this before it goes in the DB, I could take a look at generate_text_for_storage().

If anyone has any suggestions, I'm all ears. Or, should I say, eyes. :D
User avatar
Brf
Support Team Member
Support Team Member
Posts: 53599
Joined: Tue May 10, 2005 7:47 pm
Location: {postrow.POSTER_FROM}

Re: Wrapping Text Output In Paragraph Tags

Post by Brf »

No. That function is not for bbcode. It is for replacing line-breaks with <br>'s.
motumbo
Registered User
Posts: 20
Joined: Wed Aug 08, 2007 2:48 pm

Re: Wrapping Text Output In Paragraph Tags

Post by motumbo »

Brf wrote:No. That function is not for bbcode. It is for replacing line-breaks with <br>'s.
Well, you know phpBB better than I do, I'm sure. The function comments say the following:
// custom BBCodes might contain carriage returns so they
// are not converted into <br /> so now revert that
That lead me to believe that it the function would only be called if there were custom BB codes that needed parsing.

I shall run my little test and report back.

Thanks.
motumbo
Registered User
Posts: 20
Joined: Wed Aug 08, 2007 2:48 pm

Re: Wrapping Text Output In Paragraph Tags

Post by motumbo »

Brf,

You were right about the function of interest being bbcode_nl2br(). I made it look like:

Code: Select all

function bbcode_nl2br($text)
{
	//  It looks like this works.  Can't put in multiple lines of whitespace, though.
	// first, lets trim starting/trailing whitespace

	$text = trim($text);
	
	// temporarily replace two or more consecutive newlines
	// into SOH characters (not used in normal text)
	
	$text = preg_replace('~(\r\n|\n){2,}|$~', "\001", $text);
	
	// convert remaining (i.e. single) newlines into html br's
	
	$text = nl2br($text);
	
	// finally, replace SOH chars with paragraphs
	
	$text = preg_replace('/(.*?)\001/s', '<p>$1</p>' . "\n", $text); 
	return $text; 
}
It looks like it does work well at wrapping the message paragraphs in <p> tags. The only problem I saw was that using this code nobody could put in multiple lines of whitespace. You know how someone will say something at the top of a post, put in a bunch of blank lines forcing you to scroll down then reveal the answer below? That wouldn't be possible with this code as all the newlines are removed. But otherwise, it looks pretty good.

Another issue has arisen: the style of the text is different. The font size is basically larger. I'll have to try to track down a solution to that.

Thanks for your help, Brf.
User avatar
Brf
Support Team Member
Support Team Member
Posts: 53599
Joined: Tue May 10, 2005 7:47 pm
Location: {postrow.POSTER_FROM}

Re: Wrapping Text Output In Paragraph Tags

Post by Brf »

motumbo wrote: Another issue has arisen: the style of the text is different. The font size is basically larger. I'll have to try to track down a solution to that.
It may be one of these in content.css

Code: Select all

.panel p {
	font-size: 1.2em;
	margin-bottom: 1em;
	line-height: 1.4em;
}

.content p {
	font-family: "Lucida Grande", "Trebuchet MS", Verdana, Helvetica, Arial, sans-serif;
	font-size: 1.2em;
	margin-bottom: 1em;
	line-height: 1.4em;
}
Yeah. I am pretty sure it is the "content" one.

Return to “[3.0.x] MOD Writers Discussion”