-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathperoxide.engine
456 lines (386 loc) · 12.8 KB
/
peroxide.engine
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
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
<?php
/**
* Initialize peroxide
*/
function peroxide_init($theme) {
$file = dirname($theme->filename) . '/template.php';
if (file_exists($file)) {
include_once "./$file";
}
// Set Haml parser options
if (!empty($theme->info['peroxide']['options']['haml'])) {
_peroxide_set_haml_options($theme, $theme->info['peroxide']['options']['haml']);
}
else {
_peroxide_set_haml_options($theme);
}
// Set Sass parser options
if (!empty($theme->info['peroxide']['options']['sass'])) {
_peroxide_set_sass_options($theme, $theme->info['peroxide']['options']['sass']);
}
else {
_peroxide_set_sass_options($theme);
}
// Initialize parsers and render Sass
_peroxide_init();
// If the theme implements hook_css_alter(), assume it calls peroxide_css_alter()
if(!function_exists("{$theme->name}_css_alter")) {
_peroxide_scan_sass($theme);
}
_peroxide_scan_less($theme);
}
/**
* The extension for our templates
*/
function peroxide_extension() {
return ".haml";
}
/**
* We're handling HAML template files
*/
function peroxide_theme($existing, $type, $theme, $path) {
$templates = drupal_find_theme_functions($existing, array('haml', $theme));
$templates += drupal_find_theme_templates($existing, '.haml', $path);
return $templates;
}
/**
* Render a HAML template
*/
function peroxide_render_template($template, $vars) {
// Attempt to make a logical cache structure, if that doesn't work then throw it out the window
// Assuming directory is always set this should handle multiple themes well
if (isset($vars['directory'])) {
$cached_haml_path = file_default_scheme() . '://peroxide/haml_c/' . basename($vars['directory']);
}
else {
$cached_haml_path = file_default_scheme() . '://peroxide/haml_c/';
}
// Make sure that the directory we're placing css files in exists, if it doesn't exist attempt to create it
_peroxide_check_directory($cached_haml_path);
// Retrieve options for the Haml parser
$options = _peroxide_get_haml_options();
// Extract Variables
extract($vars, EXTR_SKIP);
// Render the template
ob_start();
$parser = new HamlParser($options);
include $parser->parse($template, $cached_haml_path, 0755, '.haml', '.tpl.php');
$contents = ob_get_contents();
ob_end_clean();
// Return contents
return $contents;
}
/**
* Implements hook_css_alter. Call it from your theme's hook_css_alter.
*
* Processes and .sass/.scss files in the stylesheets[] array.
*/
function peroxide_css_alter(&$css) {
$themes = list_themes();
$theme_info = $themes[$GLOBALS['theme']];
foreach($css as $fn => $info) {
$pathinfo = pathinfo($fn);
if($pathinfo['extension'] == 'scss' || $pathinfo['extension'] == 'sass') {
$new_fn = _peroxide_process_sass($theme_info, $fn);
unset($css[$fn]);
if($new_fn) {
$info['data'] = $new_fn;
$info['preprocess'] = FALSE;
$css[$new_fn] = $info;
}
}
}
}
/**
* Process a file from the stylesheets[] array. Called from peroxide_css_alter().
* The new file will be in the same directory, with the extension .sass-cache.css
*
* @param $theme Theme info object
* @param $sass Filename of the sass/scss file (from stylesheets[])
* @returns The filename of the processed file
*/
function _peroxide_process_sass($theme, $sass) {
$info = pathinfo($sass);
// We put the new file in the same place because it's easier than reimplementing Drupal's url() rewriting
$css_file = "{$info['dirname']}/{$info['filename']}.sass-cache.css";
if(filemtime($css_file) < filemtime($file)) {
return $css_file;
}
// Try and build the file with the Ruby version
@exec(
'sass '.($info['extension'] == 'scss' ? '--scss ' : '').
escapeshellarg($sass).' '.escapeshellarg($css_file),
$op, $ret
);
if($ret == 0) {
return $css_file;
}
try {
// Setup the Sass Parser
$options = _peroxide_get_sass_options($theme);
$parser = new SassParser($options);
$css = $parser->parse($sass)->render();
}
catch (SassException $e) {
drupal_set_message($e->getMessage(), 'error');
drupal_set_message($e->getTraceAsString(), 'error');
}
file_put_contents($css_file, $css);
return $css_file;
}
/**
* Initialize the Haml and Sass Parsers
*
*/
function _peroxide_init() {
$path = drupal_get_path('theme_engine', 'peroxide');
include_once $path . 'phamlp/haml/HamlParser.php';
// Test for Ruby version first
@exec("sass -v", $op, $ret);
if($ret != 0) {
include_once $path . 'phamlp/sass/SassParser.php';
}
include_once $path . 'lessphp/lessc.inc.php';
}
/**
* Scan for sass files, produce css files and then add them to the page
*/
function _peroxide_scan_sass($theme) {
// Setup initial file paths
$path = drupal_get_path('theme', $theme->name);
$cached_css_path = file_default_scheme() . '://peroxide/css/' . $theme->name;
// Make sure that the directory we're placing css files in exists, if it doesn't exist attempt to create it
_peroxide_check_directory($cached_css_path);
// Setup the Sass Parser
$options = _peroxide_get_sass_options($theme);
$parser = new SassParser($options);
// Read information about sass files from the theme's info file
if (!empty($theme->info['sass'])) {
foreach ($theme->info['sass'] as $media => $sassy) {
foreach ($sassy as $sass) {
$sass_path = $path . '/' . $sass;
$info = pathinfo($sass_path);
$css_file = $cached_css_path . '/' . $info['filename'] . '.css';
try {
$css = $parser->parse($sass_path)->render();
}
catch (SassException $e) {
drupal_set_message($e->getMessage(), 'error');
drupal_set_message($e->getTraceAsString(), 'error');
}
// rewrite asset paths if the theme's settings specify to do so
if ($theme->info['peroxide']['options']['sass']['rewrite asset paths'] == 'true'
|| (is_array($theme->info['peroxide']['options']['sass']['rewrite asset paths'])
&& in_array($sass, $theme->info['peroxide']['options']['sass']['rewrite asset paths']))) {
$matches = array();
if (preg_match_all('/url\([\'"](?P<url>.+[\'"])\)/', $css, $matches)) {
foreach ($matches['url'] as $url) {
$url = str_replace(array("'", '"'), '', $url); // remove single and double quotes which sometimes remain
// for now, just pop off the last element since there's never a time you'd reference a directory and not a file inside a css url()
$url = explode('/', $url);
$filename = array_pop($url);
$url = implode('/', $url);
// calculate the new relative path
$sass_dir = dirname($sass_path); // if sass[all][] = src/mysass.sass is specified, $sass_dir is "src"
$new_url = peroxide_calculate_path_difference($sass_dir . '/' . $url, $cached_css_path) . "/$filename";
// do the actual replacement. we either have to do str_replace twice or preg_replace once.
$css = str_replace("url('$url/$filename')", "url('$new_url')", $css);
$css = str_replace("url(\"$url/$filename\")", "url(\"$new_url\")", $css);
}
}
}
// cache CSS in a file
file_put_contents($css_file, $css);
if ($media != 'compile-only') {
drupal_add_css($css_file, 'theme', $media);
}
}
}
}
}
function _peroxide_scan_less($theme) {
// Setup initial file paths
$path = drupal_get_path('theme', $theme->name);
$cached_css_path = file_default_scheme() . '://peroxide/css/' . $theme->name;
// Read information about sass files from the theme's info file
if (!empty($theme->info['less'])) {
foreach ($theme->info['less'] as $media => $lessc) {
foreach ($lessc as $less) {
$less_path = $path . '/' . $less;
$info = pathinfo($less_path);
$css_file = $cached_css_path . '/' . $info['filename'] . ".css";
// Compile the less file
try {
lessc::ccompile($less_path, $css_file);
}
catch (Exception $e) {
drupal_set_message($e->getMessage(), 'error');
}
// Add to the page
drupal_add_css($css_file, 'theme', $media);
}
}
}
}
/**
* Check to see if a directory exists, if it doesn't
* attempt to recursively create the necessary directories.
*/
function _peroxide_check_directory($path) {
$dir_exists = file_prepare_directory($path);
if (!$dir_exists) {
$result = mkdir($path, 0755, TRUE);
if (!$result) {
drupal_set_message('You must have your Drupal files directory correctly configured to use peroxide.', 'error');
return;
}
}
else {
$result = TRUE;
}
return $result;
}
/**
* Set options for the Haml parser.
*/
function _peroxide_set_haml_options($theme = array(), $options = array()) {
$set_options = &drupal_static(__FUNCTION__);
// If no theme was passed in then return the options that have been set
if (!empty($set_options)) {
return $set_options;
}
// Merge options from theme's info file with the defaults
$set_options = array_merge(_peroxide_default_haml_options(), $options);
// Allow modules & running theme to alter Haml parser options
peroxide_alter('haml_options', $set_options, $theme);
return $set_options;
}
/**
* Get options for the Haml parser
*/
function _peroxide_get_haml_options() {
return _peroxide_set_haml_options();
}
/**
* Default options for the Haml parser.
*/
function _peroxide_default_haml_options() {
$options = array(
'style' => 'nested',
);
return $options;
}
/**
* Set options for the sass parser
*/
function _peroxide_set_sass_options($theme = array(), $options = array()) {
$set_options = drupal_static(__FUNCTION__);
if(!empty($set_options)) {
return $set_options;
}
// Merge options from theme's info file with the defaults
$set_options = array_merge(_peroxide_default_sass_options($theme), $options);
// Allow modules & running theme to alter options
peroxide_alter('sass_options', $set_options, $theme);
return $set_options;
}
/**
* Retrieve options for the Sass parser.
*/
function _peroxide_get_sass_options($theme) {
return _peroxide_set_sass_options($theme);
}
/**
* Default options for the Sass parser.
*/
function _peroxide_default_sass_options() {
$options = array(
'cache_location' => file_directory_temp(),
'css_location' => '/css',
'extensions' => array(
'compass' => array(),
),
);
return $options;
}
/**
* A function to allow alteration of underlying parser options by
* the theme using peroxide at runtime. Also allows modules
* to alter options as well.
*
* @param $hook
* The name of the alteration hook (e.g. haml_options)
* @param $theme
* Information for the theme.
* @param $options
* The options for the underlying parser.
*/
function peroxide_alter($hook, &$options, $theme) {
$hook = 'peroxide_' . $hook;
// Allow modules to alter options
drupal_alter($hook, $options, $theme);
// Allow theme to alter options
$fun = $theme->name . '_' . $hook . '_alter';
if (function_exists($fun)) {
$fun($options, $theme);
}
}
/**
* Finds the relative path from $root to $dest.
*
* @param $dest
* The destination.
* @param $root
* The starting point.
* @param $dir_sep
* The string used to separate directories. Defaults to '/'.
*/
function peroxide_calculate_path_difference($dest, $root = '', $dir_sep = '/') {
$root = explode($dir_sep, $root);
$dest = explode($dir_sep, $dest);
$path = '.';
$fix = '';
$diff = 0;
// calculate the relative path
for ($i = -1; ++$i < max(($rC = count($root)), ($dC = count($dest))); ) {
if (isset($root[$i]) and isset($dest[$i])) {
if ($diff) {
$path .= $dir_sep. '..';
$fix .= $dir_sep. $dest[$i];
continue;
}
if ($root[$i] != $dest[$i]) {
$diff = 1;
$path .= $dir_sep. '..';
$fix .= $dir_sep. $dest[$i];
continue;
}
}
else if (!isset($root[$i]) and isset($dest[$i])) {
for($j = $i-1; ++$j < $dC;) {
$fix .= $dir_sep. $dest[$j];
}
break;
}
else if (isset($root[$i]) and !isset($dest[$i])) {
for($j = $i-1; ++$j < $rC;) {
$fix = $dir_sep. '..'. $fix;
}
break;
}
}
$path .= $fix;
// clean the path of all unnecessary relative path components (".", "..")
$path = preg_replace('~/\./~', '/', $path); // resolve "."
// resolve ".."
$parts = array();
foreach (explode('/', preg_replace('~/+~', '/', $path)) as $part) {
if ($part == '..' && count($parts) > 0 && $parts[count($parts) - 1] != '..') {
$x = array_pop($parts);
}
else if ($part != '' && $part != '.') {
$parts[] = $part;
}
}
return implode('/', $parts);
}