/
peroxide.engine
363 lines (308 loc) · 9.82 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
<?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();
_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_directory_path() . '/peroxide/haml_c/' . basename($vars['directory']);
}
else {
$cached_haml_path = file_directory_path() . '/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;
}
/**
* Initialize the Haml and Sass Parsers
*
*/
function _peroxide_init() {
$path = drupal_get_path('theme_engine', 'peroxide');
include_once $path . 'phamlp/haml/HamlParser.php';
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_directory_path() . '/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');
}
if (/*$theme->info['settings']['rewrite paths in cached css'] == */1) {
$matches = array();
if (preg_match_all('/url\((?P<url>[.]+)\)/', $css, $matches)) {
foreach ($matches['url'] as $url) {
$url = explode('/', $url);
$filename = array_pop($url); // 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 = implode('/', $url);
$new_url = peroxide_calculate_new_path($path . $url, $cached_css_path) . "/$filename";
$css = str_replace("url($url)", "url($new_url)", $css);
}
}
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_directory_path() . '/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_check_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()) {
static $set_options;
// If no theme was passed in then return the options that have been set
if (empty($theme)) {
if (empty($set_options)) {
return _peroxide_default_haml_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()) {
static $set_options;
// If no options were passed in then return the options that have been set
if (empty($options)) {
if (empty($set_options)) {
return _peroxide_default_sass_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' => $path . '/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($function)) {
$function($options, $theme);
}
}
function peroxide_calculate_new_path($dest, $root = '', $dir_sep = '/') {
$root = explode($dir_sep, $root);
$dest = explode($dir_sep, $dest);
$path = '.';
$fix = '';
$diff = 0;
/* calculate the relative path back to the theme SASS */
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 */
$path = preg_replace('~/\./~', '/', $path); // resolve "."
// resolve ".."
$parts = array();
foreach (explode('/', preg_replace('~/+~', '/', $path)) as $part) { //preg_replace('~/+~', '/', $path)
if ($part == '..' && count($parts) > 0 && $parts[count($parts) - 1] != '..') {
$x = array_pop($parts); echo "popping '$x'\n";
}
else if ($part != '' && $part != '.') {
$parts[] = $part; echo "adding '$part'\n";
}
}
return implode('/', $parts);
}