10000 Jinja by aelaguiz · Pull Request #1 · cratejoy/html5lib-python · GitHub
[go: up one dir, main page]

Skip to content

Jinja #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Works with inline if
  • Loading branch information
aelaguiz committed Dec 26, 2014
commit c7461f968f2bf38d3e01fce117ff0aad76d3b327
30 changes: 30 additions & 0 deletions html5lib/tests/test_jinja.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ def test_var_1(self):
html_string = """<h1>{{ hi }}</h1>"""

tree = self.parser.parseFragment(html_string)
dump(tree)

self.assertTree(tree, [{
'tag': 'h1',
Expand All @@ -37,6 +38,7 @@ def test_var_2(self):
html_string = """<h1>{{ a.b }}</h1>"""

tree = self.parser.parseFragment(html_string)
dump(tree)

self.assertTree(tree, [{
'tag': 'h1',
Expand Down Expand Up @@ -339,6 +341,34 @@ def test_jinja_import(self):
'value': "'forms.html' import input as input_field, textarea"
}])

def test_inline_if(self):
html_string = """
{{ '[%s]' % page.title if page.title }}
"""

tree = self.parser.parseFragment(html_string)
dump(tree)

self.assertTree(tree, [{
'tag': 'jinjavariabletag',
'children': [{
'tag': 'jinjavariable',
'value': "'[%s]'"
}, {
'tag': 'jinjavariable',
'value': "%"
}, {
'tag': 'jinjavariable',
'value': "page.title"
}, {
'tag': 'jinjavariable',
'value': "if"
}, {
'tag': 'jinjavariable',
'value': "page.title"
}]
}])

def assertTree(self, root, spec):
self.assertEqual(len(root), len(spec))

Expand Down
11 changes: 7 additions & 4 deletions html5lib/tokenizer.py
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ def processEntityInAttribute(self, allowedChar):
"""
self.consumeEntity(allowedChar=allowedChar, fromAttribute=True)

def emitCurrentToken(self):
def emitCurrentToken(self, resetState=True):
"""This method is a generic handler for emitting the tags. It also sets
the state to "data" because that's what's needed after a token has been
emitted.
Expand All @@ -251,7 +251,9 @@ def emitCurrentToken(self):
self.tokenQueue.append({"type": tokenTypes["ParseError"],
"data": "self-closing-flag-on-end-tag"})
self.tokenQueue.append(token)
self.state = self.dataState

if resetState:
self.state = self.dataState

# Below are the various tokenizer states worked out.
def dataState(self):
Expand Down Expand Up @@ -508,8 +510,9 @@ def jinjaVariableState(self):
}}
self.tokenQueue.append(self.currentToken)
# If this is the first token after the variable start tag
elif self.currentToken['type'] == tokenTypes["JinjaVariableStartTag"]:
#log.debug(u"Got start tag {}".format(("|", "}", "\u0000") | spaceCharacters))
elif self.currentToken['type'] == tokenTypes["JinjaVariableStartTag"]\
or self.currentToken['type'] == tokenTypes["JinjaVariable"]:
#log.debug(u"Got start tag {}".format(("|", "}", "\u0000") | spaceCharacters))

chars = self.stream.charsUntil(frozenset(("(", "|", "}", "\u0000")) | spaceCharacters)
self.currentToken = {"type": tokenTypes["JinjaVariable"],
Expand Down
0