Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

simplified parser

  • Loading branch information...
commit 9160c2e2552f9ed625c01b7d790769d629869b07 1 parent fe7f6e1
@lvivski authored
View
45 README.md
@@ -34,53 +34,10 @@ main() {
article text here
and here
''';
- Hart.compile(template, 'template.dart');
+ print(Hart.parse(template));
}
```
-then use them in your code.
-
-``` dart
-#import('template.dart'); // import generated template
-
-main() {
- print(
- new Template({ // initialize template with local variables
- 'title': 'TITLETITLETITLE',
- 'newClass': 'newClass',
- 'items': [1,2,3]
- }).render()
- );
-}
-```
-
-This will generate
-
-``` html
-<!DOCTYPE html>
-<html>
-<head>
-<title>TITLETITLETITLE</title>
-<script><![CDATA[
-foo
-]]></script>
-<script src="jquery.js"></script></head>
-<body class="one two three">
-<h1>Welcome</h1>
-<ul id="menu" class="newClass">
-<li id="list" class="first">one</li>
-<li>two</li>
-<li class="last">three</li>
-<li>
-<ul>
-<li>nested</li></ul></li></ul>
-<ul>
-<li>1</li>
-<li>2</li>
-<li>3</li></ul>
-<div class="article first">article text hereand here</div></body></html>
-```
-
## License
(The MIT License)
View
27 bin/hart
@@ -1,27 +0,0 @@
-#!/usr/bin/env dart
-
-#import('dart:io');
-#import('../lib/hart.dart');
-
-compile(files) {
- new Directory('./gen').create((){});
- files.forEach((filepath) {
- var file = new File(filepath);
- file.readAsText(Encoding.UTF_8, (data) {
- Hart.compile(data, './gen/' + file.name.split('.')[0] + '.dart');
- });
- });
-}
-
-main(){
- List<String> argv = new Options().arguments;
- String command = argv[0];
- List params = argv.getRange(1, argv.length - 1);
- switch(argv[0]) {
- case 'compile':
- compile(params);
- break;
- default:
- break;
- }
-}
View
9 template.haml → hart.dart 100644 → 100755
@@ -1,3 +1,8 @@
+#import('dart:io');
+#import('lib/hart.dart');
+
+main(){
+ print(Hart.parse('''
!!! 5
%html
%head
@@ -21,4 +26,6 @@
%li= item
%div.article.first
article text here
- and here
+ and here
+'''));
+}
View
43 lib/hart.dart
@@ -1,48 +1,9 @@
#library('hart');
-#import('dart:io');
#import('parser.dart');
class Hart {
- static compile(data, filepath) {
- new File(filepath).open(FileMode.WRITE, (file){
- String className = camelize(file.name.split('/').last().split('.')[0]);
- file.writeStringSync("""
-#library('template');
-
-#source('../lib/utils.dart');
-
-class ${className} {
- Map locals;
-
- ${className}(this.locals);
-
- noSuchMethod(String name, List args) {
- if (locals === null) {
- locals = {};
- }
- if (name.length > 4) {
- String prefix = name.substring(0, 4);
- String key = name.substring(4);
- if (prefix == "get:") {
- return locals[key];
- } else if (prefix == "set:") {
- locals[key] = args[0];
- }
- }
- }
-
- render () {
- return '''
-${new Parser(data).parsed}
- ''';
- }
-}
-""");
- });
- }
-
- static String camelize(String name) {
- return Strings.join(name.split(const RegExp(@'-|_')).map((part) => part[0].toUpperCase() + part.substring(1)), '');
+ static parse(String data) {
+ return new Parser(data).parsed;
}
}
View
7 lib/parser.dart
@@ -83,9 +83,10 @@ class Parser {
classes.add(next['val']);
break;
case 'attrs':
- const RegExp(@'(\w+) *:', ignoreCase:true).allMatches(next['val']).forEach((match){
+ Iterable<Match> matches = const RegExp(@'(\w+) *:', ignoreCase:true).allMatches(next['val']);
+ for (Match match in matches) {
buff.add(current['val'].replaceAll(match[1], "'${match[1]}'"));
- });
+ }
}
}
if (classes.length > 0) {
@@ -226,4 +227,4 @@ class Parser {
}
return buff.toString();
}
-}
+}
View
2  lib/utils.dart
@@ -1,3 +1,5 @@
+#library('utils');
+
class Filters {
static plain (str) => str;
static cdata (str) => '<![CDATA[\n${str}\n]]>';
Please sign in to comment.
Something went wrong with that request. Please try again.