Skip to content

Commit

Permalink
* TexyParser - fixed creating empty TexyHtml objects
Browse files Browse the repository at this point in the history
  • Loading branch information
dg committed Aug 11, 2007
1 parent bb5a22f commit e642966
Show file tree
Hide file tree
Showing 14 changed files with 174 additions and 109 deletions.
9 changes: 9 additions & 0 deletions texy-for-php4/libs/TexyHandlerInvocation.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,15 @@ function getTexy()
}


/**
* PHP garbage collector helper
*/
function free()
{
$this->handlers = $this->parser = $this->args = NULL;
}


function TexyHandlerInvocation() /* PHP 4 constructor */
{
// generate references (see http://www.dgx.cz/trine/item/how-to-emulate-php5-object-model-in-php4)
Expand Down
6 changes: 3 additions & 3 deletions texy-for-php4/libs/TexyHtml.php
Original file line number Diff line number Diff line change
Expand Up @@ -453,13 +453,13 @@ function parseLine($texy, $s)
* Parses text as block
* @param Texy
* @param string
* @param bool
* @param TexyBlockParser
* @return void
*/
function parseBlock($texy, $s, $topLevel = FALSE)
function parseBlock($texy, $s, $parent = NULL)
{
$parser = new TexyBlockParser($texy, $this);
$parser->topLevel = $topLevel;
$parser->parentParser = $parent;
$parser->parse($s);
}

Expand Down
65 changes: 38 additions & 27 deletions texy-for-php4/libs/TexyHtmlCleaner.php
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ function cb($matches) /* private */
if ($mText !== '') {
$item = reset($this->tagStack);
// text not allowed?
if ($item && !isset($item['content']['%DATA'])) { }
if ($item && !isset($item['dtdContent']['%DATA'])) { }

// inside pre & textarea preserve spaces
elseif (!empty($this->tagUsed['pre']) || !empty($this->tagUsed['textarea']) || !empty($this->tagUsed['script']))
Expand Down Expand Up @@ -194,10 +194,8 @@ function cb($matches) /* private */
foreach ($this->tagStack as $i => $item)
{
$tag = $item['tag'];
if ($item['close']) {
$s .= $item['close'];
if (!isset($GLOBALS['TexyHtml::$inline'][$tag])) $this->space--;
}
$s .= $item['close'];
$this->space -= $item['indent'];
$this->tagUsed[$tag]--;
$back = $back && isset($GLOBALS['TexyHtml::$inline'][$tag]);
unset($this->tagStack[$i]);
Expand All @@ -209,55 +207,54 @@ function cb($matches) /* private */

// allowed-check (nejspis neni ani potreba)
$item = reset($this->tagStack);
if ($item) $content = $item['content'];
else $content = $this->baseDTD;
if (!isset($content[$tmp[0]['tag']])) return $s;
if ($item) $dtdContent = $item['dtdContent'];
else $dtdContent = $this->baseDTD;
if (!isset($dtdContent[$tmp[0]['tag']])) return $s;

// autoopen tags
foreach ($tmp as $item)
{
if ($item['close']) $s .= '<'.$item['tag'].$item['attr'].'>';
$s .= $item['open'];
$this->space += $item['indent'];
$this->tagUsed[$item['tag']]++;
array_unshift($this->tagStack, $item);
}


} else { // start tag

$content = $this->baseDTD;
$dtdContent = $this->baseDTD;

if (!isset($this->dtd[$mTag][1])) {
if (!isset($this->dtd[$mTag])) {
// unknown (non-html) tag
$allowed = TRUE;
$item = reset($this->tagStack);
if ($item) $content = $item['content'];
if ($item) $dtdContent = $item['dtdContent'];


} else {
// optional end tag closing
foreach ($this->tagStack as $i => $item)
{
// is tag allowed here?
$content = $item['content'];
if (isset($content[$mTag])) break;
$dtdContent = $item['dtdContent'];
if (isset($dtdContent[$mTag])) break;

$tag = $item['tag'];

// auto-close hidden, optional and inline tags
if ($item['close'] && (!isset($GLOBALS['TexyHtmlCleaner::$optional'][$tag]) && !isset($GLOBALS['TexyHtml::$inline'][$tag]))) break;

// close it
if ($item['close']) {
$s .= $item['close'];
if (!isset($GLOBALS['TexyHtml::$inline'][$tag])) $this->space--;
}
$s .= $item['close'];
$this->space -= $item['indent'];
$this->tagUsed[$tag]--;
unset($this->tagStack[$i]);
$content = $this->baseDTD;
$dtdContent = $this->baseDTD;
}

// is tag allowed in this content?
$allowed = isset($content[$mTag]);
$allowed = isset($dtdContent[$mTag]);

// check deep element prohibitions
if ($allowed && isset($GLOBALS['TexyHtmlCleaner::$prohibits'][$mTag])) {
Expand All @@ -284,31 +281,45 @@ function cb($matches) /* private */
return $s . '<' . $mTag . $mAttr . '>';
}

$open = NULL;
$close = NULL;
$indent = 0;

/*
if (!isset($GLOBALS['TexyHtml::$inline'][$mTag])) {
// block tags always decorate with \n
$s .= "\n";
$close = "\n";
}
*/

if ($allowed) {
$open = '<' . $mTag . $mAttr . '>';

// receive new content (ins & del are special cases)
if (!empty($this->dtd[$mTag][1])) $content = $this->dtd[$mTag][1];
if (!empty($this->dtd[$mTag][1])) $dtdContent = $this->dtd[$mTag][1];

// format output
if ($this->indent && !isset($GLOBALS['TexyHtml::$inline'][$mTag])) {
$close = "\x08" . '</'.$mTag.'>' . "\n" . str_repeat("\t", $this->space);
$s .= "\n" . str_repeat("\t", $this->space++) . '<'.$mTag.$mAttr.'>' . "\x07";
$s .= "\n" . str_repeat("\t", $this->space++) . $open . "\x07";
$indent = 1;
} else {
$close = '</'.$mTag.'>';
$s .= '<'.$mTag.$mAttr.'>';
$s .= $open;
}

// TODO: problematic formatting of select / options, object / params

} else $close = '';
}


// open tag, put to stack, increase counter
$item = array(
'tag' => $mTag,
'attr' => $mAttr,
'open' => $open,
'close' => $close,
'content' => $content,
'dtdContent' => $dtdContent,
'indent' => $indent,
);
array_unshift($this->tagStack, $item);
$tmp = &$this->tagUsed[$mTag]; $tmp++;
Expand Down
42 changes: 23 additions & 19 deletions texy-for-php4/libs/TexyParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class TexyParser
var $texy;

/** @var TexyHtml protected */
var $parent;
var $parentEl;

/** @var array */
var $patterns;
Expand Down Expand Up @@ -63,8 +63,8 @@ class TexyBlockParser extends TexyParser
/** @var int */
var $offset; /* private */

/** @var bool */
var $topLevel = FALSE;
/** @var TexyBlockParser */
var $parentParser;


/**
Expand All @@ -74,7 +74,7 @@ class TexyBlockParser extends TexyParser
function __construct(/*Texy*/ $texy, $element = NULL)
{
$this->texy = $texy;
$this->parent = $element;
$this->parentEl = $element;
$this->patterns = $texy->getBlockPatterns();
}

Expand Down Expand Up @@ -123,9 +123,9 @@ function parse($text)
$tx = $this->texy;
// pre-processing
foreach ($tx->_preBlockModules as $module)
$text = $module->preBlock($text, $this->topLevel);
foreach ($tx->_preBlockModules as $module) {
$text = $module->preBlock($text, $this->parentParser === TRUE);
}
// parser initialization
$this->text = $text;
Expand Down Expand Up @@ -179,14 +179,15 @@ function parse($text)
$this->offset = $next;
if ($tx->paragraphModule->mode)
$parts = preg_split('#(\n{2,})#', $str);
$parts = preg_split('#(\n{2,})#', $str, -1, PREG_SPLIT_NO_EMPTY);
else
$parts = preg_split('#(\n(?! )|\n{2,})#', $str);
$parts = preg_split('#(\n(?! )|\n{2,})#', $str, -1, PREG_SPLIT_NO_EMPTY);
foreach ($parts as $str)
{
$str = trim($str);
if ($str === '') continue;
// try to find modifier
$mod = new TexyModifier;
Expand All @@ -200,36 +201,39 @@ function parse($text)
$el = $tx->invokeHandlers('paragraph', $this, array($str, $mod));
if ($el) $nodes[] = $el;
}
continue;
if ($minKey === NULL) break;
}
$px = $pb[$minKey];
$this->offset = $arrPos[$minKey] + strlen($arrMatches[$minKey][0]) + 1; // 1 = \n
$start = $arrPos[$minKey];
$this->offset = $start + strlen($arrMatches[$minKey][0]) + 1; // 1 = \n
$res = call_user_func_array(
$px['handler'],
array($this, $arrMatches[$minKey], $minKey)
);
if ($res === FALSE || $this->offset <= $arrPos[$minKey]) { // module rejects text
if ($res === FALSE || $this->offset <= $start) { // module rejects text
// nemelo by se stat, rozdeli generic block
$this->offset = $arrPos[$minKey]; // turn offset back
$this->offset = $start; // turn offset back
$arrPos[$minKey] = -2;
continue;
} elseif (is_a($res, 'TexyHtml')) {
$nodes[] = $res;
} elseif (is_string($res)) {
$nodes[] = TexyHtml::text($res);
$res = TexyHtml::text($res);
$nodes[] = $res;
}
$arrPos[$minKey] = -1;
} while (1);
if ($this->parent)
$this->parent->children = $nodes;
if ($this->parentEl)
$this->parentEl->children = $nodes;
return $nodes;
}
Expand Down Expand Up @@ -260,7 +264,7 @@ class TexyLineParser extends TexyParser
function __construct(/*Texy*/ $texy, $element = NULL)
{
$this->texy = $texy;
$this->parent = $element;
$this->parentEl = $element;
$this->patterns = $texy->getLinePatterns();
}
Expand Down Expand Up @@ -357,8 +361,8 @@ function parse($text)
} while (1);
if ($this->parent)
$this->parent->setText($text);
if ($this->parentEl)
$this->parentEl->setText($text);
return $text;
}
Expand Down
2 changes: 1 addition & 1 deletion texy-for-php4/modules/TexyHeadingModule.php
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ function solve($invocation, $level, $content, $mod, $isSurrounded)
$el->parseLine($tx, trim($content));

