-
Notifications
You must be signed in to change notification settings - Fork 0
/
phml.php
282 lines (229 loc) · 7.31 KB
/
phml.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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
<?php
# TODO : need to separate the output indentation from the nesting
#
# - TODO : support if/else if/else
# - TODO : support switch with case
# - a line could have both ->nesting and ->indent
# - certain lines require not indent (like php flow control statements)
# while still effecting nesting for parsing
#
# - if(cond_a())
# %p bar
#
# TODO : some elements REQUIRE nested elements, like flow control statements
# TODO : some elements self close (br, hr, link, meta, etc)
# TODO : should self closing elements allow nested content?
# TODO : only allow $variables and function() calls structures like:
#
# = $foo_variable()
# = foo_function()
# = $foo_function_varaiable()
# = foo_function_with_args($foo, $bar)
# = foo_function_with_args(foo(), bar())
# = foo_function_with_args($foo(), $bar())
# = foo_function_with_args($foo($yuck), $bar())
#
# TODO : decide how to render the template w/out caching it anywhere
#
class PhmlTemplate {
protected $path;
protected $indent = 0;
protected $stack = array();
protected $buffer = array();
public function __construct($template_path) {
# TODO : raise an exception if the template is not readable or not found
$this->path = $template_path;
}
public function render() {
$this->parse();
return implode("\n", $this->buffer);
}
protected function parse() {
$num = 0;
$prev_line = NULL;
$template = fopen($this->path, 'r');
while(!feof($template)) {
$line = new PhmlLine(++$num, fgets($template));
if($line->blank()) continue;
$this->check_indentation($prev_line, $line);
$this->manage_stack($line);
switch(true) {
case $line->no_content():
$this->buffer_str($line->indent, $line->open);
$this->stack[] = $line;
break;
default:
$this->buffer_str($line->indent, $line->render());
}
$prev_line = $line;
}
fclose($template);
while($this->stack_empty() == FALSE)
$this->pop();
}
protected function check_indentation($prev_line, $line) {
if($prev_line) {
$max_indent = $prev_line->indent;
$max_indent += $prev_line->no_content() ? 1 : 0;
} else {
$max_indent = 0;
}
if($line->indent > $max_indent)
throw new Exception("invalid indentation on line {$line->num}");
}
protected function manage_stack($line) {
while($this->stack_empty() == false) {
if($line->indent <= $this->top()->indent)
$this->pop();
else
break;
}
}
protected function stack_empty() {
return count($this->stack) == 0;
}
protected function top() {
$count = count($this->stack);
return $count > 0 ? $this->stack[$count - 1] : null;
}
protected function pop() {
$top = array_pop($this->stack);
$this->buffer_str($top->indent, $top->close);
}
protected function buffer_str($indent, $str) {
$this->buffer[] = str_repeat(' ', $indent) . $str;
}
}
class PhmlLine {
const DOCTYPE = '/^!!!(\s+(.+))?$/';
const HTML_COMMENT = '/^\/(\s+(.+))?$/';
const HTML_ELEMENT = '/^(%[a-z]\w*)?(#[a-z]\w*)?(\.[a-z][\.\w]*)?(\(.+\))?(\s*\/|=\s*|\s+.+|)$/i';
const PHP_FLOW = '/^- (if|foreach)(\(.+\))$/';
const PHP_OPEN = '<?php';
const PHP_CLOSE = '?>';
public $num;
public $indent;
public $line;
public $type;
public $open = NULL;
public $content = NULL;
public $close = NULL;
public function __construct($num, $line) {
$this->num = $num;
$line = rtrim($line);
# TODO : validate the indentation
$this->indent = strspn($line, ' ') / 2;
$line = ltrim($line);
$this->line = $line;
$this->parse_line();
}
public function closed() {
#return $this->content != NULL ||
}
public function blank() {
return $this->type == 'ignore';
}
public function no_content() {
return $this->content == NULL;
}
protected function parse_line() {
$line = $this->line;
switch(true) {
# phml comments and blank lines
case $line == '':
case $this->begins_with('-#');
$this->type = 'ignore';
break;
## doctype
#case $line == '!!!':
case preg_match(self::DOCTYPE, $line, $matches):
# TODO : add other doctypes
$this->type = 'doctype';
$this->content = '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">';
break;
## html comment
case preg_match(self::HTML_COMMENT, $line, $matches):
$this->type = 'html_comment';
$this->open = '<!-- ';
$this->content = isset($matches[2]) ? $matches[2] : NULL;
$this->close = ' -->';
break;
## html element
# meta, img, link, script, br, and hr tags are closed by default.
case preg_match(self::HTML_ELEMENT, $line, $matches):
$this->type = 'html_element';
$tag = $matches[1] ? ltrim($matches[1], '%') : 'div';
if($matches[2]) $id = ltrim($matches[2], '#');
if($matches[3]) $class = str_replace('.', ' ', ltrim($matches[3], '.'));
if($matches[4]) $attrs = trim($matches[4], '()');
# TODO : support auto self closing tags
# TODO : meta, img, link, script, br and hr tags should auto-self-close
$content = ltrim($matches[5]);
if($content == '') {
} else if($content[0] == '/') {
$self_closing = true;
} else if($content[0] == '=') {
$this->content = "<?php echo($content); %>";
} else {
$this->content = $content;
}
$this->open = $this->tag($tag, $id, $class, $attrs);
$this->close = "</$tag>";
break;
# phml comment
case $this->begins_with('-#'):
break;
# php flow control statements
# TODO : add else, else if, do, while, switch, foreach, for
case preg_match(self::PHP_FLOW, $line, $matches):
$type = 'php_flow';
$flow = $matches[1];
$cond = trim($matches[2], '()');
$this->open = $this->php_wrap("$flow($cond):");
$this->close = $this->php_wrap("end$flow;");
break;
# interpreted as php
case $line[0] == '-':
$type = 'php';
break;
case preg_match('/^=\s+(.+)$/', $line, $matches):
$type = 'php_echo';
$this->content = $this->php_wrap("echo({$matches[1]});");
break;
# markup switch
case $line[0] == ':':
break;
# static text
# TODO : support $variable interpolation
# TODO : support {$variable} interpolation
default:
$this->type = 'content';
$this->content = $line;
break;
}
}
protected function php_wrap($str) {
return self::PHP_OPEN . ' ' . $str . ' ' . self::PHP_CLOSE;
}
protected function tag($name, $id, $class, $attrs) {
$attr = '';
if($id) $attr .= " id='$id'";
if($class) $attr .= " class='$class'";
if($attrs) $attr .= " $attrs";
return "<$name{$attr}>";
}
protected function begins_with($search) {
return (strncmp($this->line, $search, strlen($search)) == 0);
}
public function render() {
return trim("{$this->open}{$this->content}{$this->close}");
}
}
ini_set('display_errors', '1');
$options = array('abc', 'xyz', '123');
$template = new PhmlTemplate('template.phml');
echo $template->render();
echo "\n";
#ob_start();
#eval('?' . '>' . $template->render());
#echo ob_get_clean();