-
Notifications
You must be signed in to change notification settings - Fork 3
/
Filter.php
312 lines (269 loc) · 10.2 KB
/
Filter.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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
<?php
/**
* The Tale Pug Default Filters.
*
* Contains a static filter class that provides some basic
* filters for use inside Pug-files. You can define own filters
* by passing the 'filter'-option to the Compiler you compile
* your Pug files with
*
* This file is part of the Tale Pug Template Engine for PHP
*
* LICENSE:
* The code of this file is distributed under the MIT license.
* If you didn't receive a copy of the license text, you can
* read it here https://github.com/Talesoft/tale-pug/blob/master/LICENSE.md
*
* @category Presentation
* @package Tale\Pug
* @author Torben Koehn <torben@talesoft.codes>
* @author Talesoft <info@talesoft.codes>
* @copyright Copyright (c) 2015-2016 Torben Köhn (http://talesoft.codes)
* @license https://github.com/Talesoft/tale-pug/blob/master/LICENSE.md MIT License
* @version 1.4.5
* @link http://jade.talesoft.codes/docs/files/Filter.html
* @since File available since Release 1.0
*/
namespace Tale\Pug;
use Tale\Pug\Parser\Node;
/**
* Provides basic, static filters for the compiler.
*
* The only reason this class exists is so that you don't have do write
* this basic stuff yourself.
*
* This class provides the following filters for the Compiler:
*
* :plain => Converts to plain text
* :js => Converts to <script></script>
* :css => Converts to <style></style>
* :php => converts to <?php ? >
* :markdown => converts to markdown-HTML
* :coffee => converts to CoffeeScript-JavaScript
* :less => converts to LESS CSS
* :stylus => converts to Stylus CSS
* :sass => converts to SASS CSS
*
* @category Presentation
* @package Tale\Pug
* @author Torben Koehn <torben@talesoft.codes>
* @author Talesoft <info@talesoft.codes>
* @copyright Copyright (c) 2015-2016 Torben Köhn (http://talesoft.codes)
* @license https://github.com/Talesoft/tale-pug/blob/master/LICENSE.md MIT License
* @version 1.4.5
* @link http://jade.talesoft.codes/docs/classes/Tale.Pug.Filter.html
* @since File available since Release 1.0
*/
class Filter
{
/**
* Wraps a node $node in tags using $tag.
*
* Respects indentation and new-lines based on $indent and $newLine
*
* @param string $tag the tag to wrap the node in
* @param Node $node the node to be wrapped
* @param string $indent the indentation to use on each child
* @param string $newLine the new-line to append after each line
*
* @return string the wrapped PTHML-string
*/
public static function wrapTag($tag, Node $node, $indent, $newLine)
{
return "<$tag>".$newLine.self::filterPlain($node, $indent, $newLine)
.$indent."</$tag>".$newLine;
}
/**
* Similar to wrapTag, but rather puts PHP-instruction-tags around the text.
*
* This will create working PHP expressions.
*
* If <?php or ? > are already found, they will be trimmed and re-appended
* correctly to avoid failing nested expressions (<?php can't be used
* _inside_ <?php)
*
* @param Node $node the node to be wrapped
* @param string $indent the indentation to use on each child
* @param string $newLine the new-line to append after each line
*
* @return string the wrapped PHP-string
*/
public static function wrapCode(Node $node, $indent, $newLine)
{
$text = self::filterPlain($node, $indent, $newLine);
$text = preg_replace(['/^\s*<\?php ?/i', '/\?>\s*$/'], '', $text);
return $indent.'<?php '.$newLine.$text.$newLine.$indent.'?>'.$newLine;
}
/**
* A plain-text filter that just corrects indentation and new-lines.
*
* @param Node $node the node to be wrapped
* @param string $indent the indentation to use on each child
* @param string $newLine the new-line to append after each line
*
* @return string the wrapped PTHML-string
*/
public static function filterPlain(Node $node, $indent, $newLine)
{
$text = trim($node->text());
//Normalize newlines to $newLine and append our indent
$i = 0;
return implode($newLine, array_map(function ($value) use (
$indent, $newLine, &$i
) {
if (strlen($indent) < 1 && $i++ !== 0 && strlen($value) > 0) {
//Make sure we separate with at least one white-space
$indent = ' ';
}
return $indent.trim($value);
}, explode("\n", $text)));
}
/**
* Wraps the content in <style></style> tags and corrects indentation
* and new-lines.
*
* @param Node $node the node to be wrapped
* @param string $indent the indentation to use on each child
* @param string $newLine the new-line to append after each line
*
* @return string The wrapped PTHML-string
*/
public static function filterStyle(Node $node, $indent, $newLine)
{
return self::wrapTag('style', $node, $indent, $newLine);
}
/**
* Wraps the content in <script></script> tags and corrects indentation
* and new-lines.
*
* @param Node $node the node to be wrapped
* @param string $indent the indentation to use on each child
* @param string $newLine the new-line to append after each line
*
* @return string the wrapped PTHML-string
*/
public static function filterScript(Node $node, $indent, $newLine)
{
return self::wrapTag('script', $node, $indent, $newLine);
}
/**
* Wraps the content in PHP-compiler tags and corrects indentation
* and new-lines.
*
* @param Node $node the node to be wrapped
* @param string $indent the indentation to use on each child
* @param string $newLine the new-line to append after each line
*
* @return string the wrapped PHP-string
*/
public static function filterCode(Node $node, $indent, $newLine)
{
return self::wrapCode($node, $indent, $newLine);
}
/**
* Compiles the markdown content to HTML.
*
* @param Node $node the node to be compiled
* @param string $indent the indentation to use on each child
* @param string $newLine the new-line to append after each line
*
* @return string the wrapped HTML-string
* @throws Compiler\Exception when the Parsedown package is not installed
*/
public static function filterMarkdown(Node $node, $indent, $newLine)
{
if (!class_exists('Parsedown'))
throw new Compiler\Exception(
"Failed to compile Markdown: "
."Please install the erusev/parsedown composer package"
);
$parsedown = new \Parsedown();
return $parsedown->text($node->text());
}
/**
* Compiles the CoffeeScript content to JavaScript
*
* @param Node $node the node to be compiled
* @param string $indent the indentation to use on each child
* @param string $newLine the new-line to append after each line
*
* @return string the wrapped JavaScript-HTML-string
* @throws Compiler\Exception when the CoffeeScript package is not installed
*/
public static function filterCoffeeScript(Node $node, $indent, $newLine)
{
if (!class_exists('CoffeeScript\\Compiler'))
throw new Compiler\Exception(
"Failed to compile CoffeeScript: "
."Please install the coffeescript/coffeescript composer package"
);
$js = \CoffeeScript\Compiler::compile($node->text(), [
'header' => '',
'filename' => 'Imported-at-('.$node->line.':'.$node->offset.').coffee'
]);
return '<script>'.$newLine.$indent.$js.$newLine.$indent.'</script>';
}
/**
* Compiles the LESS content to CSS
*
* @param Node $node the node to be compiled
* @param string $indent the indentation to use on each child
* @param string $newLine the new-line to append after each line
*
* @return string the wrapped Less-CSS-string
* @throws Compiler\Exception when the LESS package is not installed
*/
public static function filterLess(Node $node, $indent, $newLine)
{
if (!class_exists('lessc'))
throw new Compiler\Exception(
"Failed to compile LESS: "
."Please install the leafo/lessphp composer package"
);
$less = new \lessc;
$css = $less->compile($node->text());
return '<style>'.$newLine.$indent.$css.$newLine.$indent.'</style>';
}
/**
* Compiles the Stylus content to CSS
*
* @param Node $node the node to be compiled
* @param string $indent the indentation to use on each child
* @param string $newLine the new-line to append after each line
*
* @return string the wrapped Stylus-CSS-string
* @throws Compiler\Exception when the Stylus package is not installed
*/
public static function filterStylus(Node $node, $indent, $newLine)
{
if (!class_exists('Stylus\\Stylus'))
throw new Compiler\Exception(
"Failed to compile Stylus: "
."Please install the neemzy/stylus composer package"
);
$stylus = new \Stylus\Stylus;
$css = $stylus->fromString($node->text())->toString();
return '<style>'.$newLine.$indent.$css.$newLine.$indent.'</style>';
}
/**
* Compiles the SASS content to CSS
*
* @param Node $node the node to be compiled
* @param string $indent the indentation to use on each child
* @param string $newLine the new-line to append after each line
*
* @return string the wrapped SASS-CSS-string
* @throws Compiler\Exception when the Stylus package is not installed
*/
public static function filterSass(Node $node, $indent, $newLine)
{
if (!class_exists('Leafo\\ScssPhp\\Compiler'))
throw new Compiler\Exception(
"Failed to compile SASS: "
."Please install the leafo/scssphp composer package"
);
$sass = new \Leafo\ScssPhp\Compiler;
$css = $sass->compile($node->text());
return '<style>'.$newLine.$indent.$css.$newLine.$indent.'</style>';
}
}