Add args support.

It doesn't really do anything yet.
This commit is contained in:
Natalie Weizenbaum 2016-08-29 22:52:56 -07:00 committed by Natalie Weizenbaum
parent 80e111eb56
commit 83c8c1a9b7
3 changed files with 43 additions and 5 deletions

View File

@ -4,14 +4,35 @@
import 'dart:io';
import 'package:args/args.dart';
import 'package:path/path.dart' as p;
import 'package:sass/src/parser.dart';
import 'package:sass/src/visitor/perform.dart';
import 'package:sass/src/visitor/serialize.dart';
void main(List<String> args) {
var parser = new Parser(new File(args.first).readAsStringSync(),
url: p.toUri(args.first));
var cssTree = new PerformVisitor().visitStylesheet(parser.parse());
print(toCss(cssTree));
var argParser = new ArgParser()
..addOption('precision', hide: true)
..addOption('style',
abbr: 's',
help: 'Output style.',
allowed: ['expanded'],
defaultsTo: 'expanded')
..addFlag('help',
abbr: 'h', help: 'Print this usage information.', negatable: false);
var options = argParser.parse(args);
if (options['help']) {
print("Compile Sass to CSS.\n");
print("Usage: sass <input>\n");
print(argParser.usage);
exit(64);
}
var file = options.rest.first;
var parser =
new Parser(new File(file).readAsStringSync(), url: p.toUri(file));
var cssTree = new PerformVisitor().visitStylesheet(parser.parse());
var css = toCss(cssTree);
if (css.isNotEmpty) print(css);
}

View File

@ -15,8 +15,8 @@ import 'interface/css.dart';
import 'interface/selector.dart';
import 'interface/value.dart';
String toCss(CssNode node) {
var visitor = new _SerializeCssVisitor();
String toCss(CssNode node, {OutputStyle style}) {
var visitor = new _SerializeCssVisitor(style: style);
node.accept(visitor);
var result = visitor._buffer.toString();
if (result.codeUnits.any((codeUnit) => codeUnit > 0x7F)) {
@ -42,10 +42,15 @@ String selectorToCss(Selector selector) {
class _SerializeCssVisitor
implements CssVisitor, ValueVisitor, SelectorVisitor {
final OutputStyle _style;
final _buffer = new StringBuffer();
var _indentation = 0;
_SerializeCssVisitor({OutputStyle style})
: _style = style ?? OutputStyle.expanded;
void visitStylesheet(CssStylesheet node) {
for (var child in node.children) {
child.accept(this);
@ -408,3 +413,14 @@ class _SerializeCssVisitor
_indentation--;
}
}
class OutputStyle {
static const expanded = const OutputStyle._("expanded");
static const nested = const OutputStyle._("nested");
final String _name;
const OutputStyle._(this._name);
String toString() => _name;
}

View File

@ -8,6 +8,7 @@ environment:
sdk: '>=1.8.0 <2.0.0'
dependencies:
args: "^0.13.0"
charcode: "^1.1.0"
collection: "^1.1.0"
path: "^1.0.0"