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 @@
namespace Sass {
using std::string;
using std::stringstream;
using std::endl;
string Document::emit_css(CSS_Style style) {
stringstream output;
for (int i = 0; i < statements.size(); ++i) {
switch (style) {
// case nested:
// statements[i].emit_nested_css(output, "");
// break;
case nested:
statements[i].emit_nested_css(output, "", 0);
break;
case expanded:
statements[i].emit_expanded_css(output, "");
break;
......
......@@ -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) {
switch (type) {
case value:
case selector:
if (!prefix.empty()) buf << " ";
buf << string(token);
......@@ -85,7 +132,6 @@ namespace Sass {
case values:
for (int i = 0; i < children.size(); ++i) {
buf << " " << string(children[i].token);
// children[i].token.stream_unquoted(buf);
}
break;
case rule:
......
......@@ -4,6 +4,8 @@
namespace Sass {
using std::vector;
using std::stringstream;
struct Node {
enum Node_Type {
null,
......@@ -33,6 +35,7 @@ namespace Sass {
void push_child(const Node& node);
void push_opt_child(const Node& node);
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[]) {
Document doc(argv[1], 0);
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;
return 0;
......
$blah: bloo blee;
$blip: "a 'red' and \"blue\" value";
/* top level comment -- should preserved */
/* top level comment -- should be preserved */
div {
/* another comment that should be preserved */
color: red;
background: blue;
$blux: hux;
$blux: hux; // gone!
span {
font-weight: bold;
a {
......@@ -17,6 +17,13 @@ div {
}
/* yet another comment that should be preserved */
display: inline-block;
} // gone!
/* the next selector should be indented two spaces */
empty {
not_empty {
blah: blah; // gone!
bloo: bloo;
}
}
p {
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