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
  
/* 
  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 
 
//! SCSS compiler 
//! 
//! @example 
//! @code 
//! Tools.Sass.SCSS compiler = Tools.Sass.SCSS(); 
//! // 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 SCSS 
{ 
  inherit Tools@module@.Api; 
 
  //! 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 @[EXTERNAL_IMPORT_NONE]. 
  //! 
  //! @seealso 
  //!  @[EXTERNAL_IMPORT_NONE], @[EXTERNAL_IMPORT_GREEDY] and 
  //!  @[EXTERNAL_IMPORT_ANY]. 
  public int(0..2) external_use = EXTERNAL_IMPORT_NONE; 
 
 
  //! @ignore 
  protected void create() 
  { 
    ::__set_importer_callback(__resolve_import); 
  } 
  //! @endignore 
 
  //! @ignore 
  //! Resolve external imports in sass files. 
  protected string __resolve_import(string path) 
  { 
    Standards.URI uri; 
 
    if (catch (uri = Standards.URI(path))) { 
      return UNDEFINED; 
    } 
 
    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 (external_use == EXTERNAL_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" and alike. 
      if (charset && charset[0] < 65) { 
        charset = charset[1..<1]; 
      } 
    } 
 
    return data; 
  } 
  //! @endignore 
 
 
  //! 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 "css" 
  //!    The generated CSS 
  //!   @member string "map" 
  //!    The generated source mapping data 
  //!  @endmapping 
  mapping(string:string) compile_file(string input_file) 
  { 
    mapping(string:string) val = ::compile_file(input_file); 
    return val; 
  } 
 
  //! 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 @[set_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 input_file, string output_file) 
  { 
    mapping(string:string) val = ::compile_file(input_file); 
    Stdio.write_file(output_file, val->css); 
 
    if (val->map) { 
      string smap_path = ::get_source_map_file(); 
      Stdio.write_file(smap_path, val->map); 
    } 
  } 
 
 
  //! Compile the string @[source] 
  //! 
  //! @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. @[opts] 
  //! 
  //! @param opts 
  //!  @mapping 
  //!   @member int "output_style" 
  //!    Any of the @[STYLE_NESTED], @[STYLE_EXPANDED], @[STYLE_COMPACT] 
  //!    or @[STYLE_COMPRESSED] constants. See also @[set_output_style()]. 
  //! 
  //!   @member string "include_path" 
  //!    Path to root of incude files. See also @[set_include_path()]. 
  //! 
  //!   @member string "source_map_file" 
  //!    File to write source map file to. Only has effect in @[compile_file()]. 
  //!    See also @[set_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 
  //!    @[set_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 @[set_source_map_embed()]. 
  //! 
  //!   @member string "source_map_root" 
  //!    Set the root path of the source files, relative to where the 
  //!    source.map file is written. 
  //!    See also @[set_source_map_root()] 
  //! 
  //!   @member bool "omit_source_map" 
  //!    Omit the #sourceMappingURL or not. 
  //!    See also @[set_omit_source_map_url()] 
  //!  @endmapping 
  void set_options(mapping(string:string|int) opts) 
  { 
    foreach (opts; string opt; string|int val) { 
      switch (opt) 
      { 
        case "output_style": 
          if (!intp(val)) { 
            error("Value to set_output_style() must be an integer!\n"); 
          } 
          set_output_style(val); 
          break; 
 
        case "include_path": 
          if (!stringp(val)) { 
            error("Value to set_include_path() must be a string!\n"); 
          } 
          set_include_path(val); 
          break; 
 
        case "source_map_file": 
          if (!stringp(val)) { 
            error("Value to set_source_map_file() must be a string!\n"); 
          } 
          set_source_map_file(val); 
          break; 
 
        case "source_map_embed": 
          if (!intp(val)) { 
            error("Value to set_source_map_embed() must be an int(0..1)!\n"); 
          } 
          set_source_map_embed(val); 
          break; 
 
        case "source_map_root": 
          if (!stringp(val)) { 
            error("Value to set_source_map_root() must be a string!\n"); 
          } 
          set_source_map_root(val); 
          break; 
 
        case "omit_source_map": 
          if (!intp(val)) { 
            error("Value to set_omit_source_map_url() must be an integer!\n"); 
          } 
          set_omit_source_map_url(val); 
          break; 
 
        case "source_comments": 
          if (!intp(val)) { 
            error("Value to set_source_comments() must be an integer!\n"); 
          } 
          set_source_comments(val); 
          break; 
 
        default: 
          error("Unknown option %O!\n", opt); 
      } 
    } 
  } 
}