2007-06-23 18:50:41 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
require_once 'HTMLPurifier/Injector.php';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Injector that auto paragraphs text in the root node based on
|
|
|
|
* double-spacing.
|
|
|
|
*/
|
|
|
|
class HTMLPurifier_Injector_AutoParagraph extends HTMLPurifier_Injector
|
|
|
|
{
|
|
|
|
|
|
|
|
function handleText(&$token, $config, &$context) {
|
|
|
|
$current_nesting =& $context->get('CurrentNesting');
|
2007-06-23 19:39:03 +00:00
|
|
|
$text = $token->data;
|
|
|
|
// $token is the focus: if processing is needed, it gets
|
|
|
|
// turned into an array of tokens that will replace the
|
|
|
|
// original token
|
2007-06-23 18:50:41 +00:00
|
|
|
if (empty($current_nesting)) {
|
|
|
|
// we're in root node, great time to start a paragraph
|
|
|
|
// since we're also dealing with a text node
|
2007-06-23 19:39:03 +00:00
|
|
|
$token = array(new HTMLPurifier_Token_Start('p'));
|
|
|
|
$this->_splitText($text, $token, $config, $context);
|
|
|
|
} elseif ($current_nesting[count($current_nesting)-1]->name == 'p') {
|
|
|
|
// we're not in root node but we're in a paragraph, so don't
|
|
|
|
// add a paragraph start tag but still perform processing
|
|
|
|
$token = array();
|
|
|
|
$this->_splitText($text, $token, $config, $context);
|
2007-06-23 18:50:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-06-23 18:58:51 +00:00
|
|
|
function handleStart(&$token, $config, &$context) {
|
|
|
|
// check if we're inside a tag already, if so, don't add
|
|
|
|
// paragraph tags
|
|
|
|
$current_nesting = $context->get('CurrentNesting');
|
|
|
|
if (!empty($current_nesting)) return;
|
|
|
|
|
|
|
|
// check if the start tag counts as a "block" element
|
2007-06-23 19:39:03 +00:00
|
|
|
if (!$this->_isInline($token, $config)) return;
|
2007-06-23 18:58:51 +00:00
|
|
|
|
|
|
|
// append a paragraph tag before the token
|
|
|
|
$token = array(new HTMLPurifier_Token_Start('p'), $token);
|
|
|
|
}
|
|
|
|
|
2007-06-23 18:50:41 +00:00
|
|
|
/**
|
2007-06-23 19:39:03 +00:00
|
|
|
* Splits up a text in paragraph tokens and appends them
|
|
|
|
* to the result stream that will replace the original
|
|
|
|
* @param $data String text data that will be processed
|
|
|
|
* into paragraphs
|
|
|
|
* @param $result Reference to array of tokens that the
|
|
|
|
* tags will be appended onto
|
|
|
|
* @param $config Instance of HTMLPurifier_Config
|
|
|
|
* @param $context Instance of HTMLPurifier_Context
|
|
|
|
* @private
|
2007-06-23 18:50:41 +00:00
|
|
|
*/
|
2007-06-23 19:39:03 +00:00
|
|
|
function _splitText($data, &$result, $config, &$context) {
|
|
|
|
$raw_paragraphs = explode(PHP_EOL . PHP_EOL, $data);
|
2007-06-23 18:50:41 +00:00
|
|
|
|
|
|
|
// remove empty paragraphs
|
|
|
|
$paragraphs = array();
|
|
|
|
foreach ($raw_paragraphs as $par) {
|
|
|
|
if (trim($par) !== '') $paragraphs[] = $par;
|
|
|
|
}
|
|
|
|
|
2007-06-23 19:39:03 +00:00
|
|
|
// check if there are no "real" paragraphs to be processed
|
2007-06-23 18:50:41 +00:00
|
|
|
if (empty($paragraphs) && count($raw_paragraphs) > 1) {
|
|
|
|
$result[] = new HTMLPurifier_Token_End('p');
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2007-06-23 19:39:03 +00:00
|
|
|
// append the paragraphs onto the result
|
|
|
|
foreach ($paragraphs as $par) {
|
|
|
|
$result[] = new HTMLPurifier_Token_Text($par);
|
2007-06-23 18:50:41 +00:00
|
|
|
$result[] = new HTMLPurifier_Token_End('p');
|
|
|
|
$result[] = new HTMLPurifier_Token_Start('p');
|
|
|
|
}
|
|
|
|
array_pop($result); // remove trailing start token
|
|
|
|
|
2007-06-23 19:39:03 +00:00
|
|
|
// check the outside to determine whether or not the
|
|
|
|
// end paragraph tag should be removed
|
|
|
|
if ($this->_removeParagraphEnd($config, $context)) {
|
2007-06-23 18:50:41 +00:00
|
|
|
array_pop($result);
|
|
|
|
}
|
|
|
|
|
2007-06-23 19:39:03 +00:00
|
|
|
|
2007-06-23 18:50:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2007-06-23 19:39:03 +00:00
|
|
|
* Returns boolean whether or not to remove the paragraph end tag
|
|
|
|
* that was automatically added. The paragraph end tag should be
|
|
|
|
* removed unless the next token is a paragraph or block element.
|
|
|
|
* @param $config Instance of HTMLPurifier_Config
|
|
|
|
* @param $context Instance of HTMLPurifier_Context
|
|
|
|
* @private
|
2007-06-23 18:50:41 +00:00
|
|
|
*/
|
2007-06-23 19:39:03 +00:00
|
|
|
function _removeParagraphEnd($config, &$context) {
|
|
|
|
$tokens = $context->get('InputTokens');
|
|
|
|
$i = $context->get('InputIndex');
|
|
|
|
$remove_paragraph_end = true;
|
|
|
|
// Start of the checks one after the current token's index
|
|
|
|
for ($i++; isset($tokens[$i]); $i++) {
|
|
|
|
if ($tokens[$i]->type == 'start' || $tokens[$i]->type == 'empty') {
|
|
|
|
$definition = $config->getHTMLDefinition();
|
|
|
|
$remove_paragraph_end = $this->_isInline($tokens[$i], $config);
|
2007-06-23 18:50:41 +00:00
|
|
|
break;
|
|
|
|
}
|
2007-06-23 19:39:03 +00:00
|
|
|
// check if we can abort early (whitespace means we carry-on!)
|
|
|
|
if ($tokens[$i]->type == 'text' && !$tokens[$i]->is_whitespace) break;
|
|
|
|
if ($tokens[$i]->type == 'end') break; // nonsensical
|
2007-06-23 18:50:41 +00:00
|
|
|
}
|
2007-06-23 19:39:03 +00:00
|
|
|
return $remove_paragraph_end;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns true if passed token is inline (and, ergo, allowed in
|
|
|
|
* paragraph tags)
|
|
|
|
*/
|
|
|
|
function _isInline($token, $config) {
|
|
|
|
$definition = $config->getHTMLDefinition();
|
2007-06-23 20:52:57 +00:00
|
|
|
return isset($definition->info['p']->child->elements[$token->name]);
|
2007-06-23 18:50:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
?>
|