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
  
/* 
  Author: Pontus Östlund <https://profiles.google.com/poppanator> 
*/ 
 
//! Sass is a scripting language that is interpreted into Cascading Style 
//! Sheets (CSS). This module is a glue for @tt{libsass@}. 
//! 
//! @seealso 
//!  SASS @url{http://sass-lang.com/@} 
 
#pike __REAL_VERSION__ 
#require constant(Tools@module@) 
 
//! @ignore 
inherit Tools@module@; 
//! @endignore 
 
//! Sass/SCSS compiler. 
//! 
//! @example 
//! @code 
//! Tools.Sass.Compiler compiler = Tools.Sass.Compiler(); 
//! 
//! // Allow for HTTP imports, disallowed by default. 
//! compiler->http_import = Tools.Sass.HTTP_IMPORT_ANY; 
//! 
//! // Minify the output and create a source map file. 
//! compiler->set_options(([ 
//!   "output_style" : Tools.Sass.STYLE_COMPRESSED, 
//!   "source_map_file" : "path/to/write/source.map" 
//! ])); 
//! 
//! if (mixed e = catch(compiler->compile_file("input.scss", "output.css"))) { 
//!   werror("Failed compiling input.scss to output.css\n"); 
//! } 
//! @endcode 
class Compiler 
{ 
  // NOTE: The only reason for this wrapper class is to be able to explicily 
  //       destruct the real compiler. There's a cyclic reference between 
  //       the compiler and the import callback in the real compiler so it 
  //       won't be destructed when it runs out of scope. If it can be solved 
  //       internally in the Low_Compiler this class no longer has a purpose 
 
  //! @ignore 
  protected Low_Compiler compiler; 
 
  protected void create() 
  { 
    compiler = Low_Compiler(); 
  } 
 
#define SETTER(T,P) \ 
  public void ` ## P ## =(T value) { compiler-> ## P = value; } 
 
#define GETTER(T,P) \ 
  public T ` ## P ## () { return compiler-> ## P; } 
 
#define GETTER_SETTER(T,P) \ 
  GETTER(T,P) \ 
  SETTER(T,P) 
 
  GETTER_SETTER(int(0..2),              http_import) 
  GETTER_SETTER(multiset(string(8bit)), http_import_allow_ct) 
  GETTER_SETTER(bool,                   check_file_access) 
  GETTER_SETTER(string(8bit),           include_path) 
  GETTER_SETTER(int(0..3),              output_style) 
  GETTER_SETTER(string(8bit),           source_map_file) 
  GETTER_SETTER(string(8bit),           source_map_root) 
  GETTER_SETTER(int(0..),               precision) 
  GETTER_SETTER(int(0..1),              source_comments) 
  GETTER_SETTER(int(0..1),              source_map_embed) 
  GETTER_SETTER(int(0..1),              omit_source_map_url) 
 
  mapping(string(8bit):string(8bit)) compile_file(string(8bit) input_file) 
  { 
    return compiler->compile_file(input_file); 
  } 
 
  variant void compile_file(string(8bit) input_file, string(8bit) output_file) 
  { 
    compiler->compile_file(input_file, output_file); 
  } 
 
  string(8bit) compile_string(string(8bit) source) 
  { 
    return compiler->compile_string(source); 
  } 
 
  void set_options(mapping(string(8bit):string(8bit)|int) opts) 
  { 
    compiler->set_options(opts); 
  } 
 
  protected void _destruct() 
  { 
    if (compiler) { 
      destruct(compiler); 
    } 
  } 
  //! @endignore 
} 
 
 
class Low_Compiler 
{ 
  //! @ignore 
  inherit Tools@module@.Api; 
  //! @endignore 
 
