Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
P
pystache_custom
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
OpenEdx
pystache_custom
Commits
182803ba
Commit
182803ba
authored
Jan 14, 2012
by
Chris Jerdonek
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Added ParsingError.
parent
2bef71e4
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
32 additions
and
11 deletions
+32
-11
pystache/parser.py
+9
-11
tests/test_renderengine.py
+23
-0
No files found.
pystache/parser.py
View file @
182803ba
...
...
@@ -39,12 +39,9 @@ def _compile_template_re(delimiters):
return
re
.
compile
(
tag
,
re
.
VERBOSE
)
class
EndOfSection
(
Exception
):
class
ParsingError
(
Exception
):
def
__init__
(
self
,
parse_tree
,
template
,
index_end
):
self
.
parse_tree
=
parse_tree
self
.
template
=
template
self
.
index_end
=
index_end
pass
class
Parser
(
object
):
...
...
@@ -74,7 +71,7 @@ class Parser(object):
self
.
_delimiters
=
delimiters
self
.
compile_template_re
()
def
parse
(
self
,
template
,
index
=
0
):
def
parse
(
self
,
template
,
index
=
0
,
section_key
=
None
):
"""
Parse a template string into a syntax tree using current attributes.
...
...
@@ -127,8 +124,9 @@ class Parser(object):
leading_whitespace
=
''
if
tag_type
==
'/'
:
if
tag_key
!=
section_key
:
raise
ParsingError
(
"Section end tag mismatch:
%
s !=
%
s"
%
(
repr
(
tag_key
),
repr
(
section_key
)))
# TODO: check that tag key matches section start tag key.
return
parse_tree
,
template
[
start_index
:
match_index
],
end_index
index
=
self
.
_handle_tag_type
(
template
,
parse_tree
,
tag_type
,
tag_key
,
leading_whitespace
,
start_index
,
match_index
,
end_index
)
...
...
@@ -138,8 +136,8 @@ class Parser(object):
return
parse_tree
def
_parse_section
(
self
,
template
,
index_start
):
parse_tree
,
template
,
index_end
=
self
.
parse
(
template
=
template
,
index
=
index_start
)
def
_parse_section
(
self
,
template
,
index_start
,
section_key
):
parse_tree
,
template
,
index_end
=
self
.
parse
(
template
=
template
,
index
=
index_start
,
section_key
=
section_key
)
return
parse_tree
,
template
,
index_end
...
...
@@ -165,12 +163,12 @@ class Parser(object):
elif
tag_type
==
'#'
:
buff
,
template
,
end_index
=
self
.
_parse_section
(
template
,
end_index
)
buff
,
template
,
end_index
=
self
.
_parse_section
(
template
,
end_index
,
tag_key
)
func
=
engine
.
_make_get_section
(
tag_key
,
buff
,
template
,
self
.
_delimiters
)
elif
tag_type
==
'^'
:
buff
,
template
,
end_index
=
self
.
_parse_section
(
template
,
end_index
)
buff
,
template
,
end_index
=
self
.
_parse_section
(
template
,
end_index
,
tag_key
)
func
=
engine
.
_make_get_inverse
(
tag_key
,
buff
)
elif
tag_type
==
'>'
:
...
...
tests/test_renderengine.py
View file @
182803ba
...
...
@@ -9,6 +9,7 @@ import cgi
import
unittest
from
pystache.context
import
Context
from
pystache.parser
import
ParsingError
from
pystache.renderengine
import
RenderEngine
from
tests.common
import
assert_strings
...
...
@@ -270,6 +271,28 @@ class RenderTests(unittest.TestCase):
## Test cases related specifically to sections.
def
test_section__end_tag_with_no_start_tag
(
self
):
"""
Check what happens if there is an end tag with no start tag.
"""
template
=
'{{/section}}'
try
:
self
.
_assert_render
(
None
,
template
)
except
ParsingError
,
err
:
self
.
assertEquals
(
str
(
err
),
"Section end tag mismatch: u'section' != None"
)
def
test_section__end_tag_mismatch
(
self
):
"""
Check what happens if the end tag doesn't match.
"""
template
=
'{{#section_start}}{{/section_end}}'
try
:
self
.
_assert_render
(
None
,
template
)
except
ParsingError
,
err
:
self
.
assertEquals
(
str
(
err
),
"Section end tag mismatch: u'section_end' != u'section_start'"
)
def
test_section__context_values
(
self
):
"""
Test that escape and literal work on context values in sections.
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment