| 1 |
#include <R.h> |
| 2 |
#include "compile.h" |
| 3 |
#include "sass/context.h" |
| 4 |
|
| 5 |
int get_index(SEXP list, const char* name) { |
| 6 |
SEXP names = PROTECT(Rf_getAttrib(list, R_NamesSymbol)); |
| 7 |
if (isNull(names)) { |
| 8 |
UNPROTECT(1); |
| 9 |
error("No named options in options list."); |
| 10 |
} |
| 11 |
|
| 12 |
int n = length(list); |
| 13 |
for (int i = 0; i < n; i++) { |
| 14 |
const char* element_name = CHAR(STRING_ELT(names, i)); |
| 15 |
if (strcmp(name, element_name) == 0) { |
| 16 |
UNPROTECT(1); |
| 17 |
return i; |
| 18 |
} |
| 19 |
} |
| 20 |
UNPROTECT(1); |
| 21 |
error("Option %s not found in option list.", name); |
| 22 |
} |
| 23 |
|
| 24 |
const char* get_char_element(SEXP list, const char* name) { |
| 25 |
int index = get_index(list, name); |
| 26 |
SEXP value = PROTECT(VECTOR_ELT(list, index)); |
| 27 |
if (TYPEOF(value) != STRSXP) { |
| 28 |
error("Invalid type for %s option. Expected string.", name); |
| 29 |
} |
| 30 |
value = asChar(value); |
| 31 |
UNPROTECT(1); |
| 32 |
return CHAR(value); |
| 33 |
} |
| 34 |
|
| 35 |
int get_bool_element(SEXP list, const char* name) { |
| 36 |
int index = get_index(list, name); |
| 37 |
SEXP value = PROTECT(VECTOR_ELT(list, index)); |
| 38 |
if (TYPEOF(value) != LGLSXP) { |
| 39 |
UNPROTECT(1); |
| 40 |
error("Invalid type for %s option. Expected logical.", name); |
| 41 |
} |
| 42 |
UNPROTECT(1); |
| 43 |
return asLogical(value); |
| 44 |
} |
| 45 |
|
| 46 |
int get_int_element(SEXP list, const char* name) { |
| 47 |
int index = get_index(list, name); |
| 48 |
SEXP value = PROTECT(VECTOR_ELT(list, index)); |
| 49 |
if ((TYPEOF(value) != INTSXP) && (TYPEOF(value) != REALSXP)) { |
| 50 |
UNPROTECT(1); |
| 51 |
error("Invalid type for %s option. Expected integer.", name); |
| 52 |
} |
| 53 |
int i = asInteger(value); |
| 54 |
if ((i < 0) || (i > 10)) { |
| 55 |
UNPROTECT(1); |
| 56 |
error("Invalid option. Integer value is out of range."); |
| 57 |
} |
| 58 |
UNPROTECT(1); |
| 59 |
return i; |
| 60 |
} |
| 61 |
|
| 62 |
void set_options(struct Sass_Options* sass_options, SEXP options) { |
| 63 |
int maximum_options = 13; |
| 64 |
if (length(options) > maximum_options) { |
| 65 |
error("Option list contains unsupported options."); |
| 66 |
} else if (length(options) < maximum_options) { |
| 67 |
error("Option list missing options."); |
| 68 |
} |
| 69 |
|
| 70 |
sass_option_set_output_path(sass_options, get_char_element(options, "output_path")); |
| 71 |
sass_option_set_output_style(sass_options, get_int_element(options, "output_style")); |
| 72 |
sass_option_set_is_indented_syntax_src(sass_options, get_bool_element(options, "indented_syntax")); |
| 73 |
sass_option_set_source_comments(sass_options, get_bool_element(options, "source_comments")); |
| 74 |
sass_option_set_omit_source_map_url(sass_options, get_bool_element(options, "omit_source_map_url")); |
| 75 |
sass_option_set_source_map_embed(sass_options, get_bool_element(options, "source_map_embed")); |
| 76 |
sass_option_set_source_map_contents(sass_options, get_bool_element(options, "source_map_contents")); |
| 77 |
sass_option_set_source_map_file(sass_options, get_char_element(options, "source_map_file")); |
| 78 |
sass_option_set_source_map_root(sass_options, get_char_element(options, "source_map_root")); |
| 79 |
sass_option_set_include_path(sass_options, get_char_element(options, "include_path")); |
| 80 |
sass_option_set_precision(sass_options, get_int_element(options, "precision")); |
| 81 |
sass_option_set_indent(sass_options, get_char_element(options, "indent")); |
| 82 |
sass_option_set_linefeed(sass_options, get_char_element(options, "linefeed")); |
| 83 |
} |
| 84 |
|
| 85 |
SEXP compile_file(SEXP file, SEXP options) { |
| 86 |
|
| 87 |
const char* input = CHAR(asChar(file)); |
| 88 |
|
| 89 |
|
| 90 |
struct Sass_File_Context* file_context = sass_make_file_context(input); |
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
struct Sass_Context* context = sass_file_context_get_context(file_context); |
| 96 |
struct Sass_Options* sass_options = sass_context_get_options(context); |
| 97 |
|
| 98 |
set_options(sass_options, options); |
| 99 |
|
| 100 |
int status = sass_compile_file_context(file_context); |
| 101 |
|
| 102 |
if (status != 0) |
| 103 |
error("%s", sass_context_get_error_message(context)); |
| 104 |
|
| 105 |
SEXP ret = PROTECT(mkString(sass_context_get_output_string(context))); |
| 106 |
|
| 107 |
sass_delete_file_context(file_context); |
| 108 |
|
| 109 |
UNPROTECT(1); |
| 110 |
return ret; |
| 111 |
} |
| 112 |
|
| 113 |
SEXP compile_data(SEXP data, SEXP options) { |
| 114 |
|
| 115 |
const char* data_string = CHAR(asChar(data)); |
| 116 |
|
| 117 |
char* input = sass_copy_c_string(data_string); |
| 118 |
|
| 119 |
|
| 120 |
struct Sass_Data_Context* data_context = sass_make_data_context(input); |
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
struct Sass_Context* context = sass_data_context_get_context(data_context); |
| 125 |
struct Sass_Options* sass_options = sass_context_get_options(context); |
| 126 |
|
| 127 |
set_options(sass_options, options); |
| 128 |
|
| 129 |
int status = sass_compile_data_context(data_context); |
| 130 |
|
| 131 |
if (status != 0) |
| 132 |
error("%s", sass_context_get_error_message(context)); |
| 133 |
|
| 134 |
SEXP ret = PROTECT(mkString(sass_context_get_output_string(context))); |
| 135 |
|
| 136 |
sass_delete_data_context(data_context); |
| 137 |
|
| 138 |
UNPROTECT(1); |
| 139 |
return ret; |
| 140 |
} |