-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFomlElementNode.php
executable file
·112 lines (99 loc) · 3.46 KB
/
FomlElementNode.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
<?php
class FomlElementNode extends FomlNode
{
// matches namespace:element[/]args
const MATCH_RE = "/^%(([-a-zA-Z0-9]+):)?([-a-zA-Z0-9]+)\s*(.*?)(\/?)\s*$/";
// %element(opts)
public $namespace;
public $tag;
public $selfClose;
public $args;
// REVISIT - should not allow children if it is autoclosed
function __construct($Matches)
{
$namespace = $Matches[2];
$tag = $Matches[3];
$args = $Matches[4];
$close = $Matches[5]; // '/' if tag should self-close, '' otherwise
$this->namespace = $namespace;
$this->selfClose = $close=='/';
$this->tag = $tag;
$extra = $this->ParseArgs($args);
$this->ParseExtra($extra);
}
// replace instances of #{...} with [php] print($1);[/php]
function ExpandExpressions($Text)
{
$text = $Text;
$expansion = '';
while (preg_match("/\#\{(.*?)\}/", $text, $matches, PREG_OFFSET_CAPTURE)) {
$matchLength = $matches[0][1]; // including length of markup
$expression = $matches[1][0];
$expansion .= substr($text, 0, $matchLength);
$text = substr($text, $matchLength+strlen($matches[0][0]));
$expansion .= '<?';
$expansion .= 'php ';
$expansion .= "print({$expression});";
$expansion .= '?>';
}
$expansion.=$text;
return $expansion;
}
function ParseArgs($Args)
{
// REVISIT - this is a piss-poor
// hack for now until I write a real arg parser.
// By making the paren matches non-hungry it will now support lines like this:
// %fo:block(border-after-style="solid") = join(" ",array("Thao","Vang","Lor"))
// but will not correctly match all possible uses of brackets and does not support
// expansion of inline evaluation contexts #{$variable}
// For now switch to hungry because it is harder to work around that deficiency
if (preg_match("/^\((.*)\)(.*)/", $Args, $matches)) {
//print "<pre>"; print_r($matches); print "</pre>";
// REVISIT - this offers only minimal support for #{<phpexpression>}
$this->args = $this->ExpandExpressions($matches[1]);
$Args = $matches[2];
//$Args = $matches[2];
}
return $Args;
}
// The $Extra string may include a shorthand for certain other node types.
// -.* : execution context
// =.* : evaluation context
// .* : text node
function ParseExtra($Extra)
{
$extNodes = array('FomlExecNode', 'FomlEvalNode', 'FomlTextNode');
// FomlTextNode matches everything
$Extra = trim($Extra);
if ($Extra != "") {
foreach ($extNodes as $nodeClass) {
if (preg_match($nodeClass::MATCH_RE, $Extra, $matches)) {
$this->children[] = new $nodeClass($matches);
break;
}
}
}
}
function Render()
{
if ($this->namespace=="") $this->namespace = Foml::$defaultNamespace;
parent::Render();
}
function RenderPrefix()
{
print "<{$this->namespace}:{$this->tag} {$this->args}";
if ($this->selfClose) {
print "/>\n";
} else {
print ">\n";
}
}
function RenderSuffix()
{
if (!$this->selfClose) {
print "</{$this->namespace}:{$this->tag}>\n";
}
}
}
?>