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
  
/* 
  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 
{ 
  //! @ignore 
  inherit Tools@module@.Api; 
  //! @endignore 
 
  //! 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 text/scss 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; 
 
 
  //! 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); 
  } 
  //! @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 != 200) { 
      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 (ct_parts[0] != "text/scss") { 
        error("Returned content type from import (%s) was %q. " 
              "Expected \"text/scss\"!\n", 
              uri, ct_parts[0]); 
      } 
    } 
 
    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 && !Stdio.exist(path)) { 
      error("Include path %q does not exist!\n", path); 
    } 
 
    ::include_path = path; 
  } 
 
 
  // Documented in the CMOD 
  string `include_path() 
  { 
    return ::include_path; 
  } 
 
 
  //! 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); 
  } 
 
  //! 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); 
    } 
  } 
 
 
  //! Compile the string @[source] 
  //! 
  //! @note 
  //!  If the @[source] contain @tt{@@import@} directives you have to 
  //!  explicitly set the include path via @[include_path]. 
  //! 
  //! @param source 
  //!  The string to compile 
  string(8bit) compile_string(string(8bit) source) 
  { 
    string(8bit) out = ::compile_string(source); 
    return out; 
  } 
 
 
  //! 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] 
  //!  @endmapping 
  void set_options(mapping(string(8bit):string(8bit)|int) opts) 
  { 
    foreach (opts; string opt; string|int val) { 
      switch (opt) 
      { 
        case "output_style": 
          if (!intp(val)) { 
            error("Value to `output_style` must be an integer!\n"); 
          } 
          if (!(< STYLE_NESTED, STYLE_COMPRESSED, STYLE_COMPACT, 
                  STYLE_EXPANDED >)[val]) 
          { 
            error("Unrecognized output style value!\n"); 
          } 
          output_style = val; 
          break; 
 
        case "include_path": 
          if (!stringp(val)) { 
            error("Value to `include_path` must be a string!\n"); 
          } 
          include_path = val; 
          break; 
 
        case "source_map_file": 
          if (!stringp(val)) { 
            error("Value to `source_map_file` must be a string!\n"); 
          } 
          source_map_file = val; 
          break; 
 
        case "source_map_embed": 
          if (!intp(val)) { 
            error("Value to  `source_map_embed` must be an int(0..1)!\n"); 
          } 
          source_map_embed = val; 
          break; 
 
        case "source_map_root": 
          if (!stringp(val)) { 
            error("Value to `source_map_root` must be a string!\n"); 
          } 
          source_map_root = val; 
          break; 
 
        case "omit_source_map_url": 
          if (!intp(val)) { 
            error("Value to `omit_source_map_url` must be an integer!\n"); 
          } 
          omit_source_map_url = val; 
          break; 
 
        case "source_comments": 
          if (!intp(val)) { 
            error("Value to `source_comments` must be an integer!\n"); 
          } 
          source_comments = val; 
          break; 
 
        default: 
          error("Unknown option %O!\n", opt); 
      } 
    } 
  } 
}