Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit2db2cb0

Browse files
committed
Added jinja comment suport
1 parentc7461f9 commit2db2cb0

File tree

5 files changed

+80
-1
lines changed

5 files changed

+80
-1
lines changed

‎html5lib/constants.py‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3099,7 +3099,8 @@
30993099
"JinjaArgument":18,
31003100
"JinjaExtendTag":19,
31013101
"JinjaIncludeTag":20,
3102-
"JinjaImportTag":21
3102+
"JinjaImportTag":21,
3103+
"JinjaComment":22
31033104
}
31043105

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

‎html5lib/html5parser.py‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,7 @@ def mainLoop(self):
180180
JinjaExtendTag=tokenTypes["JinjaExtendTag"]
181181
JinjaIncludeTag=tokenTypes["JinjaIncludeTag"]
182182
JinjaImportTag=tokenTypes["JinjaImportTag"]
183+
JinjaComment=tokenTypes["JinjaComment"]
183184

184185
fortokeninself.normalizedTokens():
185186
new_token=token
@@ -243,6 +244,8 @@ def mainLoop(self):
243244
new_token=phase.processJinjaVariable(new_token)
244245
eliftype==JinjaPipe:
245246
new_token=phase.processJinjaPipe(new_token)
247+
eliftype==JinjaComment:
248+
new_token=phase.processJinjaComment(new_token)
246249
eliftype==JinjaFilter:
247250
new_token=phase.processJinjaFilter(new_token)
248251
eliftype==JinjaArgumentStartTag:
@@ -560,6 +563,10 @@ def processJinjaIncludeTag(self, token):
560563
element=self.tree.createElementWithoutNamespace(token)
561564
self.tree.openElements[-1].appendChild(element)
562565

566+
defprocessJinjaComment(self,token):
567+
element=self.tree.createElementWithoutNamespace(token)
568+
self.tree.openElements[-1].appendChild(element)
569+
563570
defprocessJinjaImportTag(self,token):
564571
element=self.tree.createElementWithoutNamespace(token)
565572
self.tree.openElements[-1].appendChild(element)

‎html5lib/inputstream.py‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
importcodecs
66
importre
7+
importlogging
78

89
from .constantsimportEOF,spaceCharacters,asciiLetters,asciiUppercase
910
from .constantsimportencodings,ReparseException
@@ -43,6 +44,8 @@ class BufferedIOBase(object):
4344
# Cache for charsUntil()
4445
charsUntilRegEx= {}
4546

47+
log=logging.getLogger(u"html5lib")
48+
4649

4750
classBufferedStream(object):
4851
"""Buffering for streams that do not have buffering of their own

‎html5lib/tests/test_jinja.py‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -369,6 +369,19 @@ def test_inline_if(self):
369369
}]
370370
}])
371371

372+
deftest_comment(self):
373+
html_string="""
374+
{# {{ '[%s]' % page.title if page.title }} #}
375+
"""
376+
377+
tree=self.parser.parseFragment(html_string)
378+
dump(tree)
379+
380+
self.assertTree(tree, [{
381+
'tag':'jinjacomment',
382+
'value':"{{ '[%s]' % page.title if page.title }} "
383+
}])
384+
372385
defassertTree(self,root,spec):
373386
self.assertEqual(len(root),len(spec))
374387

‎html5lib/tokenizer.py‎

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -311,6 +311,8 @@ def jinjaOpenState(self):
311311
self.state=self.jinjaVariableState
312312
elifdata=="%":
313313
self.state=self.jinjaStatementStartState
314+
elifdata=="#":
315+
self.state=self.jinjaCommentStartState
314316
else:
315317
self.stream.unget(data)
316318
self.stream.unget("{")
@@ -406,6 +408,59 @@ def jinjaStatementStartState(self):
406408

407409
returnTrue
408410

411+
defjinjaCommentStartState(self):
412+
data=self.stream.char()
413+
414+
ifdatainspaceCharacters:
415+
pass
416+
elifdataisEOF:
417+
self.tokenQueue.append({"type":tokenTypes["ParseError"],"data":
418+
"eof-in-jinja-statement"})
419+
self.state=self.prevState
420+
else:
421+
comment_text=data+self.stream.charsUntil(frozenset(("#","\u0000")))
422+
next_two=self.stream.char()
423+
424+
ifnext_two:
425+
next_two+=self.stream.char()
426+
427+
ifnotnext_twoorlen(next_two)<2:
428+
log.debug(u"Comment text {} = {}".format(comment_text,len(self.stream.chunk)))
429+
self.tokenQueue.append({"type":tokenTypes["ParseError"],"data":
430+
"expected-jinja-comment-closing-tag-but-got-eof",
431+
"datavars": {"data":data}})
432+
self.state=self.bogusCommentState
433+
returnTrue
434+
435+
whilenext_two!="#}":
436+
comment_text+=self.stream.chunk+self.stream.charsUntil(frozenset(("#","\u0000")))
437+
438+
next_two=self.stream.char()
439+
440+
ifnext_two:
441+
next_two+=self.stream.char()
442+
443+
ifnotnext_twoorlen(next_two)<2:
444+
self.tokenQueue.append({"type":tokenTypes["ParseError"],"data":
445+
"expected-jinja-comment-closing-tag-but-got-eof",
446+
"datavars": {"data":data}})
447+
self.state=self.bogusCommentState
448+
returnTrue
449+
450+
self.tokenQueue.append({
451+
"type":tokenTypes["JinjaComment"],
452+
'name':u"jinjacomment",
453+
"data": {
454+
"value":comment_text,
455+
"position":self.stream.position()
456+
},
457+
"selfClosing":True
458+
})
459+
460+
self.state=self.dataState
461+
462+
returnTrue
463+
409464
defjinjaStatementEndState(self):
410465
# We got a {
411466
data=self.stream.char()

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp