Plug partial whitespace indentation string through to renderPartial
diff --git a/lib/src/scanner.dart b/lib/src/scanner.dart
index 83be433..b08bac0 100644
--- a/lib/src/scanner.dart
+++ b/lib/src/scanner.dart
@@ -58,12 +58,8 @@
_Token read() { var ret = i < tokens.length ? tokens[i++] : null; /* print('Read: $ret'); */ return ret; }
_Token peek([int n = 0]) => i + n < tokens.length ? tokens[i + n] : null;
- bool isTag(token) =>
- token != null
- && (token.type == _OPEN_SECTION
- || token.type == _OPEN_INV_SECTION
- || token.type == _CLOSE_SECTION
- || token.type == _COMMENT);
+ bool isTag(token) => token != null
+ && const [_OPEN_SECTION, _OPEN_INV_SECTION, _CLOSE_SECTION, _COMMENT, _PARTIAL].contains(token.type);
bool isWhitespace(token) => token != null && token.type == _WHITESPACE;
bool isLineEnd(token) => token != null && token.type == _LINE_END;
@@ -128,11 +124,12 @@
}
class _Token {
- _Token(this.type, this.value, this.line, this.column);
+ _Token(this.type, this.value, this.line, this.column, {this.indent});
final int type;
final String value;
final int line;
final int column;
+ final String indent;
toString() => "${_tokenTypeString(type)}: \"${value.replaceAll('\n', '\\n')}\" $line:$column";
}
@@ -160,6 +157,26 @@
_tokens.add(new _Token(type, value, l, c));
}
+ _addPartialToken() {
+ // Capture whitespace preceding a partial tag so it can used for indentation during rendering.
+ var indent = '';
+ if (_tokens.isNotEmpty) {
+ if (_tokens.length == 1 && _tokens.last == _WHITESPACE) {
+ indent = _tokens.last.value;
+
+ } else if (_tokens.length > 1) {
+ if (_tokens.last.type == _WHITESPACE
+ && _tokens[_tokens.length - 2].type == _NEWLINE) {
+ indent = _tokens.last.value;
+ }
+ }
+ }
+
+ int l = _r.line, c = _r.column;
+ var value = _readString().trim();
+ _tokens.add(new _Token(_PARTIAL, value, l, c, indent: indent));
+ }
+
_expect(int expectedCharCode) {
int c = _read();
@@ -279,7 +296,7 @@
// Partial {{> ... }}
case _GT:
_read();
- _addStringToken(_PARTIAL);
+ _addPartialToken();
break;
// Open section {{# ... }}
diff --git a/lib/src/template.dart b/lib/src/template.dart
index 1db8898..7a1fed1 100644
--- a/lib/src/template.dart
+++ b/lib/src/template.dart
@@ -115,10 +115,11 @@
this._lenient,
this._htmlEscapeValues,
this._partialResolver,
- this._templateName)
+ this._templateName,
+ [this._indent])
: _stack = new List.from(stack);
- _Renderer.partial(_Renderer renderer, _Template partial)
+ _Renderer.partial(_Renderer renderer, _Template partial, String indent)
: this(partial._root,
renderer._sink,
renderer._values,
@@ -146,6 +147,7 @@
final bool _htmlEscapeValues;
final PartialResolver _partialResolver;
final String _templateName;
+ final String _indent;
void render() {
_root.children.forEach(_renderNode);
@@ -352,7 +354,7 @@
? null
: _partialResolver(partialName);
if (template != null) {
- var renderer = new _Renderer.partial(this, template);
+ var renderer = new _Renderer.partial(this, template, node.indent);
renderer.render();
} else if (_lenient) {
// do nothing
@@ -405,16 +407,18 @@
}
class _Node {
- _Node(this.type, this.value, this.line, this.column);
+ _Node(this.type, this.value, this.line, this.column, {this.indent});
_Node.fromToken(_Token token)
: type = token.type,
value = token.value,
line = token.line,
- column = token.column;
+ column = token.column,
+ indent = token.indent;
final int type;
final String value;
final int line;
final int column;
+ final String indent;
final List<_Node> children = new List<_Node>();
String toString() => '_Node: ${_tokenTypeString(type)}';
}