1b2e7d2016-11-23Pontus Östlund /* Author: Pontus Östlund <https://profiles.google.com/poppanator> */
8384a22016-12-03Pontus Östlund 
b9018d2016-11-29Pontus Östlund //! 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/@}
1b2e7d2016-11-23Pontus Östlund  #pike __REAL_VERSION__
b9018d2016-11-29Pontus Östlund #require constant(Tools@module@)
1b2e7d2016-11-23Pontus Östlund 
e1370d2016-12-01Pontus Östlund //! @ignore
1b2e7d2016-11-23Pontus Östlund inherit Tools@module@;
e1370d2016-12-01Pontus Östlund //! @endignore
1b2e7d2016-11-23Pontus Östlund  //! SCSS compiler
b9018d2016-11-29Pontus Östlund //! //! @example //! @code
8384a22016-12-03Pontus Östlund //! Tools.Sass.Compiler compiler = Tools.Sass.Compiler();
b9018d2016-11-29Pontus Östlund //! // 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
8384a22016-12-03Pontus Östlund class Compiler
1b2e7d2016-11-23Pontus Östlund {
8384a22016-12-03Pontus Östlund  //! @ignore
b9018d2016-11-29Pontus Östlund  inherit Tools@module@.Api;
8384a22016-12-03Pontus Östlund  //! @endignore
1b2e7d2016-11-23Pontus Östlund 
72603a2016-12-03Pontus Östlund  //! 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
8384a22016-12-03Pontus Östlund  //! to be text/scss or if anything goes. Default is @[HTTP_IMPORT_NONE].
72603a2016-12-03Pontus Östlund  //! //! @seealso
8384a22016-12-03Pontus Östlund  //! @[HTTP_IMPORT_NONE], @[HTTP_IMPORT_GREEDY] and //! @[HTTP_IMPORT_ANY]. public int(0..2) import_http = HTTP_IMPORT_NONE;
72603a2016-12-03Pontus Östlund  //! @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; }
8384a22016-12-03Pontus Östlund  if (import_http == HTTP_IMPORT_NONE) { error("Imports over HTTP not allowed!\n"); }
72603a2016-12-03Pontus Östlund  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);
8384a22016-12-03Pontus Östlund  if (import_http == HTTP_IMPORT_GREEDY) {
72603a2016-12-03Pontus Östlund  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
acafa02016-12-01Pontus Östlund  //! 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; }
72603a2016-12-03Pontus Östlund 
dd8a1a2016-11-29Pontus Östlund  //! Set options to the SASS compiler. @[opts]
1b2e7d2016-11-23Pontus Östlund  //! //! @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()].
e1370d2016-12-01Pontus Östlund  //!
1b2e7d2016-11-23Pontus Östlund  //! @member string "include_path" //! Path to root of incude files. See also @[set_include_path()].
e1370d2016-12-01Pontus Östlund  //!
1b2e7d2016-11-23Pontus Östlund  //! @member string "source_map_file" //! File to write source map file to. Only has effect in @[compile_file()]. //! See also @[set_source_map_file()].
e1370d2016-12-01Pontus Östlund  //!
1b2e7d2016-11-23Pontus Östlund  //! @member bool "source_comments" //! Turn on/off comments in the output containing info about the source
ef03332016-11-26Pontus Östlund  //! file - line numbers and such. Default of @tt{false@}. See also
1b2e7d2016-11-23Pontus Östlund  //! @[set_source_comments()].
e1370d2016-12-01Pontus Östlund  //!
1b2e7d2016-11-23Pontus Östlund  //! @member bool "source_map_embed" //! Turn on/off if a source map should be embedded in the output or not.
ef03332016-11-26Pontus Östlund  //! Default is @tt{false@}. See also @[set_source_map_embed()].
e1370d2016-12-01Pontus Östlund  //! //! @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()]
1b2e7d2016-11-23Pontus Östlund  //! @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)) {
18ad792016-11-24Pontus Östlund  error("Value to set_source_map_embed() must be an int(0..1)!\n");
1b2e7d2016-11-23Pontus Östlund  } set_source_map_embed(val); break;
18ad792016-11-24Pontus Östlund  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;
1b2e7d2016-11-23Pontus Östlund  case "source_comments": if (!intp(val)) { error("Value to set_source_comments() must be an integer!\n"); } set_source_comments(val); break;
18ad792016-11-24Pontus Östlund  default: error("Unknown option %O!\n", opt);
1b2e7d2016-11-23Pontus Östlund  } } } }