  //! @appears Tools.Sass.Compiler.http_import 
  //! 
  //! If a Sass file is importing an external URI this flag determines if 
  //! thats allowed at all, or if the content type of the imported file has 
  //! to be in @[http_import_allow_ct], or if anything goes. 
  //! Default is @[HTTP_IMPORT_NONE]. 
  //! 
  //! @seealso 
  //!  @[HTTP_IMPORT_NONE], @[HTTP_IMPORT_GREEDY] and 
  //!  @[HTTP_IMPORT_ANY]. 
  public int(0..2) http_import = HTTP_IMPORT_NONE; 
 
  //! @appears Tools.Sass.Compiler.http_import_allow_ct 
  //! 
  //! List of allowed content types if @[http_import] is set to 
  //! @[HTTP_IMPORT_GREEDY]. The default is to allow @tt{text/scss@} and 
  //! @tt{text/sass@}. 
  public multiset(string(8bit)) http_import_allow_ct = 
    (< "text/scss", "text/sass" >); 
 
  //! @appears Tools.Sass.Compiler.check_file_access 
  //! 
  //! Should file access be tested right away when paths are set or should that 
  //! be left to Sass to handle? The default value is @tt{true@}. 
  public bool check_file_access = true; 
 
 
  //! @ignore 
  protected void create() 
  { 
    // __set_importer_callback(__resolve_import); 
    this::__importer_cb = __resolve_import; 
  } 
  //! @endignore 
 
 
  //! @ignore 
  //! Resolve external imports in sass/scss files. 
  protected string(8bit) __resolve_import(string(8bit) path) 
  { 
    Standards.URI uri; 
 
    // If it's not an URI we assume it's a local import and we let Sass handle 
    // it. This could of course be a, by mistake, malformed URI, but then Sass 
    // will eventually throw. 
    if (catch (uri = Standards.URI(path))) { 
      return UNDEFINED; 
    } 
 
    if (http_import == HTTP_IMPORT_NONE) { 
      error("Imports over HTTP not allowed!\n"); 
    } 
 
    Protocols.HTTP.Query q = Protocols.HTTP.get_url(uri); 
 
    if (q->status / 100 != 2) { 
      error("Bad HTTP status (%d) for @import %q!\n", 
            q->status, (string) uri); 
    } 
 
    array(string) ct_parts = map(q->headers["content-type"]/";", 
                                 String.trim_all_whites); 
 
    if (http_import == HTTP_IMPORT_GREEDY) { 
      if (!http_import_allow_ct[ct_parts[0]]) { 
        error("Returned content type from import (%s) was %q. " 
              "Expected %s!\n", uri, ct_parts[0], 
              String.implode_nicely((array)http_import_allow_ct, "or")); 
      } 
    } 
 
    string(8bit) data = q->data(); 
 
    if (sizeof(ct_parts) > 1) { 
      sscanf(ct_parts[1], "%*s=%s", string charset); 
      // In case of charset="utf-8" or charset='utf-8', remove the "fnutts" 
      if (charset && charset[0] < 65) { 
        charset = charset[1..<1]; 
      } 
    } 
 
    return data; 
  } 
  //! @endignore 
 
 
  // Documented in the CMOD 
  void `include_path=(string(8bit) path) 
  { 
    if (check_file_access && stringp(path) && !Stdio.exist(path)) { 
      error("Include path %q does not exist!\n", path); 
    } 
 
    ::include_path = path; 
  } 
 
 
  // Documented in the CMOD 
  string(8bit) `include_path() 
  { 
    return ::include_path; 
  } 
 
  //! @appears Tools.Sass.Compiler.compile_file 
  //! 
  //! Compile the file @[input_file] and return the result 
  //! 
  //! @param input_file 
  //!  The SCSS file to compile 
  //! 
  //! @returns 
  //!  A mapping with the generated CSS and source mapping file if such is 
  //!  set to be generated 
  //! 
  //!  @mapping 
  //!   @member string(8bit) "css" 
  //!    The generated CSS 
  //!   @member string(8bit) "map" 
  //!    The generated source mapping data 
  //!  @endmapping 
  mapping(string(8bit):string(8bit)) compile_file(string(8bit) input_file) 
  { 
    if (check_file_access && !Stdio.exist(input_file)) { 
      error("Input file %q does not exist or isn't accessible!\n", 
            input_file); 
    } 
 
    return ::compile_file(input_file); 
  } 
 
