-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathindex.php
More file actions
182 lines (141 loc) · 4.31 KB
/
index.php
File metadata and controls
182 lines (141 loc) · 4.31 KB
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
<?php
/**
* Sass compiler
*
* @see https://scssphp.github.io/scssphp/docs/
*/
$html->sass = function ($content = '', $options = []) use ($html) {
static $sass;
if (!is_string($content)) return;
if (!$sass) {
require_once __DIR__ . '/loader.php';
$sass = new \Tangible\ScssPhp\Compiler();
/**
* When the compiler detects a multibyte character in the Sass source code,
* it adds by default a @charset rule or "byte-order mark", which are only
* valid for CSS stylesheet as a file. The following option prevents it.
*
* @see https://developer.mozilla.org/en-US/docs/Web/CSS/@charset
* @see https://en.wikipedia.org/wiki/Byte_order_mark#UTF-8
*/
$sass->setCharset(false);
}
// Minified output by default
$minify = !isset($options['minify']) || $options['minify'] !== false;
$sass->setOutputStyle(
$minify
? \Tangible\ScssPhp\OutputStyle::COMPRESSED
: \Tangible\ScssPhp\OutputStyle::EXPANDED
);
$current_path = isset($options['path']) ? $options['path'] : '';
$sass->setImportPaths(
!empty($current_path) ? [$current_path] : []
);
/**
* Pass variables to Sass compiler
*
* Previously using $sass->addVariables( $vars ), but ScssPhp 1.x no longer
* accepts unprocessed values. Convert known types manually and pass
* as Sass variable declarations.
*
* @see scssphp/Compiler.php, addVariables()
* @see scssphp/ValueConverter.php, fromPhp()
*/
if (isset($options['variables'])) {
$vars = '';
foreach ($options['variables'] as $key => $value) {
try {
if (is_string($value)) {
/**
* Quoted string, or other types (boolean, number, etc.) already
* converted to string
*/
} elseif (is_array($value)) {
/**
* Convert to Sass map or list
*/
$value = $html->convert_array_to_sass_map_or_list($value);
} else {
/**
* Convert other types: will throw error on invalid value
*/
$value = \Tangible\ScssPhp\ValueConverter::fromPhp($value);
}
$vars .= "\$$key: $value;\n";
} catch (\Exception $error) {
$vars .= $html->convert_error_message_to_css_comment($error, $options);
}
}
$content = $vars . $content;
}
$css = '';
try {
$css = $sass->compileString($content)->getCss();
} catch (\Exception $error) {
$css = $html->convert_error_message_to_css_comment($error, $options);
}
return $css;
};
$html->convert_array_to_sass_map_or_list = function ($value) {
$value = json_encode($value);
$value = (!empty($value) && ($value[0] === '{' || $value[0] === '['
))
? '(' . substr($value, 1, strlen($value) - 2) . ')'
: '()' // Empty map
;
return $value;
};
$html->convert_error_message_to_css_comment = function ($error, $options) {
$message = $error->getMessage();
$message = str_replace('(stdin) ', '', $message);
$source = isset($options['source'])
? (is_a($options['source'], 'WP_Post')
? "Error in \"{$options['source']->post_name}\" - Post type: {$options['source']->post_type}, ID: {$options['source']->ID}"
: (is_string($options['source'])
? $options['source']
: ''
)
)
: (isset($options['path'])
? $options['path'] // Template file
: ''
);
$css = "/**\n";
if (!empty($source)) $css .= " * $source\n";
$css .= " * $message\n";
$css .= " */\n";
return $css;
};
/**
* <style type=sass>
*/
$html->register_style_type('sass', function ($content, $options = []) use ($html) {
if (!isset($options['path'])) {
$options['path'] = $html->get_current_context('path');
}
$content = $html->sass($content, $options);
return isset($options['render']) ? $html->render($content) : $content;
});
/**
* Render and enqueue Sass file
*/
$html->enqueue_sass_file = function ($file_path_or_options = [], $args = []) use ($html) {
if (is_string($file_path_or_options)) {
$options = [
'src' => $file_path_or_options
];
}
$options = $options + [
// Defaults
];
foreach ($options as $key => $value) $$key = $value;
if (empty($src)) return;
try {
$css = $html->sass(file_get_contents($src), [
'path' => dirname($src)
]);
} catch (\Throwable $th) {
return;
}
$html->enqueue_inline_style($css);
};