From 09648639763a2fd94d694dfc89fa123bc96b4d75 Mon Sep 17 00:00:00 2001 From: Guy Carpenter Date: Thu, 14 Jun 2012 18:03:05 +1000 Subject: [PATCH] Updates to fix issues with else and with whitespace only lines. --- Foml.php | 9 +++++++-- FomlDoc.php | 1 + FomlExecNode.php | 8 ++++---- FomlInsertNode.php | 6 +++--- FomlNode.php | 22 +++++++++++++++++++++- FomlParseTree.php | 5 +++-- FomlRenderState.php | 6 ++++++ 7 files changed, 45 insertions(+), 12 deletions(-) create mode 100755 FomlRenderState.php diff --git a/Foml.php b/Foml.php index 56d612e..f0856b4 100755 --- a/Foml.php +++ b/Foml.php @@ -8,9 +8,12 @@ class Foml { + const PHP_MODE = 'php'; + const XML_MODE = 'xml'; + static $fopExec = "fop-1.0/fop"; // fopExec is relative to this directory static $tempDir = null; // defaults to system temp directory - static $keepTempFiles = false; // set to true for debugging + static $keepTempFiles = true; // set to true for debugging static $pdfMimeType = "application/pdf"; static function GeneratePhp($Template) @@ -33,10 +36,12 @@ static function GenerateXslFo($Template, $Args=null) } $_php = Foml::GeneratePhp($Template); + //Dump($_php); exit; ob_start(); eval("?".">".$_php); // prefixed with ? > to exit implicit php mode $xslFo = ob_get_contents(); ob_end_clean(); + //Dump($xslFo); exit; return $xslFo; } @@ -98,7 +103,7 @@ static function RenderInline($Template, $Args=null) static function RenderAttachment($Template, $Filename, $Args=null) { - $headers = array("Content-Disposition: attachment; filename=\"{$FileName}\""); + $headers = array("Content-Disposition: attachment; filename=\"{$Filename}\""); Foml::Render($Template, $Args, $headers); } } diff --git a/FomlDoc.php b/FomlDoc.php index c3fd6aa..cbe9ace 100755 --- a/FomlDoc.php +++ b/FomlDoc.php @@ -4,6 +4,7 @@ class FomlDoc extends FomlNode function RenderToString() { + $this->state = new FomlRenderState(); ob_start(); $this->Render(); $output = ob_get_contents(); diff --git a/FomlExecNode.php b/FomlExecNode.php index 09498b2..ec82db5 100755 --- a/FomlExecNode.php +++ b/FomlExecNode.php @@ -1,6 +1,7 @@ code; if ($this->hasBlock) { - print "{ ?>"; + print "{"; } else { - print "; ?>"; + print ";"; } } function RenderSuffix() { if ($this->hasBlock) { - print ""; + print "}"; } } } diff --git a/FomlInsertNode.php b/FomlInsertNode.php index c69f0bb..2852d25 100755 --- a/FomlInsertNode.php +++ b/FomlInsertNode.php @@ -1,6 +1,7 @@ code; - print "; ?>"; - print " \n"; + print ";\n"; } } \ No newline at end of file diff --git a/FomlNode.php b/FomlNode.php index b74e9de..4df75d5 100755 --- a/FomlNode.php +++ b/FomlNode.php @@ -2,6 +2,8 @@ class FomlNode { public $children = array(); + public $state = null; // set to an instance of FomlState before calling render + public $mode = Foml::XML_MODE; function RenderPrefix() { @@ -11,12 +13,30 @@ function RenderSuffix() { } - function Render($Indent=0) + function SetMode($Mode) { + if ($Mode == Foml::PHP_MODE) { + assert($this->state); + if ($this->state->mode == Foml::XML_MODE) { + print "state->mode == Foml::PHP_MODE) { + print " ?>"; + } + } + $this->state->mode = $Mode; + } + + function Render() + { + $this->SetMode($this->mode); $this->RenderPrefix(); foreach ($this->children as $child) { + $child->state = $this->state; $child->Render(); } + $this->SetMode($this->mode); $this->RenderSuffix(); } } diff --git a/FomlParseTree.php b/FomlParseTree.php index 4148929..cb3d44d 100755 --- a/FomlParseTree.php +++ b/FomlParseTree.php @@ -60,7 +60,7 @@ static function ParseLines($Text) } # only pure whitespace lines will be skipped here - if (preg_match("/^(\s*)(.+)/", $line, $matches)) { + if (preg_match("/^(\s*)([^\s].+)/", $line, $matches)) { $tree = new FomlParseTree(); $tree->indent = strlen($matches[1]); $tree->text = $matches[2]; @@ -93,7 +93,8 @@ function Generate() // Generate and add children to the node. foreach ($this->children as $child) { - $node->children[] = $child->Generate(); + $childNode = $child->Generate(); + $node->children[] = $childNode; } return $node; diff --git a/FomlRenderState.php b/FomlRenderState.php new file mode 100755 index 0000000..7e3f20d --- /dev/null +++ b/FomlRenderState.php @@ -0,0 +1,6 @@ + \ No newline at end of file