diff options
author | cduvall@chromium.org <cduvall@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2012-08-13 17:15:21 +0000 |
---|---|---|
committer | cduvall@chromium.org <cduvall@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2012-08-13 17:15:21 +0000 |
commit | e0c92bb9a2face1f96617ec4155c2e4559cbd7da (patch) | |
tree | a11c772414a131d499780614b61c7f9d47e5bfb3 /third_party/handlebar | |
parent | 9656bc5aa37f25a11ec143732529b1c1beb97d32 (diff) | |
download | chromium_src-e0c92bb9a2face1f96617ec4155c2e4559cbd7da.zip chromium_src-e0c92bb9a2face1f96617ec4155c2e4559cbd7da.tar.gz chromium_src-e0c92bb9a2face1f96617ec4155c2e4559cbd7da.tar.bz2 |
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
Diffstat (limited to 'third_party/handlebar')
-rw-r--r-- | third_party/handlebar/README.chromium | 2 | ||||
-rw-r--r-- | third_party/handlebar/handlebar.py | 38 |
2 files changed, 24 insertions, 16 deletions
diff --git a/third_party/handlebar/README.chromium b/third_party/handlebar/README.chromium index a3c488a..47d49b2 100644 --- a/third_party/handlebar/README.chromium +++ b/third_party/handlebar/README.chromium @@ -3,7 +3,7 @@ Short Name: handlebar URL: https://github.com/kalman/templates Version: 0 Date: July 13, 2012 -Revision: commit 5dedeedf37b8c0af933f6dda15d561fcaf11ddb0 +Revision: commit 606e9f31a240173e5d6c0b8768159c2e2835da42 License: Apache License, Version 2.0 Security Critical: no diff --git a/third_party/handlebar/handlebar.py b/third_party/handlebar/handlebar.py index 2d03e42..3e7b5a5 100644 --- a/third_party/handlebar/handlebar.py +++ b/third_party/handlebar/handlebar.py @@ -44,7 +44,7 @@ print(Handlebar('hello {{world}}').render(CustomContext()).text) """ 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): """ Exception thrown while parsing the template. @@ -392,19 +392,18 @@ class SectionNode(DecoratorNode): if value == None: return - type_ = type(value) - if type_ == list: + if isinstance(value, list): for item in value: renderState.localContexts.insert(0, item) self._content.render(renderState) renderState.localContexts.pop(0) - elif type_ == dict: + elif isinstance(value, dict): renderState.localContexts.insert(0, value) self._content.render(renderState) renderState.localContexts.pop(0) else: renderState.addError("{{#", self._id, - "}} cannot be rendered with a ", type_) + "}} cannot be rendered with a ", type(value)) class VertedSectionNode(DecoratorNode): """ {{?foo}} ... {{/}} @@ -423,18 +422,19 @@ class VertedSectionNode(DecoratorNode): def _VertedSectionNodeShouldRender(value): if value == None: return False - type_ = type(value) - if type_ == bool: + if isinstance(value, bool): return value - if type_ in [int, float]: + if (isinstance(value, int) or + isinstance(value, long) or + isinstance(value, float)): return True - if type_ in [str, unicode]: + if isinstance(value, basestring): return True - if type_ == list: + if isinstance(value, list): return len(value) > 0 - if type_ == dict: + if isinstance(value, dict): return True - raise TypeError("Unhandled type: " + str(type_)) + raise TypeError("Unhandled type %s" % type(value)) class InvertedSectionNode(DecoratorNode): """ {{^foo}} ... {{/}} @@ -588,6 +588,14 @@ class TokenStream(object): self.advance() 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): """ A handlebar template. """ @@ -620,17 +628,17 @@ class Handlebar(object): nodes.append(token.clazz(id, tokens.nextLine)) elif token == Token.OPEN_START_PARTIAL: tokens.advance() - id = Identifier(tokens.advanceOverNextString(excluded=' '), + id = Identifier(tokens.advanceToNextWhitespace(), tokens.nextLine) partialNode = PartialNode(id, tokens.nextLine) while tokens.nextToken == Token.CHARACTER: - tokens.advance() + tokens.skipWhitespace() key = tokens.advanceOverNextString(excluded=':') tokens.advance() partialNode.addArgument( key, - Identifier(tokens.advanceOverNextString(excluded=' '), + Identifier(tokens.advanceToNextWhitespace(), tokens.nextLine)) tokens.advanceOver(Token.CLOSE_MUSTACHE) |