  //! @appears Tools.Sass.Compiler.compile_file 
  //! 
  //! Compile the file @[input_file] and write the result to @[output_file]. 
  //! If a source mapping file is set to be generated either via 
  //! @[set_options()] or @[source_map_file] it will be written as per 
  //! the value set in the option. 
  //! 
  //! @param input_file 
  //!  The SCSS file to compile 
  //! @param output_file 
  //!  The name of the CSS file to save the result in. 
  variant void compile_file(string(8bit) input_file, string(8bit) output_file) 
  { 
    if (check_file_access && !Stdio.exist(input_file)) { 
      error("Input file %q does not exist or isn't accessible!\n", 
            input_file); 
    } 
 
    mapping(string(8bit):string(8bit)) val = ::compile_file(input_file); 
    Stdio.write_file(output_file, val->css); 
 
    if (val->map && source_map_file) { 
      Stdio.write_file(source_map_file, val->map); 
    } 
  } 
 
  //! @appears Tools.Sass.Compiler.set_options 
  //! 
  //! Set options to the SASS compiler. 
  //! 
  //! @param opts 
  //!  @mapping 
  //!   @member int "output_style" 
  //!    Any of the @[STYLE_NESTED], @[STYLE_EXPANDED], @[STYLE_COMPACT] 
  //!    or @[STYLE_COMPRESSED] constants. See also @[output_style]. 
  //! 
  //!   @member string(8bit) "include_path" 
  //!    Path to root of incude files. See also @[include_path]. 
  //! 
  //!   @member string(8bit) "source_map_file" 
  //!    File to write source map file to. 
  //!    See also @[source_map_file]. 
  //! 
  //!   @member bool "source_comments" 
  //!    Turn on/off comments in the output containing info about the source 
  //!    file - line numbers and such. Default of @tt{false@}. See also 
  //!    @[source_comments]. 
  //! 
  //!   @member bool "source_map_embed" 
  //!    Turn on/off if a source map should be embedded in the output or not. 
  //!    Default is @tt{false@}. See also @[source_map_embed]. 
  //! 
  //!   @member string(8bit) "source_map_root" 
  //!    Set the root path of the source files, relative to where the 
  //!    source.map file is written. 
  //!    See also @[source_map_root] 
  //! 
  //!   @member bool "omit_source_map_url" 
  //!    Omit the #sourceMappingURL or not. 
  //!    See also @[omit_source_map_url] 
  //! 
  //!   @member int "precision" 
  //!    Floating point precision. See also @[precision]. 
  //!  @endmapping 
  void set_options(mapping(string(8bit):string(8bit)|int) opts) 
  { 
    foreach (opts; string opt; string|int val) { 
      switch (opt) 
      { 
        case "output_style": 
          if (!(< STYLE_NESTED, STYLE_COMPRESSED, STYLE_COMPACT, 
                  STYLE_EXPANDED >)[val]) 
          { 
            error("Unrecognized output style value!\n"); 
          } 
          output_style = val; 
          break; 
 
        case "include_path": 
          include_path = val; 
          break; 
 
        case "source_map_file": 
          source_map_file = val; 
          break; 
 
        case "source_map_embed": 
          source_map_embed = val; 
          break; 
 
        case "source_map_root": 
          source_map_root = val; 
          break; 
 
        case "omit_source_map_url": 
          omit_source_map_url = val; 
          break; 
 
        case "source_comments": 
          source_comments = val; 
          break; 
 
        case "precision": 
          precision = val; 
          break; 
 
        default: 
          error("Unknown option %O!\n", opt); 
      } 
    } 
  } 
}