Skip to content

Commit

Permalink
simplified parser
Browse files Browse the repository at this point in the history
  • Loading branch information
lvivski committed May 29, 2012
1 parent fe7f6e1 commit 9160c2e
Show file tree
Hide file tree
Showing 6 changed files with 17 additions and 116 deletions.
45 changes: 1 addition & 44 deletions README.md
Expand Up @@ -34,53 +34,10 @@ main() {
article text here article text here
and 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 ## License


(The MIT License) (The MIT License)
Expand Down
27 changes: 0 additions & 27 deletions bin/hart

This file was deleted.

9 changes: 8 additions & 1 deletion template.haml → hart.dart 100644 → 100755
@@ -1,3 +1,8 @@
#import('dart:io');
#import('lib/hart.dart');

main(){
print(Hart.parse('''
!!! 5 !!! 5
%html %html
%head %head
Expand All @@ -21,4 +26,6 @@
%li= item %li= item
%div.article.first %div.article.first
article text here article text here
and here and here
'''));
}
43 changes: 2 additions & 41 deletions lib/hart.dart
@@ -1,48 +1,9 @@
#library('hart'); #library('hart');


#import('dart:io');
#import('parser.dart'); #import('parser.dart');


class Hart { class Hart {
static compile(data, filepath) { static parse(String data) {
new File(filepath).open(FileMode.WRITE, (file){ return new Parser(data).parsed;
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)), '');
} }
} }
7 changes: 4 additions & 3 deletions lib/parser.dart
Expand Up @@ -83,9 +83,10 @@ class Parser {
classes.add(next['val']); classes.add(next['val']);
break; break;
case 'attrs': 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]}'")); buff.add(current['val'].replaceAll(match[1], "'${match[1]}'"));
}); }
} }
} }
if (classes.length > 0) { if (classes.length > 0) {
Expand Down Expand Up @@ -226,4 +227,4 @@ class Parser {
} }
return buff.toString(); return buff.toString();
} }
} }
2 changes: 2 additions & 0 deletions lib/utils.dart
@@ -1,3 +1,5 @@
#library('utils');

class Filters { class Filters {
static plain (str) => str; static plain (str) => str;
static cdata (str) => '<![CDATA[\n${str}\n]]>'; static cdata (str) => '<![CDATA[\n${str}\n]]>';
Expand Down

0 comments on commit 9160c2e

Please sign in to comment.