@@ -54,282 +54,3 @@ |
||
54 | 54 | */ |
55 | 55 | |
56 | 56 | const REGEX_PARSE_LINE = <<<'ENDREGEX' |
57 | - /^(\s*)(?:(?:([a-zA-Z0-9-]*)((?:[\.#!][\w\-\_]+)*)(\[(?:(?:\{\$[^\}]+\})?[^\\\]{]*?(?:\\.)*?(?:{[^\$])*?)+\])?)|([_\/]{1,3})|([\|:\$]\w+)|({?\$[^}]+}?)|)(?: (.*))?$/ |
|
58 | - ENDREGEX; |
|
59 | - |
|
60 | - /** |
|
61 | - * @var int Current Line Number |
|
62 | - */ |
|
63 | - protected $lineNo; |
|
64 | - /** |
|
65 | - * @var int Total Lines in File |
|
66 | - */ |
|
67 | - protected $lineCount; |
|
68 | - |
|
69 | - function __construct() |
|
70 | - { |
|
71 | - $this->init(); |
|
72 | - } |
|
73 | - |
|
74 | - /** |
|
75 | - * Clear Lines, and Line Number, so if output is |
|
76 | - * called, no output will be produced |
|
77 | - */ |
|
78 | - protected function init() |
|
79 | - { |
|
80 | - $this->lines = []; |
|
81 | - $this->lineNo = 0; |
|
82 | - $this->lineCount = 0; |
|
83 | - $this->root = []; |
|
84 | - } |
|
85 | - |
|
86 | - protected function loadLines($s) |
|
87 | - { |
|
88 | - $this->lines = explode("\n", str_replace("\r", '', $s)); |
|
89 | - $this->lineCount = count($this->lines); |
|
90 | - $this->lineNo = 0; |
|
91 | - } |
|
92 | - |
|
93 | - function parseFilter(ParseFilter $filter) |
|
94 | - { |
|
95 | - foreach ($this->root as $k => $tag) { |
|
96 | - $this->root[$k] = $filter->filterTag($tag); |
|
97 | - } |
|
98 | - } |
|
99 | - |
|
100 | - function parseSnip($s) |
|
101 | - { |
|
102 | - //save root tags |
|
103 | - /** @var Tag[] $roots */ |
|
104 | - $roots = $this->root; |
|
105 | - $this->root = []; |
|
106 | - $this->loadLines($s); |
|
107 | - $this->procLines(); |
|
108 | - $this->root = array_merge($roots, $this->root); |
|
109 | - } |
|
110 | - |
|
111 | - function applySnip() |
|
112 | - { |
|
113 | - /** @var Tag\Snippet[] $fwdSnip */ |
|
114 | - $fwdSnip = []; |
|
115 | - /** @var Tag\Snippet[] $revSnip */ |
|
116 | - $revSnip = []; |
|
117 | - /** @var Tag[] $roots */ |
|
118 | - $roots = []; |
|
119 | - foreach ($this->root as $snip) { |
|
120 | - if ($snip instanceof Tag\Snippet) { |
|
121 | - if ($snip->getType() == 'append') { |
|
122 | - array_unshift($revSnip, $snip); |
|
123 | - } else { |
|
124 | - $fwdSnip[] = $snip; |
|
125 | - } |
|
126 | - } else { |
|
127 | - $roots[] = $snip; |
|
128 | - } |
|
129 | - } |
|
130 | - foreach ($fwdSnip as $snip) { |
|
131 | - foreach ($roots as $root) { |
|
132 | - $snip->apply($root); |
|
133 | - } |
|
134 | - } |
|
135 | - foreach ($revSnip as $snip) { |
|
136 | - foreach ($roots as $root) { |
|
137 | - $snip->apply($root); |
|
138 | - } |
|
139 | - } |
|
140 | - $this->root = $roots; |
|
141 | - } |
|
142 | - |
|
143 | - /** |
|
144 | - * Parse HAMLE template, from a string |
|
145 | - * @param string $s String to parse |
|
146 | - */ |
|
147 | - function str($s) |
|
148 | - { |
|
149 | - $this->init(); |
|
150 | - $this->loadLines($s); |
|
151 | - $this->procLines(); |
|
152 | - } |
|
153 | - |
|
154 | - function procLines() |
|
155 | - { |
|
156 | - /* @var $heir Tag[] Tag Heirachy Array */ |
|
157 | - $heir = []; |
|
158 | - while ($this->lineNo < $this->lineCount) { |
|
159 | - $line = $this->lines[$this->lineNo]; |
|
160 | - if (trim($line)) { |
|
161 | - if (preg_match(self::REGEX_PARSE_LINE, $line, $m)) { |
|
162 | - if (false !== strpos($m[1], "\t")) { |
|
163 | - throw new ParseError( |
|
164 | - 'Tabs are not supported in templates at this time', |
|
165 | - ); |
|
166 | - } |
|
167 | - $indent = strlen($m[1]); |
|
168 | - $tag = isset($m[2]) ? ($tag = $m[2]) : ''; |
|
169 | - $classid = isset($m[3]) ? $m[3] : ''; |
|
170 | - $params = str_replace( |
|
171 | - ['\[', '\]', '\\&'], |
|
172 | - ['[', ']', '%26'], |
|
173 | - isset($m[4]) ? $m[4] : '', |
|
174 | - ); |
|
175 | - $textcode = isset($m[5]) ? $m[5] : ''; |
|
176 | - $text = isset($m[8]) ? $m[8] : ''; |
|
177 | - $code = isset($m[6]) ? $m[6] : ''; |
|
178 | - $i = self::indentLevel($indent); |
|
179 | - unset($m[0]); |
|
180 | - switch (strlen($code) ? $code[0] : ($textcode ? $textcode : '')) { |
|
181 | - case '|': //Control Tag |
|
182 | - if ($code == '|snippet') { |
|
183 | - $hTag = new Tag\Snippet($text); |
|
184 | - } elseif ($code == '|form') { |
|
185 | - $hTag = new Tag\Form($text); |
|
186 | - } elseif ($code == '|formhint') { |
|
187 | - $hTag = new Tag\FormHint(); |
|
188 | - } elseif ($code == '|else') { |
|
189 | - $hTag = new Tag\Control(substr($code, 1), $heir[$i - 1]); |
|
190 | - $hTag->setVar($text); |
|
191 | - } else { |
|
192 | - $hTag = new Tag\Control(substr($code, 1)); |
|
193 | - $hTag->setVar($text); |
|
194 | - } |
|
195 | - break; |
|
196 | - case ':': //Filter Tag |
|
197 | - $hTag = new Tag\Filter(substr($code, 1)); |
|
198 | - $hTag->addContent($text, Text::TOKEN_CODE); |
|
199 | - foreach ($this->consumeBlock($indent) as $l) { |
|
200 | - $hTag->addContent($l, Text::TOKEN_CODE); |
|
201 | - } |
|
202 | - break; |
|
203 | - case '_': //String Tag |
|
204 | - case '__': //Unescape String Tag |
|
205 | - case '___': //Unescape String Tag (with unescaped vars) |
|
206 | - $hTag = new Tag\Text($textcode); |
|
207 | - $hTag->addContent($text); |
|
208 | - break; |
|
209 | - case '___': //Unescape String Tag |
|
210 | - $hTag = new Tag\Text($textcode); |
|
211 | - $hTag->addContent($text); |
|
212 | - break; |
|
213 | - case '/': // HTML Comment |
|
214 | - case '//': // Non Printed Comment |
|
215 | - $hTag = new Tag\Comment($textcode); |
|
216 | - $hTag->addContent($text); |
|
217 | - foreach ($this->consumeBlock($indent) as $l) { |
|
218 | - $hTag->addContent($l, Text::TOKEN_CODE); |
|
219 | - } |
|
220 | - break; |
|
221 | - default: |
|
222 | - $attr = []; |
|
223 | - if (isset($params[0]) && $params[0] == '[') { |
|
224 | - $param = substr($params, 1, -1); |
|
225 | - $param = str_replace(['+', '\\&'], ['%2B', '%26'], $param); |
|
226 | - // parse_str($param, $attr); |
|
227 | - $attr = $this->parseQueryString($param); |
|
228 | - } |
|
229 | - $class = []; |
|
230 | - $id = ''; |
|
231 | - $ref = ''; |
|
232 | - preg_match_all('/[#\.!][a-zA-Z0-9\-\_]+/m', $classid, $cid); |
|
233 | - if (isset($cid[0])) { |
|
234 | - foreach ($cid[0] as $s) { |
|
235 | - if ($s[0] == '#') { |
|
236 | - $id = substr($s, 1); |
|
237 | - } |
|
238 | - if ($s[0] == '.') { |
|
239 | - $class[] = substr($s, 1); |
|
240 | - } |
|
241 | - if ($s[0] == '!') { |
|
242 | - $ref = substr($s, 1); |
|
243 | - } |
|
244 | - } |
|
245 | - } |
|
246 | - if ($ref) { |
|
247 | - $hTag = new Tag\DynHtml($tag, $class, $attr, $id, $ref); |
|
248 | - } else { |
|
249 | - $hTag = new Tag\Html($tag, $class, $attr, $id); |
|
250 | - } |
|
251 | - $hTag->addContent($text); |
|
252 | - break; |
|
253 | - } |
|
254 | - $heir[$i] = $hTag; |
|
255 | - if ($i > 0) { |
|
256 | - $heir[$i - 1]->addChild($hTag); |
|
257 | - } else { |
|
258 | - $this->root[] = $hTag; |
|
259 | - } |
|
260 | - } else { |
|
261 | - throw new ParseError( |
|
262 | - "Unable to parse line {$this->lineNo}\n\"$line\"/" . |
|
263 | - preg_last_error(), |
|
264 | - ); |
|
265 | - } |
|
266 | - } |
|
267 | - $this->lineNo++; |
|
268 | - } |
|
269 | - } |
|
270 | - |
|
271 | - function parseQueryString($qs) |
|
272 | - { |
|
273 | - $out = []; |
|
274 | - foreach (explode('&', $qs) as $s) { |
|
275 | - $kv = explode('=', $s, 2); |
|
276 | - $out[urldecode($kv[0])] = isset($kv[1]) ? urldecode($kv[1]) : null; |
|
277 | - } |
|
278 | - return $out; |
|
279 | - } |
|
280 | - |
|
281 | - function output($minify = false) |
|
282 | - { |
|
283 | - $out = "<?php\nuse Seufert\\Hamle;\n?>"; |
|
284 | - foreach ($this->root as $tag) { |
|
285 | - $out .= $tag->render(0, $minify); |
|
286 | - } |
|
287 | - return $out; |
|
288 | - } |
|
289 | - |
|
290 | - function consumeBlock($indent) |
|
291 | - { |
|
292 | - $out = []; |
|
293 | - $m = []; |
|
294 | - while ( |
|
295 | - $this->lineNo + 1 < $this->lineCount && |
|
296 | - (!trim($this->lines[$this->lineNo + 1]) || |
|
297 | - preg_match( |
|
298 | - '/^(\s){' . $indent . '}((\s)+[^\s].*)$/', |
|
299 | - $this->lines[$this->lineNo + 1], |
|
300 | - $m, |
|
301 | - )) |
|
302 | - ) { |
|
303 | - if (trim($this->lines[$this->lineNo + 1])) { |
|
304 | - $out[] = $m[2]; |
|
305 | - } |
|
306 | - $this->lineNo++; |
|
307 | - } |
|
308 | - return $out; |
|
309 | - } |
|
310 | - |
|
311 | - function indentLevel($indent) |
|
312 | - { |
|
313 | - if (!isset($this->indents)) { |
|
314 | - $this->indents = []; |
|
315 | - } |
|
316 | - if (!count($this->indents)) { |
|
317 | - $this->indents = [0 => $indent]; |
|
318 | - // Key = indent level, Value = Depth in spaces |
|
319 | - return 0; |
|
320 | - } |
|
321 | - foreach ($this->indents as $k => $v) { |
|
322 | - if ($v == $indent) { |
|
323 | - $this->indents = array_slice($this->indents, 0, $k + 1); |
|
324 | - return $k; |
|
325 | - } |
|
326 | - } |
|
327 | - $this->indents[] = $indent; |
|
328 | - return max(array_keys($this->indents)); |
|
329 | - } |
|
330 | - |
|
331 | - function getLineNo() |
|
332 | - { |
|
333 | - return $this->lineNo; |
|
334 | - } |
|
335 | -} |
@@ -54,282 +54,3 @@ |
||
54 | 54 | */ |
55 | 55 | |
56 | 56 | const REGEX_PARSE_LINE = <<<'ENDREGEX' |
57 | - /^(\s*)(?:(?:([a-zA-Z0-9-]*)((?:[\.#!][\w\-\_]+)*)(\[(?:(?:\{\$[^\}]+\})?[^\\\]{]*?(?:\\.)*?(?:{[^\$])*?)+\])?)|([_\/]{1,3})|([\|:\$]\w+)|({?\$[^}]+}?)|)(?: (.*))?$/ |
|
58 | - ENDREGEX; |
|
59 | - |
|
60 | - /** |
|
61 | - * @var int Current Line Number |
|
62 | - */ |
|
63 | - protected $lineNo; |
|
64 | - /** |
|
65 | - * @var int Total Lines in File |
|
66 | - */ |
|
67 | - protected $lineCount; |
|
68 | - |
|
69 | - function __construct() |
|
70 | - { |
|
71 | - $this->init(); |
|
72 | - } |
|
73 | - |
|
74 | - /** |
|
75 | - * Clear Lines, and Line Number, so if output is |
|
76 | - * called, no output will be produced |
|
77 | - */ |
|
78 | - protected function init() |
|
79 | - { |
|
80 | - $this->lines = []; |
|
81 | - $this->lineNo = 0; |
|
82 | - $this->lineCount = 0; |
|
83 | - $this->root = []; |
|
84 | - } |
|
85 | - |
|
86 | - protected function loadLines($s) |
|
87 | - { |
|
88 | - $this->lines = explode("\n", str_replace("\r", '', $s)); |
|
89 | - $this->lineCount = count($this->lines); |
|
90 | - $this->lineNo = 0; |
|
91 | - } |
|
92 | - |
|
93 | - function parseFilter(ParseFilter $filter) |
|
94 | - { |
|
95 | - foreach ($this->root as $k => $tag) { |
|
96 | - $this->root[$k] = $filter->filterTag($tag); |
|
97 | - } |
|
98 | - } |
|
99 | - |
|
100 | - function parseSnip($s) |
|
101 | - { |
|
102 | - //save root tags |
|
103 | - /** @var Tag[] $roots */ |
|
104 | - $roots = $this->root; |
|
105 | - $this->root = []; |
|
106 | - $this->loadLines($s); |
|
107 | - $this->procLines(); |
|
108 | - $this->root = array_merge($roots, $this->root); |
|
109 | - } |
|
110 | - |
|
111 | - function applySnip() |
|
112 | - { |
|
113 | - /** @var Tag\Snippet[] $fwdSnip */ |
|
114 | - $fwdSnip = []; |
|
115 | - /** @var Tag\Snippet[] $revSnip */ |
|
116 | - $revSnip = []; |
|
117 | - /** @var Tag[] $roots */ |
|
118 | - $roots = []; |
|
119 | - foreach ($this->root as $snip) { |
|
120 | - if ($snip instanceof Tag\Snippet) { |
|
121 | - if ($snip->getType() == 'append') { |
|
122 | - array_unshift($revSnip, $snip); |
|
123 | - } else { |
|
124 | - $fwdSnip[] = $snip; |
|
125 | - } |
|
126 | - } else { |
|
127 | - $roots[] = $snip; |
|
128 | - } |
|
129 | - } |
|
130 | - foreach ($fwdSnip as $snip) { |
|
131 | - foreach ($roots as $root) { |
|
132 | - $snip->apply($root); |
|
133 | - } |
|
134 | - } |
|
135 | - foreach ($revSnip as $snip) { |
|
136 | - foreach ($roots as $root) { |
|
137 | - $snip->apply($root); |
|
138 | - } |
|
139 | - } |
|
140 | - $this->root = $roots; |
|
141 | - } |
|
142 | - |
|
143 | - /** |
|
144 | - * Parse HAMLE template, from a string |
|
145 | - * @param string $s String to parse |
|
146 | - */ |
|
147 | - function str($s) |
|
148 | - { |
|
149 | - $this->init(); |
|
150 | - $this->loadLines($s); |
|
151 | - $this->procLines(); |
|
152 | - } |
|
153 | - |
|
154 | - function procLines() |
|
155 | - { |
|
156 | - /* @var $heir Tag[] Tag Heirachy Array */ |
|
157 | - $heir = []; |
|
158 | - while ($this->lineNo < $this->lineCount) { |
|
159 | - $line = $this->lines[$this->lineNo]; |
|
160 | - if (trim($line)) { |
|
161 | - if (preg_match(self::REGEX_PARSE_LINE, $line, $m)) { |
|
162 | - if (false !== strpos($m[1], "\t")) { |
|
163 | - throw new ParseError( |
|
164 | - 'Tabs are not supported in templates at this time', |
|
165 | - ); |
|
166 | - } |
|
167 | - $indent = strlen($m[1]); |
|
168 | - $tag = isset($m[2]) ? ($tag = $m[2]) : ''; |
|
169 | - $classid = isset($m[3]) ? $m[3] : ''; |
|
170 | - $params = str_replace( |
|
171 | - ['\[', '\]', '\\&'], |
|
172 | - ['[', ']', '%26'], |
|
173 | - isset($m[4]) ? $m[4] : '', |
|
174 | - ); |
|
175 | - $textcode = isset($m[5]) ? $m[5] : ''; |
|
176 | - $text = isset($m[8]) ? $m[8] : ''; |
|
177 | - $code = isset($m[6]) ? $m[6] : ''; |
|
178 | - $i = self::indentLevel($indent); |
|
179 | - unset($m[0]); |
|
180 | - switch (strlen($code) ? $code[0] : ($textcode ? $textcode : '')) { |
|
181 | - case '|': //Control Tag |
|
182 | - if ($code == '|snippet') { |
|
183 | - $hTag = new Tag\Snippet($text); |
|
184 | - } elseif ($code == '|form') { |
|
185 | - $hTag = new Tag\Form($text); |
|
186 | - } elseif ($code == '|formhint') { |
|
187 | - $hTag = new Tag\FormHint(); |
|
188 | - } elseif ($code == '|else') { |
|
189 | - $hTag = new Tag\Control(substr($code, 1), $heir[$i - 1]); |
|
190 | - $hTag->setVar($text); |
|
191 | - } else { |
|
192 | - $hTag = new Tag\Control(substr($code, 1)); |
|
193 | - $hTag->setVar($text); |
|
194 | - } |
|
195 | - break; |
|
196 | - case ':': //Filter Tag |
|
197 | - $hTag = new Tag\Filter(substr($code, 1)); |
|
198 | - $hTag->addContent($text, Text::TOKEN_CODE); |
|
199 | - foreach ($this->consumeBlock($indent) as $l) { |
|
200 | - $hTag->addContent($l, Text::TOKEN_CODE); |
|
201 | - } |
|
202 | - break; |
|
203 | - case '_': //String Tag |
|
204 | - case '__': //Unescape String Tag |
|
205 | - case '___': //Unescape String Tag (with unescaped vars) |
|
206 | - $hTag = new Tag\Text($textcode); |
|
207 | - $hTag->addContent($text); |
|
208 | - break; |
|
209 | - case '___': //Unescape String Tag |
|
210 | - $hTag = new Tag\Text($textcode); |
|
211 | - $hTag->addContent($text); |
|
212 | - break; |
|
213 | - case '/': // HTML Comment |
|
214 | - case '//': // Non Printed Comment |
|
215 | - $hTag = new Tag\Comment($textcode); |
|
216 | - $hTag->addContent($text); |
|
217 | - foreach ($this->consumeBlock($indent) as $l) { |
|
218 | - $hTag->addContent($l, Text::TOKEN_CODE); |
|
219 | - } |
|
220 | - break; |
|
221 | - default: |
|
222 | - $attr = []; |
|
223 | - if (isset($params[0]) && $params[0] == '[') { |
|
224 | - $param = substr($params, 1, -1); |
|
225 | - $param = str_replace(['+', '\\&'], ['%2B', '%26'], $param); |
|
226 | - // parse_str($param, $attr); |
|
227 | - $attr = $this->parseQueryString($param); |
|
228 | - } |
|
229 | - $class = []; |
|
230 | - $id = ''; |
|
231 | - $ref = ''; |
|
232 | - preg_match_all('/[#\.!][a-zA-Z0-9\-\_]+/m', $classid, $cid); |
|
233 | - if (isset($cid[0])) { |
|
234 | - foreach ($cid[0] as $s) { |
|
235 | - if ($s[0] == '#') { |
|
236 | - $id = substr($s, 1); |
|
237 | - } |
|
238 | - if ($s[0] == '.') { |
|
239 | - $class[] = substr($s, 1); |
|
240 | - } |
|
241 | - if ($s[0] == '!') { |
|
242 | - $ref = substr($s, 1); |
|
243 | - } |
|
244 | - } |
|
245 | - } |
|
246 | - if ($ref) { |
|
247 | - $hTag = new Tag\DynHtml($tag, $class, $attr, $id, $ref); |
|
248 | - } else { |
|
249 | - $hTag = new Tag\Html($tag, $class, $attr, $id); |
|
250 | - } |
|
251 | - $hTag->addContent($text); |
|
252 | - break; |
|
253 | - } |
|
254 | - $heir[$i] = $hTag; |
|
255 | - if ($i > 0) { |
|
256 | - $heir[$i - 1]->addChild($hTag); |
|
257 | - } else { |
|
258 | - $this->root[] = $hTag; |
|
259 | - } |
|
260 | - } else { |
|
261 | - throw new ParseError( |
|
262 | - "Unable to parse line {$this->lineNo}\n\"$line\"/" . |
|
263 | - preg_last_error(), |
|
264 | - ); |
|
265 | - } |
|
266 | - } |
|
267 | - $this->lineNo++; |
|
268 | - } |
|
269 | - } |
|
270 | - |
|
271 | - function parseQueryString($qs) |
|
272 | - { |
|
273 | - $out = []; |
|
274 | - foreach (explode('&', $qs) as $s) { |
|
275 | - $kv = explode('=', $s, 2); |
|
276 | - $out[urldecode($kv[0])] = isset($kv[1]) ? urldecode($kv[1]) : null; |
|
277 | - } |
|
278 | - return $out; |
|
279 | - } |
|
280 | - |
|
281 | - function output($minify = false) |
|
282 | - { |
|
283 | - $out = "<?php\nuse Seufert\\Hamle;\n?>"; |
|
284 | - foreach ($this->root as $tag) { |
|
285 | - $out .= $tag->render(0, $minify); |
|
286 | - } |
|
287 | - return $out; |
|
288 | - } |
|
289 | - |
|
290 | - function consumeBlock($indent) |
|
291 | - { |
|
292 | - $out = []; |
|
293 | - $m = []; |
|
294 | - while ( |
|
295 | - $this->lineNo + 1 < $this->lineCount && |
|
296 | - (!trim($this->lines[$this->lineNo + 1]) || |
|
297 | - preg_match( |
|
298 | - '/^(\s){' . $indent . '}((\s)+[^\s].*)$/', |
|
299 | - $this->lines[$this->lineNo + 1], |
|
300 | - $m, |
|
301 | - )) |
|
302 | - ) { |
|
303 | - if (trim($this->lines[$this->lineNo + 1])) { |
|
304 | - $out[] = $m[2]; |
|
305 | - } |
|
306 | - $this->lineNo++; |
|
307 | - } |
|
308 | - return $out; |
|
309 | - } |
|
310 | - |
|
311 | - function indentLevel($indent) |
|
312 | - { |
|
313 | - if (!isset($this->indents)) { |
|
314 | - $this->indents = []; |
|
315 | - } |
|
316 | - if (!count($this->indents)) { |
|
317 | - $this->indents = [0 => $indent]; |
|
318 | - // Key = indent level, Value = Depth in spaces |
|
319 | - return 0; |
|
320 | - } |
|
321 | - foreach ($this->indents as $k => $v) { |
|
322 | - if ($v == $indent) { |
|
323 | - $this->indents = array_slice($this->indents, 0, $k + 1); |
|
324 | - return $k; |
|
325 | - } |
|
326 | - } |
|
327 | - $this->indents[] = $indent; |
|
328 | - return max(array_keys($this->indents)); |
|
329 | - } |
|
330 | - |
|
331 | - function getLineNo() |
|
332 | - { |
|
333 | - return $this->lineNo; |
|
334 | - } |
|
335 | -} |
@@ -54,282 +54,3 @@ |
||
54 | 54 | */ |
55 | 55 | |
56 | 56 | const REGEX_PARSE_LINE = <<<'ENDREGEX' |
57 | - /^(\s*)(?:(?:([a-zA-Z0-9-]*)((?:[\.#!][\w\-\_]+)*)(\[(?:(?:\{\$[^\}]+\})?[^\\\]{]*?(?:\\.)*?(?:{[^\$])*?)+\])?)|([_\/]{1,3})|([\|:\$]\w+)|({?\$[^}]+}?)|)(?: (.*))?$/ |
|
58 | - ENDREGEX; |
|
59 | - |
|
60 | - /** |
|
61 | - * @var int Current Line Number |
|
62 | - */ |
|
63 | - protected $lineNo; |
|
64 | - /** |
|
65 | - * @var int Total Lines in File |
|
66 | - */ |
|
67 | - protected $lineCount; |
|
68 | - |
|
69 | - function __construct() |
|
70 | - { |
|
71 | - $this->init(); |
|
72 | - } |
|
73 | - |
|
74 | - /** |
|
75 | - * Clear Lines, and Line Number, so if output is |
|
76 | - * called, no output will be produced |
|
77 | - */ |
|
78 | - protected function init() |
|
79 | - { |
|
80 | - $this->lines = []; |
|
81 | - $this->lineNo = 0; |
|
82 | - $this->lineCount = 0; |
|
83 | - $this->root = []; |
|
84 | - } |
|
85 | - |
|
86 | - protected function loadLines($s) |
|
87 | - { |
|
88 | - $this->lines = explode("\n", str_replace("\r", '', $s)); |
|
89 | - $this->lineCount = count($this->lines); |
|
90 | - $this->lineNo = 0; |
|
91 | - } |
|
92 | - |
|
93 | - function parseFilter(ParseFilter $filter) |
|
94 | - { |
|
95 | - foreach ($this->root as $k => $tag) { |
|
96 | - $this->root[$k] = $filter->filterTag($tag); |
|
97 | - } |
|
98 | - } |
|
99 | - |
|
100 | - function parseSnip($s) |
|
101 | - { |
|
102 | - //save root tags |
|
103 | - /** @var Tag[] $roots */ |
|
104 | - $roots = $this->root; |
|
105 | - $this->root = []; |
|
106 | - $this->loadLines($s); |
|
107 | - $this->procLines(); |
|
108 | - $this->root = array_merge($roots, $this->root); |
|
109 | - } |
|
110 | - |
|
111 | - function applySnip() |
|
112 | - { |
|
113 | - /** @var Tag\Snippet[] $fwdSnip */ |
|
114 | - $fwdSnip = []; |
|
115 | - /** @var Tag\Snippet[] $revSnip */ |
|
116 | - $revSnip = []; |
|
117 | - /** @var Tag[] $roots */ |
|
118 | - $roots = []; |
|
119 | - foreach ($this->root as $snip) { |
|
120 | - if ($snip instanceof Tag\Snippet) { |
|
121 | - if ($snip->getType() == 'append') { |
|
122 | - array_unshift($revSnip, $snip); |
|
123 | - } else { |
|
124 | - $fwdSnip[] = $snip; |
|
125 | - } |
|
126 | - } else { |
|
127 | - $roots[] = $snip; |
|
128 | - } |
|
129 | - } |
|
130 | - foreach ($fwdSnip as $snip) { |
|
131 | - foreach ($roots as $root) { |
|
132 | - $snip->apply($root); |
|
133 | - } |
|
134 | - } |
|
135 | - foreach ($revSnip as $snip) { |
|
136 | - foreach ($roots as $root) { |
|
137 | - $snip->apply($root); |
|
138 | - } |
|
139 | - } |
|
140 | - $this->root = $roots; |
|
141 | - } |
|
142 | - |
|
143 | - /** |
|
144 | - * Parse HAMLE template, from a string |
|
145 | - * @param string $s String to parse |
|
146 | - */ |
|
147 | - function str($s) |
|
148 | - { |
|
149 | - $this->init(); |
|
150 | - $this->loadLines($s); |
|
151 | - $this->procLines(); |
|
152 | - } |
|
153 | - |
|
154 | - function procLines() |
|
155 | - { |
|
156 | - /* @var $heir Tag[] Tag Heirachy Array */ |
|
157 | - $heir = []; |
|
158 | - while ($this->lineNo < $this->lineCount) { |
|
159 | - $line = $this->lines[$this->lineNo]; |
|
160 | - if (trim($line)) { |
|
161 | - if (preg_match(self::REGEX_PARSE_LINE, $line, $m)) { |
|
162 | - if (false !== strpos($m[1], "\t")) { |
|
163 | - throw new ParseError( |
|
164 | - 'Tabs are not supported in templates at this time', |
|
165 | - ); |
|
166 | - } |
|
167 | - $indent = strlen($m[1]); |
|
168 | - $tag = isset($m[2]) ? ($tag = $m[2]) : ''; |
|
169 | - $classid = isset($m[3]) ? $m[3] : ''; |
|
170 | - $params = str_replace( |
|
171 | - ['\[', '\]', '\\&'], |
|
172 | - ['[', ']', '%26'], |
|
173 | - isset($m[4]) ? $m[4] : '', |
|
174 | - ); |
|
175 | - $textcode = isset($m[5]) ? $m[5] : ''; |
|
176 | - $text = isset($m[8]) ? $m[8] : ''; |
|
177 | - $code = isset($m[6]) ? $m[6] : ''; |
|
178 | - $i = self::indentLevel($indent); |
|
179 | - unset($m[0]); |
|
180 | - switch (strlen($code) ? $code[0] : ($textcode ? $textcode : '')) { |
|
181 | - case '|': //Control Tag |
|
182 | - if ($code == '|snippet') { |
|
183 | - $hTag = new Tag\Snippet($text); |
|
184 | - } elseif ($code == '|form') { |
|
185 | - $hTag = new Tag\Form($text); |
|
186 | - } elseif ($code == '|formhint') { |
|
187 | - $hTag = new Tag\FormHint(); |
|
188 | - } elseif ($code == '|else') { |
|
189 | - $hTag = new Tag\Control(substr($code, 1), $heir[$i - 1]); |
|
190 | - $hTag->setVar($text); |
|
191 | - } else { |
|
192 | - $hTag = new Tag\Control(substr($code, 1)); |
|
193 | - $hTag->setVar($text); |
|
194 | - } |
|
195 | - break; |
|
196 | - case ':': //Filter Tag |
|
197 | - $hTag = new Tag\Filter(substr($code, 1)); |
|
198 | - $hTag->addContent($text, Text::TOKEN_CODE); |
|
199 | - foreach ($this->consumeBlock($indent) as $l) { |
|
200 | - $hTag->addContent($l, Text::TOKEN_CODE); |
|
201 | - } |
|
202 | - break; |
|
203 | - case '_': //String Tag |
|
204 | - case '__': //Unescape String Tag |
|
205 | - case '___': //Unescape String Tag (with unescaped vars) |
|
206 | - $hTag = new Tag\Text($textcode); |
|
207 | - $hTag->addContent($text); |
|
208 | - break; |
|
209 | - case '___': //Unescape String Tag |
|
210 | - $hTag = new Tag\Text($textcode); |
|
211 | - $hTag->addContent($text); |
|
212 | - break; |
|
213 | - case '/': // HTML Comment |
|
214 | - case '//': // Non Printed Comment |
|
215 | - $hTag = new Tag\Comment($textcode); |
|
216 | - $hTag->addContent($text); |
|
217 | - foreach ($this->consumeBlock($indent) as $l) { |
|
218 | - $hTag->addContent($l, Text::TOKEN_CODE); |
|
219 | - } |
|
220 | - break; |
|
221 | - default: |
|
222 | - $attr = []; |
|
223 | - if (isset($params[0]) && $params[0] == '[') { |
|
224 | - $param = substr($params, 1, -1); |
|
225 | - $param = str_replace(['+', '\\&'], ['%2B', '%26'], $param); |
|
226 | - // parse_str($param, $attr); |
|
227 | - $attr = $this->parseQueryString($param); |
|
228 | - } |
|
229 | - $class = []; |
|
230 | - $id = ''; |
|
231 | - $ref = ''; |
|
232 | - preg_match_all('/[#\.!][a-zA-Z0-9\-\_]+/m', $classid, $cid); |
|
233 | - if (isset($cid[0])) { |
|
234 | - foreach ($cid[0] as $s) { |
|
235 | - if ($s[0] == '#') { |
|
236 | - $id = substr($s, 1); |
|
237 | - } |
|
238 | - if ($s[0] == '.') { |
|
239 | - $class[] = substr($s, 1); |
|
240 | - } |
|
241 | - if ($s[0] == '!') { |
|
242 | - $ref = substr($s, 1); |
|
243 | - } |
|
244 | - } |
|
245 | - } |
|
246 | - if ($ref) { |
|
247 | - $hTag = new Tag\DynHtml($tag, $class, $attr, $id, $ref); |
|
248 | - } else { |
|
249 | - $hTag = new Tag\Html($tag, $class, $attr, $id); |
|
250 | - } |
|
251 | - $hTag->addContent($text); |
|
252 | - break; |
|
253 | - } |
|
254 | - $heir[$i] = $hTag; |
|
255 | - if ($i > 0) { |
|
256 | - $heir[$i - 1]->addChild($hTag); |
|
257 | - } else { |
|
258 | - $this->root[] = $hTag; |
|
259 | - } |
|
260 | - } else { |
|
261 | - throw new ParseError( |
|
262 | - "Unable to parse line {$this->lineNo}\n\"$line\"/" . |
|
263 | - preg_last_error(), |
|
264 | - ); |
|
265 | - } |
|
266 | - } |
|
267 | - $this->lineNo++; |
|
268 | - } |
|
269 | - } |
|
270 | - |
|
271 | - function parseQueryString($qs) |
|
272 | - { |
|
273 | - $out = []; |
|
274 | - foreach (explode('&', $qs) as $s) { |
|
275 | - $kv = explode('=', $s, 2); |
|
276 | - $out[urldecode($kv[0])] = isset($kv[1]) ? urldecode($kv[1]) : null; |
|
277 | - } |
|
278 | - return $out; |
|
279 | - } |
|
280 | - |
|
281 | - function output($minify = false) |
|
282 | - { |
|
283 | - $out = "<?php\nuse Seufert\\Hamle;\n?>"; |
|
284 | - foreach ($this->root as $tag) { |
|
285 | - $out .= $tag->render(0, $minify); |
|
286 | - } |
|
287 | - return $out; |
|
288 | - } |
|
289 | - |
|
290 | - function consumeBlock($indent) |
|
291 | - { |
|
292 | - $out = []; |
|
293 | - $m = []; |
|
294 | - while ( |
|
295 | - $this->lineNo + 1 < $this->lineCount && |
|
296 | - (!trim($this->lines[$this->lineNo + 1]) || |
|
297 | - preg_match( |
|
298 | - '/^(\s){' . $indent . '}((\s)+[^\s].*)$/', |
|
299 | - $this->lines[$this->lineNo + 1], |
|
300 | - $m, |
|
301 | - )) |
|
302 | - ) { |
|
303 | - if (trim($this->lines[$this->lineNo + 1])) { |
|
304 | - $out[] = $m[2]; |
|
305 | - } |
|
306 | - $this->lineNo++; |
|
307 | - } |
|
308 | - return $out; |
|
309 | - } |
|
310 | - |
|
311 | - function indentLevel($indent) |
|
312 | - { |
|
313 | - if (!isset($this->indents)) { |
|
314 | - $this->indents = []; |
|
315 | - } |
|
316 | - if (!count($this->indents)) { |
|
317 | - $this->indents = [0 => $indent]; |
|
318 | - // Key = indent level, Value = Depth in spaces |
|
319 | - return 0; |
|
320 | - } |
|
321 | - foreach ($this->indents as $k => $v) { |
|
322 | - if ($v == $indent) { |
|
323 | - $this->indents = array_slice($this->indents, 0, $k + 1); |
|
324 | - return $k; |
|
325 | - } |
|
326 | - } |
|
327 | - $this->indents[] = $indent; |
|
328 | - return max(array_keys($this->indents)); |
|
329 | - } |
|
330 | - |
|
331 | - function getLineNo() |
|
332 | - { |
|
333 | - return $this->lineNo; |
|
334 | - } |
|
335 | -} |
@@ -54,282 +54,3 @@ |
||
54 | 54 | */ |
55 | 55 | |
56 | 56 | const REGEX_PARSE_LINE = <<<'ENDREGEX' |
57 | - /^(\s*)(?:(?:([a-zA-Z0-9-]*)((?:[\.#!][\w\-\_]+)*)(\[(?:(?:\{\$[^\}]+\})?[^\\\]{]*?(?:\\.)*?(?:{[^\$])*?)+\])?)|([_\/]{1,3})|([\|:\$]\w+)|({?\$[^}]+}?)|)(?: (.*))?$/ |
|
58 | - ENDREGEX; |
|
59 | - |
|
60 | - /** |
|
61 | - * @var int Current Line Number |
|
62 | - */ |
|
63 | - protected $lineNo; |
|
64 | - /** |
|
65 | - * @var int Total Lines in File |
|
66 | - */ |
|
67 | - protected $lineCount; |
|
68 | - |
|
69 | - function __construct() |
|
70 | - { |
|
71 | - $this->init(); |
|
72 | - } |
|
73 | - |
|
74 | - /** |
|
75 | - * Clear Lines, and Line Number, so if output is |
|
76 | - * called, no output will be produced |
|
77 | - */ |
|
78 | - protected function init() |
|
79 | - { |
|
80 | - $this->lines = []; |
|
81 | - $this->lineNo = 0; |
|
82 | - $this->lineCount = 0; |
|
83 | - $this->root = []; |
|
84 | - } |
|
85 | - |
|
86 | - protected function loadLines($s) |
|
87 | - { |
|
88 | - $this->lines = explode("\n", str_replace("\r", '', $s)); |
|
89 | - $this->lineCount = count($this->lines); |
|
90 | - $this->lineNo = 0; |
|
91 | - } |
|
92 | - |
|
93 | - function parseFilter(ParseFilter $filter) |
|
94 | - { |
|
95 | - foreach ($this->root as $k => $tag) { |
|
96 | - $this->root[$k] = $filter->filterTag($tag); |
|
97 | - } |
|
98 | - } |
|
99 | - |
|
100 | - function parseSnip($s) |
|
101 | - { |
|
102 | - //save root tags |
|
103 | - /** @var Tag[] $roots */ |
|
104 | - $roots = $this->root; |
|
105 | - $this->root = []; |
|
106 | - $this->loadLines($s); |
|
107 | - $this->procLines(); |
|
108 | - $this->root = array_merge($roots, $this->root); |
|
109 | - } |
|
110 | - |
|
111 | - function applySnip() |
|
112 | - { |
|
113 | - /** @var Tag\Snippet[] $fwdSnip */ |
|
114 | - $fwdSnip = []; |
|
115 | - /** @var Tag\Snippet[] $revSnip */ |
|
116 | - $revSnip = []; |
|
117 | - /** @var Tag[] $roots */ |
|
118 | - $roots = []; |
|
119 | - foreach ($this->root as $snip) { |
|
120 | - if ($snip instanceof Tag\Snippet) { |
|
121 | - if ($snip->getType() == 'append') { |
|
122 | - array_unshift($revSnip, $snip); |
|
123 | - } else { |
|
124 | - $fwdSnip[] = $snip; |
|
125 | - } |
|
126 | - } else { |
|
127 | - $roots[] = $snip; |
|
128 | - } |
|
129 | - } |
|
130 | - foreach ($fwdSnip as $snip) { |
|
131 | - foreach ($roots as $root) { |
|
132 | - $snip->apply($root); |
|
133 | - } |
|
134 | - } |
|
135 | - foreach ($revSnip as $snip) { |
|
136 | - foreach ($roots as $root) { |
|
137 | - $snip->apply($root); |
|
138 | - } |
|
139 | - } |
|
140 | - $this->root = $roots; |
|
141 | - } |
|
142 | - |
|
143 | - /** |
|
144 | - * Parse HAMLE template, from a string |
|
145 | - * @param string $s String to parse |
|
146 | - */ |
|
147 | - function str($s) |
|
148 | - { |
|
149 | - $this->init(); |
|
150 | - $this->loadLines($s); |
|
151 | - $this->procLines(); |
|
152 | - } |
|
153 | - |
|
154 | - function procLines() |
|
155 | - { |
|
156 | - /* @var $heir Tag[] Tag Heirachy Array */ |
|
157 | - $heir = []; |
|
158 | - while ($this->lineNo < $this->lineCount) { |
|
159 | - $line = $this->lines[$this->lineNo]; |
|
160 | - if (trim($line)) { |
|
161 | - if (preg_match(self::REGEX_PARSE_LINE, $line, $m)) { |
|
162 | - if (false !== strpos($m[1], "\t")) { |
|
163 | - throw new ParseError( |
|
164 | - 'Tabs are not supported in templates at this time', |
|
165 | - ); |
|
166 | - } |
|
167 | - $indent = strlen($m[1]); |
|
168 | - $tag = isset($m[2]) ? ($tag = $m[2]) : ''; |
|
169 | - $classid = isset($m[3]) ? $m[3] : ''; |
|
170 | - $params = str_replace( |
|
171 | - ['\[', '\]', '\\&'], |
|
172 | - ['[', ']', '%26'], |
|
173 | - isset($m[4]) ? $m[4] : '', |
|
174 | - ); |
|
175 | - $textcode = isset($m[5]) ? $m[5] : ''; |
|
176 | - $text = isset($m[8]) ? $m[8] : ''; |
|
177 | - $code = isset($m[6]) ? $m[6] : ''; |
|
178 | - $i = self::indentLevel($indent); |
|
179 | - unset($m[0]); |
|
180 | - switch (strlen($code) ? $code[0] : ($textcode ? $textcode : '')) { |
|
181 | - case '|': //Control Tag |
|
182 | - if ($code == '|snippet') { |
|
183 | - $hTag = new Tag\Snippet($text); |
|
184 | - } elseif ($code == '|form') { |
|
185 | - $hTag = new Tag\Form($text); |
|
186 | - } elseif ($code == '|formhint') { |
|
187 | - $hTag = new Tag\FormHint(); |
|
188 | - } elseif ($code == '|else') { |
|
189 | - $hTag = new Tag\Control(substr($code, 1), $heir[$i - 1]); |
|
190 | - $hTag->setVar($text); |
|
191 | - } else { |
|
192 | - $hTag = new Tag\Control(substr($code, 1)); |
|
193 | - $hTag->setVar($text); |
|
194 | - } |
|
195 | - break; |
|
196 | - case ':': //Filter Tag |
|
197 | - $hTag = new Tag\Filter(substr($code, 1)); |
|
198 | - $hTag->addContent($text, Text::TOKEN_CODE); |
|
199 | - foreach ($this->consumeBlock($indent) as $l) { |
|
200 | - $hTag->addContent($l, Text::TOKEN_CODE); |
|
201 | - } |
|
202 | - break; |
|
203 | - case '_': //String Tag |
|
204 | - case '__': //Unescape String Tag |
|
205 | - case '___': //Unescape String Tag (with unescaped vars) |
|
206 | - $hTag = new Tag\Text($textcode); |
|
207 | - $hTag->addContent($text); |
|
208 | - break; |
|
209 | - case '___': //Unescape String Tag |
|
210 | - $hTag = new Tag\Text($textcode); |
|
211 | - $hTag->addContent($text); |
|
212 | - break; |
|
213 | - case '/': // HTML Comment |
|
214 | - case '//': // Non Printed Comment |
|
215 | - $hTag = new Tag\Comment($textcode); |
|
216 | - $hTag->addContent($text); |
|
217 | - foreach ($this->consumeBlock($indent) as $l) { |
|
218 | - $hTag->addContent($l, Text::TOKEN_CODE); |
|
219 | - } |
|
220 | - break; |
|
221 | - default: |
|
222 | - $attr = []; |
|
223 | - if (isset($params[0]) && $params[0] == '[') { |
|
224 | - $param = substr($params, 1, -1); |
|
225 | - $param = str_replace(['+', '\\&'], ['%2B', '%26'], $param); |
|
226 | - // parse_str($param, $attr); |
|
227 | - $attr = $this->parseQueryString($param); |
|
228 | - } |
|
229 | - $class = []; |
|
230 | - $id = ''; |
|
231 | - $ref = ''; |
|
232 | - preg_match_all('/[#\.!][a-zA-Z0-9\-\_]+/m', $classid, $cid); |
|
233 | - if (isset($cid[0])) { |
|
234 | - foreach ($cid[0] as $s) { |
|
235 | - if ($s[0] == '#') { |
|
236 | - $id = substr($s, 1); |
|
237 | - } |
|
238 | - if ($s[0] == '.') { |
|
239 | - $class[] = substr($s, 1); |
|
240 | - } |
|
241 | - if ($s[0] == '!') { |
|
242 | - $ref = substr($s, 1); |
|
243 | - } |
|
244 | - } |
|
245 | - } |
|
246 | - if ($ref) { |
|
247 | - $hTag = new Tag\DynHtml($tag, $class, $attr, $id, $ref); |
|
248 | - } else { |
|
249 | - $hTag = new Tag\Html($tag, $class, $attr, $id); |
|
250 | - } |
|
251 | - $hTag->addContent($text); |
|
252 | - break; |
|
253 | - } |
|
254 | - $heir[$i] = $hTag; |
|
255 | - if ($i > 0) { |
|
256 | - $heir[$i - 1]->addChild($hTag); |
|
257 | - } else { |
|
258 | - $this->root[] = $hTag; |
|
259 | - } |
|
260 | - } else { |
|
261 | - throw new ParseError( |
|
262 | - "Unable to parse line {$this->lineNo}\n\"$line\"/" . |
|
263 | - preg_last_error(), |
|
264 | - ); |
|
265 | - } |
|
266 | - } |
|
267 | - $this->lineNo++; |
|
268 | - } |
|
269 | - } |
|
270 | - |
|
271 | - function parseQueryString($qs) |
|
272 | - { |
|
273 | - $out = []; |
|
274 | - foreach (explode('&', $qs) as $s) { |
|
275 | - $kv = explode('=', $s, 2); |
|
276 | - $out[urldecode($kv[0])] = isset($kv[1]) ? urldecode($kv[1]) : null; |
|
277 | - } |
|
278 | - return $out; |
|
279 | - } |
|
280 | - |
|
281 | - function output($minify = false) |
|
282 | - { |
|
283 | - $out = "<?php\nuse Seufert\\Hamle;\n?>"; |
|
284 | - foreach ($this->root as $tag) { |
|
285 | - $out .= $tag->render(0, $minify); |
|
286 | - } |
|
287 | - return $out; |
|
288 | - } |
|
289 | - |
|
290 | - function consumeBlock($indent) |
|
291 | - { |
|
292 | - $out = []; |
|
293 | - $m = []; |
|
294 | - while ( |
|
295 | - $this->lineNo + 1 < $this->lineCount && |
|
296 | - (!trim($this->lines[$this->lineNo + 1]) || |
|
297 | - preg_match( |
|
298 | - '/^(\s){' . $indent . '}((\s)+[^\s].*)$/', |
|
299 | - $this->lines[$this->lineNo + 1], |
|
300 | - $m, |
|
301 | - )) |
|
302 | - ) { |
|
303 | - if (trim($this->lines[$this->lineNo + 1])) { |
|
304 | - $out[] = $m[2]; |
|
305 | - } |
|
306 | - $this->lineNo++; |
|
307 | - } |
|
308 | - return $out; |
|
309 | - } |
|
310 | - |
|
311 | - function indentLevel($indent) |
|
312 | - { |
|
313 | - if (!isset($this->indents)) { |
|
314 | - $this->indents = []; |
|
315 | - } |
|
316 | - if (!count($this->indents)) { |
|
317 | - $this->indents = [0 => $indent]; |
|
318 | - // Key = indent level, Value = Depth in spaces |
|
319 | - return 0; |
|
320 | - } |
|
321 | - foreach ($this->indents as $k => $v) { |
|
322 | - if ($v == $indent) { |
|
323 | - $this->indents = array_slice($this->indents, 0, $k + 1); |
|
324 | - return $k; |
|
325 | - } |
|
326 | - } |
|
327 | - $this->indents[] = $indent; |
|
328 | - return max(array_keys($this->indents)); |
|
329 | - } |
|
330 | - |
|
331 | - function getLineNo() |
|
332 | - { |
|
333 | - return $this->lineNo; |
|
334 | - } |
|
335 | -} |
@@ -54,282 +54,3 @@ |
||
54 | 54 | */ |
55 | 55 | |
56 | 56 | const REGEX_PARSE_LINE = <<<'ENDREGEX' |
57 | - /^(\s*)(?:(?:([a-zA-Z0-9-]*)((?:[\.#!][\w\-\_]+)*)(\[(?:(?:\{\$[^\}]+\})?[^\\\]{]*?(?:\\.)*?(?:{[^\$])*?)+\])?)|([_\/]{1,3})|([\|:\$]\w+)|({?\$[^}]+}?)|)(?: (.*))?$/ |
|
58 | - ENDREGEX; |
|
59 | - |
|
60 | - /** |
|
61 | - * @var int Current Line Number |
|
62 | - */ |
|
63 | - protected $lineNo; |
|
64 | - /** |
|
65 | - * @var int Total Lines in File |
|
66 | - */ |
|
67 | - protected $lineCount; |
|
68 | - |
|
69 | - function __construct() |
|
70 | - { |
|
71 | - $this->init(); |
|
72 | - } |
|
73 | - |
|
74 | - /** |
|
75 | - * Clear Lines, and Line Number, so if output is |
|
76 | - * called, no output will be produced |
|
77 | - */ |
|
78 | - protected function init() |
|
79 | - { |
|
80 | - $this->lines = []; |
|
81 | - $this->lineNo = 0; |
|
82 | - $this->lineCount = 0; |
|
83 | - $this->root = []; |
|
84 | - } |
|
85 | - |
|
86 | - protected function loadLines($s) |
|
87 | - { |
|
88 | - $this->lines = explode("\n", str_replace("\r", '', $s)); |
|
89 | - $this->lineCount = count($this->lines); |
|
90 | - $this->lineNo = 0; |
|
91 | - } |
|
92 | - |
|
93 | - function parseFilter(ParseFilter $filter) |
|
94 | - { |
|
95 | - foreach ($this->root as $k => $tag) { |
|
96 | - $this->root[$k] = $filter->filterTag($tag); |
|
97 | - } |
|
98 | - } |
|
99 | - |
|
100 | - function parseSnip($s) |
|
101 | - { |
|
102 | - //save root tags |
|
103 | - /** @var Tag[] $roots */ |
|
104 | - $roots = $this->root; |
|
105 | - $this->root = []; |
|
106 | - $this->loadLines($s); |
|
107 | - $this->procLines(); |
|
108 | - $this->root = array_merge($roots, $this->root); |
|
109 | - } |
|
110 | - |
|
111 | - function applySnip() |
|
112 | - { |
|
113 | - /** @var Tag\Snippet[] $fwdSnip */ |
|
114 | - $fwdSnip = []; |
|
115 | - /** @var Tag\Snippet[] $revSnip */ |
|
116 | - $revSnip = []; |
|
117 | - /** @var Tag[] $roots */ |
|
118 | - $roots = []; |
|
119 | - foreach ($this->root as $snip) { |
|
120 | - if ($snip instanceof Tag\Snippet) { |
|
121 | - if ($snip->getType() == 'append') { |
|
122 | - array_unshift($revSnip, $snip); |
|
123 | - } else { |
|
124 | - $fwdSnip[] = $snip; |
|
125 | - } |
|
126 | - } else { |
|
127 | - $roots[] = $snip; |
|
128 | - } |
|
129 | - } |
|
130 | - foreach ($fwdSnip as $snip) { |
|
131 | - foreach ($roots as $root) { |
|
132 | - $snip->apply($root); |
|
133 | - } |
|
134 | - } |
|
135 | - foreach ($revSnip as $snip) { |
|
136 | - foreach ($roots as $root) { |
|
137 | - $snip->apply($root); |
|
138 | - } |
|
139 | - } |
|
140 | - $this->root = $roots; |
|
141 | - } |
|
142 | - |
|
143 | - /** |
|
144 | - * Parse HAMLE template, from a string |
|
145 | - * @param string $s String to parse |
|
146 | - */ |
|
147 | - function str($s) |
|
148 | - { |
|
149 | - $this->init(); |
|
150 | - $this->loadLines($s); |
|
151 | - $this->procLines(); |
|
152 | - } |
|
153 | - |
|
154 | - function procLines() |
|
155 | - { |
|
156 | - /* @var $heir Tag[] Tag Heirachy Array */ |
|
157 | - $heir = []; |
|
158 | - while ($this->lineNo < $this->lineCount) { |
|
159 | - $line = $this->lines[$this->lineNo]; |
|
160 | - if (trim($line)) { |
|
161 | - if (preg_match(self::REGEX_PARSE_LINE, $line, $m)) { |
|
162 | - if (false !== strpos($m[1], "\t")) { |
|
163 | - throw new ParseError( |
|
164 | - 'Tabs are not supported in templates at this time', |
|
165 | - ); |
|
166 | - } |
|
167 | - $indent = strlen($m[1]); |
|
168 | - $tag = isset($m[2]) ? ($tag = $m[2]) : ''; |
|
169 | - $classid = isset($m[3]) ? $m[3] : ''; |
|
170 | - $params = str_replace( |
|
171 | - ['\[', '\]', '\\&'], |
|
172 | - ['[', ']', '%26'], |
|
173 | - isset($m[4]) ? $m[4] : '', |
|
174 | - ); |
|
175 | - $textcode = isset($m[5]) ? $m[5] : ''; |
|
176 | - $text = isset($m[8]) ? $m[8] : ''; |
|
177 | - $code = isset($m[6]) ? $m[6] : ''; |
|
178 | - $i = self::indentLevel($indent); |
|
179 | - unset($m[0]); |
|
180 | - switch (strlen($code) ? $code[0] : ($textcode ? $textcode : '')) { |
|
181 | - case '|': //Control Tag |
|
182 | - if ($code == '|snippet') { |
|
183 | - $hTag = new Tag\Snippet($text); |
|
184 | - } elseif ($code == '|form') { |
|
185 | - $hTag = new Tag\Form($text); |
|
186 | - } elseif ($code == '|formhint') { |
|
187 | - $hTag = new Tag\FormHint(); |
|
188 | - } elseif ($code == '|else') { |
|
189 | - $hTag = new Tag\Control(substr($code, 1), $heir[$i - 1]); |
|
190 | - $hTag->setVar($text); |
|
191 | - } else { |
|
192 | - $hTag = new Tag\Control(substr($code, 1)); |
|
193 | - $hTag->setVar($text); |
|
194 | - } |
|
195 | - break; |
|
196 | - case ':': //Filter Tag |
|
197 | - $hTag = new Tag\Filter(substr($code, 1)); |
|
198 | - $hTag->addContent($text, Text::TOKEN_CODE); |
|
199 | - foreach ($this->consumeBlock($indent) as $l) { |
|
200 | - $hTag->addContent($l, Text::TOKEN_CODE); |
|
201 | - } |
|
202 | - break; |
|
203 | - case '_': //String Tag |
|
204 | - case '__': //Unescape String Tag |
|
205 | - case '___': //Unescape String Tag (with unescaped vars) |
|
206 | - $hTag = new Tag\Text($textcode); |
|
207 | - $hTag->addContent($text); |
|
208 | - break; |
|
209 | - case '___': //Unescape String Tag |
|
210 | - $hTag = new Tag\Text($textcode); |
|
211 | - $hTag->addContent($text); |
|
212 | - break; |
|
213 | - case '/': // HTML Comment |
|
214 | - case '//': // Non Printed Comment |
|
215 | - $hTag = new Tag\Comment($textcode); |
|
216 | - $hTag->addContent($text); |
|
217 | - foreach ($this->consumeBlock($indent) as $l) { |
|
218 | - $hTag->addContent($l, Text::TOKEN_CODE); |
|
219 | - } |
|
220 | - break; |
|
221 | - default: |
|
222 | - $attr = []; |
|
223 | - if (isset($params[0]) && $params[0] == '[') { |
|
224 | - $param = substr($params, 1, -1); |
|
225 | - $param = str_replace(['+', '\\&'], ['%2B', '%26'], $param); |
|
226 | - // parse_str($param, $attr); |
|
227 | - $attr = $this->parseQueryString($param); |
|
228 | - } |
|
229 | - $class = []; |
|
230 | - $id = ''; |
|
231 | - $ref = ''; |
|
232 | - preg_match_all('/[#\.!][a-zA-Z0-9\-\_]+/m', $classid, $cid); |
|
233 | - if (isset($cid[0])) { |
|
234 | - foreach ($cid[0] as $s) { |
|
235 | - if ($s[0] == '#') { |
|
236 | - $id = substr($s, 1); |
|
237 | - } |
|
238 | - if ($s[0] == '.') { |
|
239 | - $class[] = substr($s, 1); |
|
240 | - } |
|
241 | - if ($s[0] == '!') { |
|
242 | - $ref = substr($s, 1); |
|
243 | - } |
|
244 | - } |
|
245 | - } |
|
246 | - if ($ref) { |
|
247 | - $hTag = new Tag\DynHtml($tag, $class, $attr, $id, $ref); |
|
248 | - } else { |
|
249 | - $hTag = new Tag\Html($tag, $class, $attr, $id); |
|
250 | - } |
|
251 | - $hTag->addContent($text); |
|
252 | - break; |
|
253 | - } |
|
254 | - $heir[$i] = $hTag; |
|
255 | - if ($i > 0) { |
|
256 | - $heir[$i - 1]->addChild($hTag); |
|
257 | - } else { |
|
258 | - $this->root[] = $hTag; |
|
259 | - } |
|
260 | - } else { |
|
261 | - throw new ParseError( |
|
262 | - "Unable to parse line {$this->lineNo}\n\"$line\"/" . |
|
263 | - preg_last_error(), |
|
264 | - ); |
|
265 | - } |
|
266 | - } |
|
267 | - $this->lineNo++; |
|
268 | - } |
|
269 | - } |
|
270 | - |
|
271 | - function parseQueryString($qs) |
|
272 | - { |
|
273 | - $out = []; |
|
274 | - foreach (explode('&', $qs) as $s) { |
|
275 | - $kv = explode('=', $s, 2); |
|
276 | - $out[urldecode($kv[0])] = isset($kv[1]) ? urldecode($kv[1]) : null; |
|
277 | - } |
|
278 | - return $out; |
|
279 | - } |
|
280 | - |
|
281 | - function output($minify = false) |
|
282 | - { |
|
283 | - $out = "<?php\nuse Seufert\\Hamle;\n?>"; |
|
284 | - foreach ($this->root as $tag) { |
|
285 | - $out .= $tag->render(0, $minify); |
|
286 | - } |
|
287 | - return $out; |
|
288 | - } |
|
289 | - |
|
290 | - function consumeBlock($indent) |
|
291 | - { |
|
292 | - $out = []; |
|
293 | - $m = []; |
|
294 | - while ( |
|
295 | - $this->lineNo + 1 < $this->lineCount && |
|
296 | - (!trim($this->lines[$this->lineNo + 1]) || |
|
297 | - preg_match( |
|
298 | - '/^(\s){' . $indent . '}((\s)+[^\s].*)$/', |
|
299 | - $this->lines[$this->lineNo + 1], |
|
300 | - $m, |
|
301 | - )) |
|
302 | - ) { |
|
303 | - if (trim($this->lines[$this->lineNo + 1])) { |
|
304 | - $out[] = $m[2]; |
|
305 | - } |
|
306 | - $this->lineNo++; |
|
307 | - } |
|
308 | - return $out; |
|
309 | - } |
|
310 | - |
|
311 | - function indentLevel($indent) |
|
312 | - { |
|
313 | - if (!isset($this->indents)) { |
|
314 | - $this->indents = []; |
|
315 | - } |
|
316 | - if (!count($this->indents)) { |
|
317 | - $this->indents = [0 => $indent]; |
|
318 | - // Key = indent level, Value = Depth in spaces |
|
319 | - return 0; |
|
320 | - } |
|
321 | - foreach ($this->indents as $k => $v) { |
|
322 | - if ($v == $indent) { |
|
323 | - $this->indents = array_slice($this->indents, 0, $k + 1); |
|
324 | - return $k; |
|
325 | - } |
|
326 | - } |
|
327 | - $this->indents[] = $indent; |
|
328 | - return max(array_keys($this->indents)); |
|
329 | - } |
|
330 | - |
|
331 | - function getLineNo() |
|
332 | - { |
|
333 | - return $this->lineNo; |
|
334 | - } |
|
335 | -} |
@@ -20,7 +20,7 @@ discard block |
||
20 | 20 | $this->filters = $filters; |
21 | 21 | } |
22 | 22 | |
23 | - static function for(string $rel, array $filters): self |
|
23 | + static function for (string $rel, array $filters): self |
|
24 | 24 | { |
25 | 25 | return new self( |
26 | 26 | $rel === '>' ? Hamle::REL_CHILD : Hamle::REL_PARENT, |
@@ -31,9 +31,9 @@ discard block |
||
31 | 31 | public function apply(string $s): string |
32 | 32 | { |
33 | 33 | $s = |
34 | - $s . |
|
35 | - "->hamleRel({$this->rel}," . |
|
36 | - Query::queryParams($this->filters, true) . |
|
34 | + $s. |
|
35 | + "->hamleRel({$this->rel},". |
|
36 | + Query::queryParams($this->filters, true). |
|
37 | 37 | ')'; |
38 | 38 | if ($this->chain) { |
39 | 39 | $s = $this->chain->apply($s); |
@@ -37,7 +37,7 @@ |
||
37 | 37 | Throwable $previous = null |
38 | 38 | ) { |
39 | 39 | ///@todo Include Line number & file name within parse error exceptions |
40 | - $message .= ', on line ' . Hamle\Hamle::getLineNo() . ' in file ?.hamle'; |
|
40 | + $message .= ', on line '.Hamle\Hamle::getLineNo().' in file ?.hamle'; |
|
41 | 41 | parent::__construct($message, $code, $previous); |
42 | 42 | } |
43 | 43 | } |
@@ -72,7 +72,7 @@ discard block |
||
72 | 72 | ]); |
73 | 73 | } catch (SyntaxError $e) { |
74 | 74 | throw new ParseError( |
75 | - 'Unable to parse:' . $s . "\n\n" . $e->getMessage(), |
|
75 | + 'Unable to parse:'.$s."\n\n".$e->getMessage(), |
|
76 | 76 | 0, |
77 | 77 | $e, |
78 | 78 | ); |
@@ -132,7 +132,7 @@ discard block |
||
132 | 132 | { |
133 | 133 | $func = $filter['func']; |
134 | 134 | if (method_exists(Filter::class, $func)) { |
135 | - $func = Filter::class . '::' . $func; |
|
135 | + $func = Filter::class.'::'.$func; |
|
136 | 136 | } elseif ( |
137 | 137 | in_array($func, ['round', 'strtoupper', 'strtolower', 'ucfirst']) |
138 | 138 | ) { |
@@ -148,7 +148,7 @@ discard block |
||
148 | 148 | } |
149 | 149 | $args = join( |
150 | 150 | ',', |
151 | - array_map(function ($v) { |
|
151 | + array_map(function($v) { |
|
152 | 152 | if (is_array($v) && $v['type'] ?? false === 'expr') { |
153 | 153 | return self::renderExpr($v); |
154 | 154 | } else { |
@@ -157,9 +157,9 @@ discard block |
||
157 | 157 | }, $filter['args']), |
158 | 158 | ); |
159 | 159 | if (strlen($args)) { |
160 | - $args = ',' . $args; |
|
160 | + $args = ','.$args; |
|
161 | 161 | } |
162 | - $o = "$func($o" . $args . ')'; |
|
162 | + $o = "$func($o".$args.')'; |
|
163 | 163 | if ($filter['chain'] ?? false) { |
164 | 164 | $o = self::addFilter($o, $filter['chain']); |
165 | 165 | } |
@@ -168,21 +168,21 @@ discard block |
||
168 | 168 | |
169 | 169 | static function renderScopeThis($n) |
170 | 170 | { |
171 | - $o = 'Hamle\Scope::get()->hamleGet(' . self::varToCode($n['name']) . ')'; |
|
171 | + $o = 'Hamle\Scope::get()->hamleGet('.self::varToCode($n['name']).')'; |
|
172 | 172 | $o = self::addParams($o, $n['param'] ?? []); |
173 | 173 | return $o; |
174 | 174 | } |
175 | 175 | |
176 | 176 | static function renderScopeId($n) |
177 | 177 | { |
178 | - $o = 'Hamle\Scope::get(' . $n['id'] . ')'; |
|
178 | + $o = 'Hamle\Scope::get('.$n['id'].')'; |
|
179 | 179 | $o = self::addParams($o, $n['param'] ?? []); |
180 | 180 | return $o; |
181 | 181 | } |
182 | 182 | |
183 | 183 | static function renderScopeName($n) |
184 | 184 | { |
185 | - $o = 'Hamle\Scope::getName(' . self::varToCode($n['name']) . ')'; |
|
185 | + $o = 'Hamle\Scope::getName('.self::varToCode($n['name']).')'; |
|
186 | 186 | $o = self::addParams($o, $n['param'] ?? []); |
187 | 187 | return $o; |
188 | 188 | } |
@@ -190,7 +190,7 @@ discard block |
||
190 | 190 | static function addParams(string $o, array $params) |
191 | 191 | { |
192 | 192 | while ($params['type'] ?? null === 'sub') { |
193 | - $o .= '->hamleGet(' . self::varToCode($params['name']) . ')'; |
|
193 | + $o .= '->hamleGet('.self::varToCode($params['name']).')'; |
|
194 | 194 | $params = $params['params'] ?? []; |
195 | 195 | } |
196 | 196 | return $o; |
@@ -200,11 +200,11 @@ discard block |
||
200 | 200 | { |
201 | 201 | $r = $rel === 'child' ? Hamle::REL_CHILD : Hamle::REL_PARENT; |
202 | 202 | $o = |
203 | - $o . |
|
204 | - '->hamleRel(' . |
|
205 | - self::varToCode($r) . |
|
206 | - ',' . |
|
207 | - self::queryParams($query, true) . |
|
203 | + $o. |
|
204 | + '->hamleRel('. |
|
205 | + self::varToCode($r). |
|
206 | + ','. |
|
207 | + self::queryParams($query, true). |
|
208 | 208 | ')'; |
209 | 209 | return $o; |
210 | 210 | } |
@@ -266,7 +266,7 @@ discard block |
||
266 | 266 | self::varToCode($limit), |
267 | 267 | self::varToCode($offset), |
268 | 268 | ]; |
269 | - return 'Hamle\Run::modelTypeId(' . join(',', $opt) . ')'; |
|
269 | + return 'Hamle\Run::modelTypeId('.join(',', $opt).')'; |
|
270 | 270 | } |
271 | 271 | |
272 | 272 | static function renderQuery($n) |
@@ -287,7 +287,7 @@ discard block |
||
287 | 287 | } elseif ($id !== null) { |
288 | 288 | $o = self::queryId($n['query']); |
289 | 289 | } else { |
290 | - $o = 'Hamle\Run::modelTypeTags(' . self::queryParams($n['query']) . ')'; |
|
290 | + $o = 'Hamle\Run::modelTypeTags('.self::queryParams($n['query']).')'; |
|
291 | 291 | } |
292 | 292 | if ($n['sub'] ?? []) { |
293 | 293 | $o = self::addRel($o, $n['sub'], $n['rel']); |
@@ -314,7 +314,7 @@ discard block |
||
314 | 314 | $o = self::renderQuery($expr['body']); |
315 | 315 | break; |
316 | 316 | default: |
317 | - throw new \RuntimeException('Invalid Node: ' . $expr['body']['type']); |
|
317 | + throw new \RuntimeException('Invalid Node: '.$expr['body']['type']); |
|
318 | 318 | } |
319 | 319 | if ($expr['body']['filter'] ?? false) { |
320 | 320 | $o = self::addFilter($o, $expr['body']['filter']); |
@@ -334,16 +334,16 @@ discard block |
||
334 | 334 | } |
335 | 335 | break; |
336 | 336 | case 'scopeName': |
337 | - $out .= '<?=' . self::renderScopeName($node) . '?>'; |
|
337 | + $out .= '<?='.self::renderScopeName($node).'?>'; |
|
338 | 338 | break; |
339 | 339 | case 'scopeThis': |
340 | - $out .= '<?=' . self::renderScopeThis($node) . '?>'; |
|
340 | + $out .= '<?='.self::renderScopeThis($node).'?>'; |
|
341 | 341 | break; |
342 | 342 | case 'expr': |
343 | - $out .= '<?=' . self::renderExpr($node) . '?>'; |
|
343 | + $out .= '<?='.self::renderExpr($node).'?>'; |
|
344 | 344 | break; |
345 | 345 | default: |
346 | - throw new \RuntimeException('Invalid Node:' . $node['type']); |
|
346 | + throw new \RuntimeException('Invalid Node:'.$node['type']); |
|
347 | 347 | } |
348 | 348 | } |
349 | 349 | return $out; |
@@ -372,7 +372,7 @@ discard block |
||
372 | 372 | $out[] = self::renderExpr($node); |
373 | 373 | break; |
374 | 374 | default: |
375 | - throw new \RuntimeException('Invalid Node:' . $node['type']); |
|
375 | + throw new \RuntimeException('Invalid Node:'.$node['type']); |
|
376 | 376 | } |
377 | 377 | } |
378 | 378 | return join('.', $out); |
@@ -380,7 +380,7 @@ discard block |
||
380 | 380 | |
381 | 381 | function doEval() |
382 | 382 | { |
383 | - return eval('use Seufert\Hamle; return ' . $this->toPHP() . ';'); |
|
383 | + return eval('use Seufert\Hamle; return '.$this->toPHP().';'); |
|
384 | 384 | } |
385 | 385 | |
386 | 386 | static function varToCode($var) |
@@ -388,9 +388,9 @@ discard block |
||
388 | 388 | if (is_array($var)) { |
389 | 389 | $code = []; |
390 | 390 | foreach ($var as $key => $value) { |
391 | - $code[] = self::varToCode($key) . '=>' . self::varToCode($value); |
|
391 | + $code[] = self::varToCode($key).'=>'.self::varToCode($value); |
|
392 | 392 | } |
393 | - return 'array(' . implode(',', $code) . ')'; //remove unnecessary coma |
|
393 | + return 'array('.implode(',', $code).')'; //remove unnecessary coma |
|
394 | 394 | } |
395 | 395 | if (is_bool($var)) { |
396 | 396 | return $var ? 'TRUE' : 'FALSE'; |
@@ -402,16 +402,16 @@ discard block |
||
402 | 402 | return $var->toPHP(); |
403 | 403 | } |
404 | 404 | if (strpos($var, "\n") !== false) { |
405 | - return '"' . |
|
405 | + return '"'. |
|
406 | 406 | str_replace( |
407 | 407 | ['$', '"', '\\', "\n"], |
408 | 408 | ['\$', '\\"', '\\\\', '\\n'], |
409 | 409 | $var, |
410 | - ) . |
|
410 | + ). |
|
411 | 411 | '"'; |
412 | 412 | } |
413 | - return "'" . |
|
414 | - str_replace(['$', "'", '\\'], ['$', "\\'", '\\\\'], $var) . |
|
413 | + return "'". |
|
414 | + str_replace(['$', "'", '\\'], ['$', "\\'", '\\\\'], $var). |
|
415 | 415 | "'"; |
416 | 416 | } |
417 | 417 |