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
337b6344
Commit
337b6344
authored
Apr 28, 2012
by
Chris Jerdonek
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Commented some of the section parsing code.
parent
f7f63aea
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
48 additions
and
13 deletions
+48
-13
pystache/parser.py
+46
-11
pystache/tests/test_parser.py
+2
-2
No files found.
pystache/parser.py
View file @
337b6344
...
...
@@ -17,7 +17,13 @@ END_OF_LINE_CHARACTERS = ['\r', '\n']
NON_BLANK_RE
=
re
.
compile
(
r'^(.)'
,
re
.
M
)
def
_compile_template_re
(
delimiters
):
def
_compile_template_re
(
delimiters
=
None
):
"""
Return a regular expresssion object (re.RegexObject) instance.
"""
if
delimiters
is
None
:
delimiters
=
DEFAULT_DELIMITERS
# The possible tag type characters following the opening tag,
# excluding "=" and "{".
...
...
@@ -74,19 +80,25 @@ class Parser(object):
self
.
_delimiters
=
delimiters
self
.
compile_template_re
()
def
parse
(
self
,
template
,
index
=
0
,
section_key
=
None
):
def
parse
(
self
,
template
,
start_
index
=
0
,
section_key
=
None
):
"""
Parse a template string
into a ParsedTemplate instance
.
Parse a template string
starting at some index
.
This method uses the current tag delimiter.
Arguments:
template: a template string of type unicode.
template: a unicode string that is the template to parse.
index: the index at which to start parsing.
Returns:
a ParsedTemplate instance.
"""
parse_tree
=
[]
start_index
=
index
index
=
start_
index
while
True
:
match
=
self
.
_template_re
.
search
(
template
,
index
)
...
...
@@ -142,10 +154,33 @@ class Parser(object):
return
ParsedTemplate
(
parse_tree
)
def
_parse_section
(
self
,
template
,
index_start
,
section_key
):
parsed_template
,
template
,
index_end
=
self
.
parse
(
template
=
template
,
index
=
index_start
,
section_key
=
section_key
)
def
_parse_section
(
self
,
template
,
start_index
,
section_key
):
"""
Parse the contents of a template section.
Arguments:
template: a unicode template string.
start_index: the string index at which the section contents begin.
section_key: the tag key of the section.
Returns: a 3-tuple:
parsed_section: the section contents parsed as a ParsedTemplate
instance.
section_contents: the unparsed section contents.
end_index: the string index after the closing section tag (and
including any trailing newlines).
"""
parsed_section
,
section_contents
,
end_index
=
\
self
.
parse
(
template
=
template
,
start_index
=
start_index
,
section_key
=
section_key
)
return
parsed_
template
,
template
,
index_end
return
parsed_
section
,
section_contents
,
end_index
def
_handle_tag_type
(
self
,
template
,
parse_tree
,
tag_type
,
tag_key
,
leading_whitespace
,
end_index
):
...
...
@@ -170,12 +205,12 @@ class Parser(object):
elif
tag_type
==
'#'
:
parsed_section
,
template
,
end_index
=
self
.
_parse_section
(
template
,
end_index
,
tag_key
)
func
=
engine
.
_make_get_section
(
tag_key
,
parsed_section
,
template
,
self
.
_delimiters
)
parsed_section
,
section_contents
,
end_index
=
self
.
_parse_section
(
template
,
end_index
,
tag_key
)
func
=
engine
.
_make_get_section
(
tag_key
,
parsed_section
,
section_contents
,
self
.
_delimiters
)
elif
tag_type
==
'^'
:
parsed_section
,
template
,
end_index
=
self
.
_parse_section
(
template
,
end_index
,
tag_key
)
parsed_section
,
section_contents
,
end_index
=
self
.
_parse_section
(
template
,
end_index
,
tag_key
)
func
=
engine
.
_make_get_inverse
(
tag_key
,
parsed_section
)
elif
tag_type
==
'>'
:
...
...
pystache/tests/test_parser.py
View file @
337b6344
...
...
@@ -20,7 +20,7 @@ class RegularExpressionTestCase(unittest.TestCase):
"""
re
=
make_re
()
match
=
re
.
search
(
"{{test}}"
)
match
=
re
.
search
(
"
b
{{test}}"
)
self
.
assertEqual
(
match
.
start
(),
0
)
self
.
assertEqual
(
match
.
start
(),
1
)
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