11#include " source_map.hpp"
2+ #include " json.hpp"
23
34#ifndef SASS_CONTEXT
45#include " context.hpp"
@@ -13,45 +14,54 @@ namespace Sass {
1314 using std::ptrdiff_t ;
1415 SourceMap::SourceMap (const string& file) : current_position(Position(1 , 1 )), file(file) { }
1516
16- // taken from http://stackoverflow.com/a/7725289/1550314
17- std::string encodeJsonString (const std::string& input) {
18- std::ostringstream ss;
19- for (std::string::const_iterator iter = input.begin (); iter != input.end (); iter++) {
20- switch (*iter) {
21- case ' \\ ' : ss << " \\\\ " ; break ;
22- case ' "' : ss << " \\\" " ; break ;
23- case ' \b ' : ss << " \\ b" ; break ;
24- case ' \f ' : ss << " \\ f" ; break ;
25- case ' \n ' : ss << " \\ n" ; break ;
26- case ' \r ' : ss << " \\ r" ; break ;
27- case ' \t ' : ss << " \\ t" ; break ;
28- // is a legal escape in JSON
29- case ' /' : ss << " \\ /" ; break ;
30- default : ss << *iter; break ;
31- }
32- }
17+ string SourceMap::generate_source_map (Context &ctx) {
3318
34- return ss.str ();
35- }
19+ const bool include_sources = ctx.source_map_contents ;
20+ const vector<string> includes = ctx.include_links ;
21+ const vector<const char *> sources = ctx.sources ;
22+
23+ JsonNode *json_srcmap = json_mkobject ();
24+
25+ json_append_member (json_srcmap, " version" , json_mknumber (3 ));
26+
27+ const char *include = file.c_str ();
28+ JsonNode *json_include = json_mkstring (include);
29+ json_append_member (json_srcmap, " file" , json_include);
3630
37- string SourceMap::generate_source_map () {
38- string result = " {\n " ;
39- result += " \" version\" : 3,\n " ;
40- result += " \" file\" : \" " + encodeJsonString (file) + " \" ,\n " ;
41- result += " \" sources\" : [" ;
42- for (size_t i = 0 ; i < files.size (); ++i) {
43- result+=" \" " + encodeJsonString (files[i]) + " \" ," ;
31+ JsonNode *json_includes = json_mkarray ();
32+ for (size_t i = 0 ; i < source_index.size (); ++i) {
33+ const char *include = includes[source_index[i]].c_str ();
34+ JsonNode *json_include = json_mkstring (include);
35+ json_append_element (json_includes, json_include);
4436 }
45- if (!files.empty ()) result.erase (result.length () - 1 );
46- result += " ],\n " ;
47- result += " \" names\" : [],\n " ;
48- result += " \" mappings\" : \" " + serialize_mappings () + " \"\n " ;
49- result += " }" ;
37+ json_append_member (json_srcmap, " sources" , json_includes);
38+
39+ JsonNode *json_contents = json_mkarray ();
40+ if (include_sources) {
41+ for (size_t i = 1 ; i < source_index.size (); ++i) {
42+ const char *content = sources[source_index[i]];
43+ JsonNode *json_content = json_mkstring (content);
44+ json_append_element (json_contents, json_content);
45+ }
46+ }
47+ json_append_member (json_srcmap, " sourcesContent" , json_contents);
48+
49+ string mappings = serialize_mappings ();
50+ JsonNode *json_mappings = json_mkstring (mappings.c_str ());
51+ json_append_member (json_srcmap, " mappings" , json_mappings);
5052
53+ JsonNode *json_names = json_mkarray ();
54+ // so far we have no implementation for names
55+ // no problem as we do not alter any identifiers
56+ json_append_member (json_srcmap, " names" , json_names);
57+
58+ char *str = json_stringify (json_srcmap, " \t " );
59+ string result = string (str);
60+ free (str);
61+ json_delete (json_srcmap);
5162 return result;
5263 }
5364
54-
5565 string SourceMap::serialize_mappings () {
5666 string result = " " ;
5767
0 commit comments