// document title
$title = $el->toText($tx);
$title = trim($el->toText($tx));
if ($this->title === NULL) $this->title = $title;

// Table of Contents
Expand Down
3 changes: 2 additions & 1 deletion texy-for-php4/modules/TexyListModule.php
Original file line number Diff line number Diff line change
Expand Up @@ -118,8 +118,9 @@ function patternList($parser, $matches)

$parser->moveBackward(1);

while ($elItem = $this->patternItem($parser, $bullet, FALSE, 'li'))
while ($elItem = $this->patternItem($parser, $bullet, FALSE, 'li')) {
$el->addChild($elItem);
}

if (count($el->children) < $min) return FALSE;

Expand Down
15 changes: 11 additions & 4 deletions texy-for-php4/texy.php
Original file line number Diff line number Diff line change
Expand Up @@ -388,10 +388,11 @@ function process($text, $singleLine = FALSE)

// parse Texy! document into internal DOM structure
$this->DOM = TexyHtml::el();
if ($singleLine)
if ($singleLine) {
$this->DOM->parseLine($this, $text);
else
} else {
$this->DOM->parseBlock($this, $text, TRUE);
}

// user after handler
$this->invokeAfterHandlers('afterParse', array($this, $this->DOM, $singleLine));
Expand Down Expand Up @@ -463,8 +464,9 @@ function stringToHtml($s)
foreach ($this->modules as $module) {
if (isset($module->interface['TexyPostLineInterface'])) {
foreach ($blocks as $n => $s) {
if ($n % 2 === 0 && $s !== '')
if ($n % 2 === 0 && $s !== '') {
$blocks[$n] = $module->postLine($s);
}
}
}
}
Expand Down Expand Up @@ -543,7 +545,9 @@ function invokeHandlers($event, $parser, $args)
if (!isset($this->handlers[$event])) return FALSE;

$invocation = new TexyHandlerInvocation($this->handlers[$event], $parser, $args);
return $invocation->proceed();
$res = $invocation->proceed();
$invocation->free();
return $res;
}


Expand Down Expand Up @@ -832,6 +836,9 @@ function tabCb($m) /* private */



/**
* PHP garbage collector helper
*/
function free()
{
foreach (array_keys(get_object_vars($this)) as $key)
Expand Down
9 changes: 9 additions & 0 deletions texy/libs/TexyHandlerInvocation.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,4 +88,13 @@ public function getTexy()
return $this->parser->getTexy();
}


/**
* PHP garbage collector helper
*/
public function free()
{
$this->handlers = $this->parser = $this->args = NULL;
}

}
Loading

0 comments on commit e642966

Please sign in to comment.