Recent Topics

1 Mar 15, 2005 21:47    

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 &amp;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('.  ', '.&nbsp; ', $text);
		$text = str_replace('!  ', '!&nbsp; ', $text);
		$text = str_replace('?  ', '?&nbsp; ', $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.

2 Mar 15, 2005 22:33

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 &nbsp; since being chars they are highlightable.

3 Mar 15, 2005 23:04

I first tried just using the CSS white-space property to cause it to display all spaces individually (not collapse them), but any value other than the default of "normal" will prevent line wrapping, which of course kills your layout. Maybe some future CSS spec will allow for simultaneous line wrapping and proper multi-space display. Until then, the only option is to use &nbsp;.

PS - Many programs, including this BB system, have the same problem of improper handling of consecutive spaces. I'm glad I'm not the only person it bugs.


Form is loading...

CMS + forums – This forum is powered by b2evolution CMS, a complete engine for your website.