Commit e0c92bb9 authored by cduvall@chromium.org's avatar cduvall@chromium.org

Extensions Docs Server: Handlebar handles line breaks in partials

Update for Handlebar so we can make cleaner templates.

BUG=131095
TBR=kalman@chromium.org

Review URL: https://chromiumcodereview.appspot.com/10836212

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@151292 0039d316-1c4b-4281-b951-d872f2087c98
parent 9656bc5a
...@@ -3,7 +3,7 @@ Short Name: handlebar ...@@ -3,7 +3,7 @@ Short Name: handlebar
URL: https://github.com/kalman/templates URL: https://github.com/kalman/templates
Version: 0 Version: 0
Date: July 13, 2012 Date: July 13, 2012
Revision: commit 5dedeedf37b8c0af933f6dda15d561fcaf11ddb0 Revision: commit 606e9f31a240173e5d6c0b8768159c2e2835da42
License: Apache License, Version 2.0 License: Apache License, Version 2.0
Security Critical: no Security Critical: no
......
...@@ -44,7 +44,7 @@ print(Handlebar('hello {{world}}').render(CustomContext()).text) ...@@ -44,7 +44,7 @@ print(Handlebar('hello {{world}}').render(CustomContext()).text)
""" """
def _SafeStr(obj): def _SafeStr(obj):
return obj if (type(obj) in [str, unicode]) else str(obj) return obj if isinstance(obj, basestring) else str(obj)
class ParseException(Exception): class ParseException(Exception):
""" Exception thrown while parsing the template. """ Exception thrown while parsing the template.
...@@ -392,19 +392,18 @@ class SectionNode(DecoratorNode): ...@@ -392,19 +392,18 @@ class SectionNode(DecoratorNode):
if value == None: if value == None:
return return
type_ = type(value) if isinstance(value, list):
if type_ == list:
for item in value: for item in value:
renderState.localContexts.insert(0, item) renderState.localContexts.insert(0, item)
self._content.render(renderState) self._content.render(renderState)
renderState.localContexts.pop(0) renderState.localContexts.pop(0)
elif type_ == dict: elif isinstance(value, dict):
renderState.localContexts.insert(0, value) renderState.localContexts.insert(0, value)
self._content.render(renderState) self._content.render(renderState)
renderState.localContexts.pop(0) renderState.localContexts.pop(0)
else: else:
renderState.addError("{{#", self._id, renderState.addError("{{#", self._id,
"}} cannot be rendered with a ", type_) "}} cannot be rendered with a ", type(value))
class VertedSectionNode(DecoratorNode): class VertedSectionNode(DecoratorNode):
""" {{?foo}} ... {{/}} """ {{?foo}} ... {{/}}
...@@ -423,18 +422,19 @@ class VertedSectionNode(DecoratorNode): ...@@ -423,18 +422,19 @@ class VertedSectionNode(DecoratorNode):
def _VertedSectionNodeShouldRender(value): def _VertedSectionNodeShouldRender(value):
if value == None: if value == None:
return False return False
type_ = type(value) if isinstance(value, bool):
if type_ == bool:
return value return value
if type_ in [int, float]: if (isinstance(value, int) or
isinstance(value, long) or
isinstance(value, float)):
return True return True
if type_ in [str, unicode]: if isinstance(value, basestring):
return True return True
if type_ == list: if isinstance(value, list):
return len(value) > 0 return len(value) > 0
if type_ == dict: if isinstance(value, dict):
return True return True
raise TypeError("Unhandled type: " + str(type_)) raise TypeError("Unhandled type %s" % type(value))
class InvertedSectionNode(DecoratorNode): class InvertedSectionNode(DecoratorNode):
""" {{^foo}} ... {{/}} """ {{^foo}} ... {{/}}
...@@ -588,6 +588,14 @@ class TokenStream(object): ...@@ -588,6 +588,14 @@ class TokenStream(object):
self.advance() self.advance()
return buf.toString() return buf.toString()
def advanceToNextWhitespace(self):
return self.advanceOverNextString(excluded=' \n\r\t')
def skipWhitespace(self):
while len(self.nextContents) > 0 and \
' \n\r\t'.find(self.nextContents) >= 0:
self.advance()
class Handlebar(object): class Handlebar(object):
""" A handlebar template. """ A handlebar template.
""" """
...@@ -620,17 +628,17 @@ class Handlebar(object): ...@@ -620,17 +628,17 @@ class Handlebar(object):
nodes.append(token.clazz(id, tokens.nextLine)) nodes.append(token.clazz(id, tokens.nextLine))
elif token == Token.OPEN_START_PARTIAL: elif token == Token.OPEN_START_PARTIAL:
tokens.advance() tokens.advance()
id = Identifier(tokens.advanceOverNextString(excluded=' '), id = Identifier(tokens.advanceToNextWhitespace(),
tokens.nextLine) tokens.nextLine)
partialNode = PartialNode(id, tokens.nextLine) partialNode = PartialNode(id, tokens.nextLine)
while tokens.nextToken == Token.CHARACTER: while tokens.nextToken == Token.CHARACTER:
tokens.advance() tokens.skipWhitespace()
key = tokens.advanceOverNextString(excluded=':') key = tokens.advanceOverNextString(excluded=':')
tokens.advance() tokens.advance()
partialNode.addArgument( partialNode.addArgument(
key, key,
Identifier(tokens.advanceOverNextString(excluded=' '), Identifier(tokens.advanceToNextWhitespace(),
tokens.nextLine)) tokens.nextLine))
tokens.advanceOver(Token.CLOSE_MUSTACHE) tokens.advanceOver(Token.CLOSE_MUSTACHE)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment