8000 WIP - Jinja parsing · html5lib/html5lib-python@f756cab · GitHub
[go: up one dir, main page]

Skip to content
< 10000 script crossorigin="anonymous" type="application/javascript" src="https://github.githubassets.com/assets/vendors-node_modules_github_remote-form_dist_index_js-node_modules_delegated-events_dist_inde-94fd67-b0625c39513c.js" defer="defer">

Commit f756cab

Browse files
committed
WIP - Jinja parsing
1 parent 5246944 commit f756cab

File tree

3 files changed

+30
-11
lines changed

3 files changed

+30
-11
lines changed

html5lib/constants.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3092,7 +3092,8 @@
30923092
"JinjaVariableStartTag": 11,
30933093
"JinjaVariableEndTag": 12,
30943094
"JinjaVariable": 13,
3095-
"JinjaFilter": 14
3095+
"JinjaFilter": 14,
3096+
"JinjaPipe": 15
30963097
}
30973098

30983099
tagTokenTypes = frozenset((tokenTypes["StartTag"], tokenTypes["EndTag"],

html5lib/html5parser.py

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,7 @@ def mainLoop(self):
172172
JinjaVariableStartTag = tokenTypes["JinjaVariableStartTag"]
173173
JinjaVariableEndTag = tokenTypes["JinjaVariableEndTag"]
174174
JinjaVariable = tokenTypes["JinjaVariable"]
175+
JinjaPipe = tokenTypes["JinjaPipe"]
175176
JinjaFilter = tokenTypes["JinjaFilter"]
176177

177178
for token in self.normalizedTokens():
@@ -188,7 +189,7 @@ def mainLoop(self):
188189
self.parseError(new_token["data"], new_token.get("datavars", {}))
189190
new_token = None
190191
else:
191-
if type in (JinjaVariableStartTag, JinjaVariableEndTag, JinjaVariable, JinjaFilter):
192+
if type in (JinjaVariableStartTag, JinjaVariableEndTag, JinjaVariable, JinjaFilter, JinjaPipe):
192193
log.debug(u"Type is a jinja tag")
193194
phase = self.phases["inJinjaVariable"]
194195
elif (
@@ -231,6 +232,8 @@ def mainLoop(self):
231232
new_token = phase.processJinjaVariableEndTag(new_token)
232233
elif type == JinjaVariable:
233234
new_token = phase.processJinjaVariable(new_token)
235+
elif type == JinjaPipe:
236+
new_token = phase.processJinjaPipe(new_token)
234237
elif type == JinjaFilter:
235238
new_token = phase.processJinjaFilter(new_token)
236239

@@ -429,7 +432,7 @@ def resetInsertionMode(self):
429432
new_phase = self.phases["inBody"]
430433
break
431434

432-
log.debug(u"Changing phase to {}".format(new_phase))
435+
#log.debug(u"Changing phase to {}".format(new_phase))
433436
self.phase = new_phase
434437

435438
def parseRCDataRawtext(self, token, contentType):
@@ -526,6 +529,9 @@ def processJinjaVariableEndTag(self, token):
526529
def processJinjaVariable(self, token):
527530
pass
528531

532+
def processJinjaPipe(self, token):
533+
pass
534+
529535
def processJinjaFilterTag(self, token):
530536
pass
531537

@@ -575,6 +581,10 @@ def processJinjaVariable(self, token):
575581
element = self.tree.createElementWithoutNamespace(token)
576582
self.tree.openElements[-1].appendChild(element)
577583

584+
def processJinjaPipe(self, token):
585+
element = self.tree.createElementWithoutNamespace(token)
586+
self.tree.openElements[-1].appendChild(element)
587+
578588
def processJinjaFilter(self, token):
579589
element = self.tree.createElementWithoutNamespace(token)
580590
self.tree.openElements[-1].appendChild(element)

html5lib/tokenizer.py

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -390,6 +390,8 @@ def jinjaVariableState(self):
390390

391391
if data == "}":
392392
self.state = self.jinjaVariableEndState
393+
#elif data == "(":
394+
#self.state = self.jinjaArgState
393395
elif data is EOF:
394396
self.tokenQueue.append({"type": tokenTypes["ParseError"], "data":
395397
"eof-in-jinja-variable"})
@@ -398,30 +400,36 @@ def jinjaVariableState(self):
398400
# Skip spaces
399401
pass
400402
elif data == "|":
401-
pass
403+
self.currentToken = {"type": tokenTypes["JinjaPipe"],
404+
"name": "jinjapipe", "selfClosing": True, "data": {
405+
"value": data,
406+
"position": self.stream.position(),
407+
}}
408+
self.tokenQueue.append(self.currentToken)
402409
# If this is the first token after the variable start tag
403410
elif self.currentToken['type'] == tokenTypes["JinjaVariableStartTag"]:
404411
#log.debug(u"Got start tag {}".format(("|", "}", "\u0000") | spaceCharacters))
405412

406-
chars = self.stream.charsUntil(frozenset(("|", "}", "\u0000")) | spaceCharacters)
413+
chars = self.stream.charsUntil(frozenset(("(", "|", "}", "\u0000")) | spaceCharacters)
407414
self.currentToken = {"type": tokenTypes["JinjaVariable"],
408415
"name": "jinjavariable", "selfClosing": True, "data": {
409416
"value": data + chars,
410417
"position": self.stream.position(),
411418
}}
412419
self.tokenQueue.append(self.currentToken)
413-
else:
414-
chars = self.stream.charsUntil(frozenset(("|", "}", "\u0000")) | spaceCharacters)
420+
elif self.currentToken['type'] == tokenTypes["JinjaPipe"]:
421+
chars = self.stream.charsUntil(frozenset(("(", "|", "}", "\u0000")) | spaceCharacters)
415422
self.currentToken = {"type": tokenTypes["JinjaFilter"],
416423
"name": "jinjafilter", "selfClosing": True, "data": {
417424
"value": data + chars,
418425
"position": self.stream.position(),
419426
}}
420427
self.tokenQueue.append(self.currentToken)
421-
#else:
422-
#chars = self.stream.charsUntil(("}", "\u0000"))
423-
#self.tokenQueue.append({"type": tokenTypes["JinjaVariableTag"], "data":
424-
#data + chars})
428+
else:
429+
self.tokenQueue.append({"type": tokenTypes["ParseError"], "data":
430+
"expected-jinja-pipe-got-character"})
431+
self.stream.unget(data)
432+
self.state = self.bogusCommentState
425433

426434
return True
427435

0 commit comments

Comments
 (0)
0