Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1404)

Unified Diff: pkg/csslib/lib/transform.dart

Issue 23576012: Added .scss transform (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: cleanup Created 7 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: pkg/csslib/lib/transform.dart
diff --git a/pkg/csslib/lib/transform.dart b/pkg/csslib/lib/transform.dart
new file mode 100644
index 0000000000000000000000000000000000000000..aa538980f71ecd2965e2e20e12c0424e888fef22
--- /dev/null
+++ b/pkg/csslib/lib/transform.dart
@@ -0,0 +1,65 @@
+// Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file
+// for details. All rights reserved. Use of this source code is governed by a
+// BSD-style license that can be found in the LICENSE file.
+
+/**
+ * Code transform for pre-processing CSS files any extension of .scss is
+ * transformed to runable CSS styling (nesting, calc, var, @define, @extends,
+ * @mixin, etc).
+ */
+library csslib.transform;
+
+import 'dart:async';
+
+import 'package:barback/barback.dart';
+import 'package:source_maps/span.dart' show SourceFile;
+import 'package:csslib/parser.dart' show parse, analyze;
+import 'package:csslib/visitor.dart' show StyleSheet, CssPrinter;
+
+class ScssTransformer extends Transformer {
+ Future<bool> isPrimary(Asset input) =>
+ new Future.value((input.id.extension == '.scss'));
Siggi Cherem (dart-lang) 2013/09/08 20:31:33 replace the above two lines with: /** Only run th
+
+ Future apply(Transform transform) {
+ return transform.primaryInput.readAsString().then((content) {
+ // Generate .css output file.
+ var id = transform.primaryInput.id.changeExtension('.css');
+
+ // Parse the .scss file.
+ var sourceFile = new SourceFile.text(id.path, content);
+ var stylesheet = _parseCss(content, sourceFile, transform.logger);
+
+ analyze([stylesheet]);
+
+ transform.addOutput(new Asset.fromString(id, _prettyPrint(stylesheet)));
+ });
+ }
+}
+
+StyleSheet _parseCss(String content, SourceFile sourceFile, logger) {
+ if (content.trim().isEmpty) return null;
+
+ var errors = [];
+
+ // TODO(terry): Add --checked when fully implemented and error handling as
+ // part of validation transform.
+ // TODO(terry): All warnings as errors.
+ var stylesheet = parse(content, errors: errors, options:
+ ['--warnings_as_errors', 'memory']);
+
+ // Note: errors aren't fatal in HTML (unless strict mode is on).
+ // So just print them as warnings.
+ for (var e in errors) {
+ logger.warning(e.message, e.span);
+ }
+
+ return stylesheet;
+}
+
+/** Pretty printer for CSS. */
+String _prettyPrint(StyleSheet ss) {
+ // Walks the style sheet tree and emits readable CSS.
+ var formatCss = (new CssPrinter()..visitTree(ss, pretty: true)).toString();
+ return "/***** Generated by SCSS transform. *****/\n\n$formatCss";
+}
+

Powered by Google App Engine
This is Rietveld 408576698