I've been using b2evolution for a couple of months now, and one of the things that has been bugging me is that when I type two spaces after the end of a sentence (like my typing teacher way back in Jr. High told me to do), only one space shows up when the page is rendered, due to the way HTML handles whitespace. So, I wrote a plugin to render the first of two consecutive spaces at the end of a sentence (after a period, question mark, or exclamation) as a non-breaking space character in HTML ( ). The reason I'm only converting the first one is that non-breaking spaces, by definition, do not allow for line wrapping, so having one non-breaking space and one regular space allows the line to wrap normally if necessary.
Just add the following code to a file called "_doublespace.renderer.php" and stick it in your /plugins/renderers folder
<?php
/**
* This file formats double spaces correctly for b2evolution
* It is simply a modified version of the supplied auto_p renderer
*
* @author Kweb
*
* @package plugins
*/
if( !defined('DB_USER') ) die( 'Please, do not access this page directly.' );
/**
* Includes:
*/
require_once dirname(__FILE__).'/../renderer.class.php';
/**
* @package plugins
*/
class doublespace_Rendererplugin extends RendererPlugin
{
var $code = 'b2DoubleSpace';
var $name = 'Double Space';
var $priority = 90;
var $apply_when = 'opt-out';
var $apply_to_html = true;
var $apply_to_xml = false;
var $short_desc;
var $long_desc;
/**
* Constructor
*
* {@internal doublespace_Rendererplugin::doublespace_Rendererplugin(-)}}
*/
function doublespace_Rendererplugin()
{
$this->short_desc = T_('Automatic conversion of double white spaces to &nbsp;');
$this->long_desc = T_('No description available');
}
/**
* Perform rendering
*
* {@internal auto_p_Rendererplugin::render(-)}}
*
* @param string content to render (by reference) / rendered content
* @param string Output format, see {@link format_to_output()}
* @return boolean true if we can render something for the required output format
*/
function render( & $content, $format )
{
if( ! parent::render( $content, $format ) )
{ // We cannot render the required format
return false;
}
// REPLACE: But not in code blocks.
if( strpos( $content , '<pre>' ) !== false )
{ // If there are code tags run this substitution
$content_parts = preg_split("/<\/?pre>/", $content);
$content = '';
for ( $x = 0 ; $x < count( $content_parts ) ; $x++ )
{
if ( ( $x % 2 ) == 0 )
{ // If x is even then it's not code and replace any smiles
$content .= $this->doublespace( $content_parts[$x] );
}
else
{ // If x is odd don't replace smiles. and put code tags back in.
$content .= '<pre>' . $content_parts[$x] . '</pre>';
}
}
}
else
{ // No code blocks, replace on the whole thing
$content = $this->doublespace( $content );
}
return true;
}
function doublespace( $text )
{
$text = str_replace('. ', '. ', $text);
$text = str_replace('! ', '! ', $text);
$text = str_replace('? ', '? ', $text);
return $text;
}
}
// Register the plugin:
$this->register( new doublespace_Rendererplugin() );
?>
Note that this must be a higher priority than the "smilies" renderer or that one will convert your semi-colons first, and the non-breaking spaces will show up as incomplete HTML code with smiley at the end of it (not pretty).
Comments are welcome - especially if there is some glaring problem I have not considered. So far, it workes pretty well on my blog.
hmm, very nice idea, something Ive never seen done before!
I too like to see 2 spaces between the end of one sentence and the beginning of another. I only wish you didnt have to use since being chars they are highlightable.