Commit 13f04721 by Aaron Leung

Getting the nested-style emitter working on basic cases. Still buggy on multiply…

Getting the nested-style emitter working on basic cases. Still buggy on multiply nested empty rulesets.
parent 1e476642
...@@ -3,14 +3,15 @@ ...@@ -3,14 +3,15 @@
namespace Sass { namespace Sass {
using std::string; using std::string;
using std::stringstream; using std::stringstream;
using std::endl;
string Document::emit_css(CSS_Style style) { string Document::emit_css(CSS_Style style) {
stringstream output; stringstream output;
for (int i = 0; i < statements.size(); ++i) { for (int i = 0; i < statements.size(); ++i) {
switch (style) { switch (style) {
// case nested: case nested:
// statements[i].emit_nested_css(output, ""); statements[i].emit_nested_css(output, "", 0);
// break; break;
case expanded: case expanded:
statements[i].emit_expanded_css(output, ""); statements[i].emit_expanded_css(output, "");
break; break;
......
...@@ -68,9 +68,56 @@ namespace Sass { ...@@ -68,9 +68,56 @@ namespace Sass {
} }
} }
void Node::emit_nested_css(stringstream& buf,
const string& prefix,
size_t depth) {
switch(type) {
case selector:
if (!prefix.empty()) buf << " ";
buf << string(token);
break;
case comment:
buf << string(2 * depth, ' ') << string(token);
if (depth == 0) buf << endl;
break;
case property:
buf << string(token) << ":";
break;
case values:
for (int i = 0; i < children.size(); ++i) {
buf << " " << string(children[i].token);
}
break;
case rule:
buf << string(2 * depth, ' ');
children[0].emit_nested_css(buf, prefix, depth);
children[1].emit_nested_css(buf, prefix, depth);
buf << ";";
break;
case clauses:
if (!children.empty()) {
buf << " {";
for (int i = 0; i < children.size() && children[i].type != Node::null; ++i) {
buf << endl;
children[i].emit_nested_css(buf, prefix, depth + 1);
}
buf << " }" << endl;
}
for (int i = 0; i < opt_children.size(); ++i)
opt_children[i].emit_nested_css(buf, prefix, depth + (children.empty() ? 0 : 1));
break;
case ruleset:
buf << string(2 * depth, ' ') << prefix;
if (!(children[1].children.empty()))
children[0].emit_nested_css(buf, prefix, depth);
string newprefix(prefix.empty() ? prefix : prefix + " ");
children[1].emit_nested_css(buf, newprefix + string(children[0].token), depth);
break;
}
}
void Node::emit_expanded_css(stringstream& buf, const string& prefix) { void Node::emit_expanded_css(stringstream& buf, const string& prefix) {
switch (type) { switch (type) {
case value:
case selector: case selector:
if (!prefix.empty()) buf << " "; if (!prefix.empty()) buf << " ";
buf << string(token); buf << string(token);
...@@ -85,7 +132,6 @@ namespace Sass { ...@@ -85,7 +132,6 @@ namespace Sass {
case values: case values:
for (int i = 0; i < children.size(); ++i) { for (int i = 0; i < children.size(); ++i) {
buf << " " << string(children[i].token); buf << " " << string(children[i].token);
// children[i].token.stream_unquoted(buf);
} }
break; break;
case rule: case rule:
......
...@@ -4,6 +4,8 @@ ...@@ -4,6 +4,8 @@
namespace Sass { namespace Sass {
using std::vector; using std::vector;
using std::stringstream;
struct Node { struct Node {
enum Node_Type { enum Node_Type {
null, null,
...@@ -33,6 +35,7 @@ namespace Sass { ...@@ -33,6 +35,7 @@ namespace Sass {
void push_child(const Node& node); void push_child(const Node& node);
void push_opt_child(const Node& node); void push_opt_child(const Node& node);
void dump(unsigned int depth = 0); void dump(unsigned int depth = 0);
void emit_expanded_css(std::stringstream& buf, const string& prefix); void emit_nested_css(stringstream& buf, const string& prefix, size_t depth);
void emit_expanded_css(stringstream& buf, const string& prefix);
}; };
} }
\ No newline at end of file
...@@ -13,7 +13,10 @@ int main(int argc, char* argv[]) { ...@@ -13,7 +13,10 @@ int main(int argc, char* argv[]) {
Document doc(argv[1], 0); Document doc(argv[1], 0);
doc.parse_scss(); doc.parse_scss();
string output = doc.emit_css(doc.expanded); // string output = doc.emit_css(doc.expanded);
// cout << output;
// cout << endl;
string output = doc.emit_css(doc.nested);
cout << output; cout << output;
return 0; return 0;
......
$blah: bloo blee; $blah: bloo blee;
$blip: "a 'red' and \"blue\" value"; $blip: "a 'red' and \"blue\" value";
/* top level comment -- should preserved */ /* top level comment -- should be preserved */
div { div {
/* another comment that should be preserved */ /* another comment that should be preserved */
color: red; color: red;
background: blue; background: blue;
$blux: hux; $blux: hux; // gone!
span { span {
font-weight: bold; font-weight: bold;
a { a {
...@@ -17,6 +17,13 @@ div { ...@@ -17,6 +17,13 @@ div {
} }
/* yet another comment that should be preserved */ /* yet another comment that should be preserved */
display: inline-block; display: inline-block;
} // gone!
/* the next selector should be indented two spaces */
empty {
not_empty {
blah: blah; // gone!
bloo: bloo;
}
} }
p { p {
padding: 10px 8%; padding: 10px 8%;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment