Skip to content

Commit

Permalink
Updates to fix issues with else and with whitespace only lines.
Browse files Browse the repository at this point in the history
  • Loading branch information
guyc committed Jun 14, 2012
1 parent afcbd69 commit 0964863
Show file tree
Hide file tree
Showing 7 changed files with 45 additions and 12 deletions.
9 changes: 7 additions & 2 deletions Foml.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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;
}

Expand Down Expand Up @@ -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);
}
}
Expand Down
1 change: 1 addition & 0 deletions FomlDoc.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ class FomlDoc extends FomlNode

function RenderToString()
{
$this->state = new FomlRenderState();
ob_start();
$this->Render();
$output = ob_get_contents();
Expand Down
8 changes: 4 additions & 4 deletions FomlExecNode.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php
class FomlExecNode extends FomlNode
{
public $mode = Foml::PHP_MODE;
const MATCH_RE = "/^-\s*(.*)/";
/*
* Examples
Expand Down Expand Up @@ -31,19 +32,18 @@ function __construct($Matches)

function RenderPrefix()
{
print "<?php ";
print $this->code;
if ($this->hasBlock) {
print "{ ?>";
print "{";
} else {
print "; ?>";
print ";";
}
}

function RenderSuffix()
{
if ($this->hasBlock) {
print "<?php } ?>";
print "}";
}
}
}
6 changes: 3 additions & 3 deletions FomlInsertNode.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php
class FomlInsertNode extends FomlNode
{
public $mode = Foml::PHP_MODE;
const MATCH_RE = "/^=\s*(.*)/";

function __construct($Matches)
Expand All @@ -10,9 +11,8 @@ function __construct($Matches)

function RenderPrefix()
{
print "<?php print ";
print "print ";
print $this->code;
print "; ?>";
print " \n";
print ";\n";
}
}
22 changes: 21 additions & 1 deletion FomlNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
{
Expand All @@ -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 "<?php ";
}
} elseif ($Mode == Foml::XML_MODE) {
if ($this->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();
}
}
Expand Down
5 changes: 3 additions & 2 deletions FomlParseTree.php
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Expand Down Expand Up @@ -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;
Expand Down
6 changes: 6 additions & 0 deletions FomlRenderState.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?php
class FomlRenderState
{
public $mode = Foml::XML_MODE;
}
?>

0 comments on commit 0964863

Please sign in to comment.