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
01a2daf4
Commit
01a2daf4
authored
May 05, 2012
by
Chris Jerdonek
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Added parse() function and made parser classes private.
parent
e4108154
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
26 additions
and
18 deletions
+26
-18
pystache/parser.py
+24
-15
pystache/renderengine.py
+2
-3
No files found.
pystache/parser.py
View file @
01a2daf4
...
@@ -17,6 +17,15 @@ END_OF_LINE_CHARACTERS = [u'\r', u'\n']
...
@@ -17,6 +17,15 @@ END_OF_LINE_CHARACTERS = [u'\r', u'\n']
NON_BLANK_RE
=
re
.
compile
(
ur'^(.)'
,
re
.
M
)
NON_BLANK_RE
=
re
.
compile
(
ur'^(.)'
,
re
.
M
)
def
parse
(
template
,
delimiters
=
None
):
"""
Parse a unicode template string and return a ParsedTemplate instance.
"""
parser
=
_Parser
(
delimiters
)
return
parser
.
parse
(
template
)
def
_compile_template_re
(
delimiters
=
None
):
def
_compile_template_re
(
delimiters
=
None
):
"""
"""
Return a regular expresssion object (re.RegexObject) instance.
Return a regular expresssion object (re.RegexObject) instance.
...
@@ -56,13 +65,13 @@ class ParsingError(Exception):
...
@@ -56,13 +65,13 @@ class ParsingError(Exception):
## Node types
## Node types
class
CommentNode
(
object
):
class
_
CommentNode
(
object
):
def
render
(
self
,
engine
,
context
):
def
render
(
self
,
engine
,
context
):
return
u''
return
u''
class
ChangeNode
(
object
):
class
_
ChangeNode
(
object
):
def
__init__
(
self
,
delimiters
):
def
__init__
(
self
,
delimiters
):
self
.
delimiters
=
delimiters
self
.
delimiters
=
delimiters
...
@@ -71,7 +80,7 @@ class ChangeNode(object):
...
@@ -71,7 +80,7 @@ class ChangeNode(object):
return
u''
return
u''
class
Tag
(
object
):
class
_TagNode
(
object
):
def
__init__
(
self
,
key
):
def
__init__
(
self
,
key
):
self
.
key
=
key
self
.
key
=
key
...
@@ -81,7 +90,7 @@ class Tag(object):
...
@@ -81,7 +90,7 @@ class Tag(object):
return
engine
.
escape
(
s
)
return
engine
.
escape
(
s
)
class
LiteralNode
(
object
):
class
_
LiteralNode
(
object
):
def
__init__
(
self
,
key
):
def
__init__
(
self
,
key
):
self
.
key
=
key
self
.
key
=
key
...
@@ -91,7 +100,7 @@ class LiteralNode(object):
...
@@ -91,7 +100,7 @@ class LiteralNode(object):
return
engine
.
literal
(
s
)
return
engine
.
literal
(
s
)
class
PartialNode
(
object
):
class
_
PartialNode
(
object
):
def
__init__
(
self
,
key
,
indent
):
def
__init__
(
self
,
key
,
indent
):
self
.
key
=
key
self
.
key
=
key
...
@@ -105,7 +114,7 @@ class PartialNode(object):
...
@@ -105,7 +114,7 @@ class PartialNode(object):
return
engine
.
render
(
template
,
context
)
return
engine
.
render
(
template
,
context
)
class
InvertedNode
(
object
):
class
_
InvertedNode
(
object
):
def
__init__
(
self
,
key
,
parsed_section
):
def
__init__
(
self
,
key
,
parsed_section
):
self
.
key
=
key
self
.
key
=
key
...
@@ -122,7 +131,7 @@ class InvertedNode(object):
...
@@ -122,7 +131,7 @@ class InvertedNode(object):
return
engine
.
render_parsed
(
self
.
parsed_section
,
context
)
return
engine
.
render_parsed
(
self
.
parsed_section
,
context
)
class
SectionNode
(
object
):
class
_
SectionNode
(
object
):
# TODO: the template_ and parsed_template_ arguments don't both seem
# TODO: the template_ and parsed_template_ arguments don't both seem
# to be necessary. Can we remove one of them? For example, if
# to be necessary. Can we remove one of them? For example, if
...
@@ -168,7 +177,7 @@ class SectionNode(object):
...
@@ -168,7 +177,7 @@ class SectionNode(object):
return
unicode
(
''
.
join
(
parts
))
return
unicode
(
''
.
join
(
parts
))
class
Parser
(
object
):
class
_
Parser
(
object
):
_delimiters
=
None
_delimiters
=
None
_template_re
=
None
_template_re
=
None
...
@@ -292,21 +301,21 @@ class Parser(object):
...
@@ -292,21 +301,21 @@ class Parser(object):
"""
"""
# TODO: switch to using a dictionary instead of a bunch of ifs and elifs.
# TODO: switch to using a dictionary instead of a bunch of ifs and elifs.
if
tag_type
==
'!'
:
if
tag_type
==
'!'
:
return
CommentNode
()
return
_
CommentNode
()
if
tag_type
==
'='
:
if
tag_type
==
'='
:
delimiters
=
tag_key
.
split
()
delimiters
=
tag_key
.
split
()
self
.
_change_delimiters
(
delimiters
)
self
.
_change_delimiters
(
delimiters
)
return
ChangeNode
(
delimiters
)
return
_
ChangeNode
(
delimiters
)
if
tag_type
==
''
:
if
tag_type
==
''
:
return
Tag
(
tag_key
)
return
_TagNode
(
tag_key
)
if
tag_type
==
'&'
:
if
tag_type
==
'&'
:
return
LiteralNode
(
tag_key
)
return
_
LiteralNode
(
tag_key
)
if
tag_type
==
'>'
:
if
tag_type
==
'>'
:
return
PartialNode
(
tag_key
,
leading_whitespace
)
return
_
PartialNode
(
tag_key
,
leading_whitespace
)
raise
Exception
(
"Invalid symbol for interpolation tag:
%
s"
%
repr
(
tag_type
))
raise
Exception
(
"Invalid symbol for interpolation tag:
%
s"
%
repr
(
tag_type
))
...
@@ -317,10 +326,10 @@ class Parser(object):
...
@@ -317,10 +326,10 @@ class Parser(object):
"""
"""
if
tag_type
==
'#'
:
if
tag_type
==
'#'
:
return
SectionNode
(
tag_key
,
parsed_section
,
self
.
_delimiters
,
return
_
SectionNode
(
tag_key
,
parsed_section
,
self
.
_delimiters
,
template
,
section_start_index
,
section_end_index
)
template
,
section_start_index
,
section_end_index
)
if
tag_type
==
'^'
:
if
tag_type
==
'^'
:
return
InvertedNode
(
tag_key
,
parsed_section
)
return
_
InvertedNode
(
tag_key
,
parsed_section
)
raise
Exception
(
"Invalid symbol for section tag:
%
s"
%
repr
(
tag_type
))
raise
Exception
(
"Invalid symbol for section tag:
%
s"
%
repr
(
tag_type
))
pystache/renderengine.py
View file @
01a2daf4
...
@@ -8,7 +8,7 @@ Defines a class responsible for rendering logic.
...
@@ -8,7 +8,7 @@ Defines a class responsible for rendering logic.
import
re
import
re
from
pystache.common
import
is_string
from
pystache.common
import
is_string
from
pystache.parser
import
Parser
from
pystache.parser
import
parse
def
context_get
(
stack
,
name
):
def
context_get
(
stack
,
name
):
...
@@ -165,7 +165,6 @@ class RenderEngine(object):
...
@@ -165,7 +165,6 @@ class RenderEngine(object):
context_stack: a ContextStack instance.
context_stack: a ContextStack instance.
"""
"""
parser
=
Parser
(
delimiters
=
delimiters
)
parsed_template
=
parse
(
template
,
delimiters
)
parsed_template
=
parser
.
parse
(
template
)
return
self
.
render_parsed
(
parsed_template
,
context_stack
)
return
self
.
render_parsed
(
parsed_template
,
context_stack
)
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