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
f94aa621
Commit
f94aa621
authored
May 05, 2012
by
Chris Jerdonek
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Started decoupling Parser from RenderEngine.
parent
7eef0a68
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
32 additions
and
32 deletions
+32
-32
pystache/parsed.py
+4
-3
pystache/parser.py
+22
-2
pystache/renderengine.py
+6
-27
No files found.
pystache/parsed.py
View file @
f94aa621
...
...
@@ -38,7 +38,7 @@ class ParsedTemplate(object):
def
add
(
self
,
node
):
self
.
_parse_tree
.
append
(
node
)
def
render
(
self
,
context
):
def
render
(
self
,
engine
,
context
):
"""
Returns: a string of type unicode.
...
...
@@ -47,9 +47,10 @@ class ParsedTemplate(object):
def
get_unicode
(
val
):
if
callable
(
val
):
return
val
(
context
)
return
val
if
isinstance
(
val
,
basestring
):
return
val
return
val
.
render
(
engine
,
context
)
parts
=
map
(
get_unicode
,
self
.
_parse_tree
)
s
=
''
.
join
(
parts
)
return
unicode
(
s
)
pystache/parser.py
View file @
f94aa621
...
...
@@ -52,6 +52,26 @@ class ParsingError(Exception):
pass
class
VariableNode
(
object
):
def
__init__
(
self
,
key
):
self
.
key
=
key
def
render
(
self
,
engine
,
context
):
s
=
engine
.
_get_string_value
(
context
,
self
.
key
)
return
engine
.
escape
(
s
)
class
LiteralNode
(
object
):
def
__init__
(
self
,
key
):
self
.
key
=
key
def
render
(
self
,
engine
,
context
):
s
=
engine
.
_get_string_value
(
context
,
self
.
key
)
return
engine
.
literal
(
s
)
class
Parser
(
object
):
_delimiters
=
None
...
...
@@ -192,10 +212,10 @@ class Parser(object):
return
u''
if
tag_type
==
''
:
return
self
.
engine
.
_make_get_escaped
(
tag_key
)
return
VariableNode
(
tag_key
)
if
tag_type
==
'&'
:
return
self
.
engine
.
_make_get_literal
(
tag_key
)
return
LiteralNode
(
tag_key
)
if
tag_type
==
'>'
:
return
self
.
engine
.
_make_get_partial
(
tag_key
,
leading_whitespace
)
...
...
pystache/renderengine.py
View file @
f94aa621
...
...
@@ -106,30 +106,6 @@ class RenderEngine(object):
return
val
def
_make_get_literal
(
self
,
name
):
def
get_literal
(
context
):
"""
Returns: a string of type unicode.
"""
s
=
self
.
_get_string_value
(
context
,
name
)
return
self
.
literal
(
s
)
return
get_literal
def
_make_get_escaped
(
self
,
name
):
get_literal
=
self
.
_make_get_literal
(
name
)
def
get_escaped
(
context
):
"""
Returns: a string of type unicode.
"""
s
=
self
.
_get_string_value
(
context
,
name
)
return
self
.
escape
(
s
)
return
get_escaped
def
_make_get_partial
(
self
,
tag_key
,
leading_whitespace
):
template
=
self
.
resolve_partial
(
tag_key
)
...
...
@@ -158,7 +134,7 @@ class RenderEngine(object):
# Per the spec, lambdas in inverted sections are considered truthy.
if
data
:
return
u''
return
parsed_template
.
render
(
context
)
return
self
.
_render_parsed
(
parsed_template
,
context
)
return
get_inverse
...
...
@@ -228,13 +204,16 @@ class RenderEngine(object):
continue
context
.
push
(
val
)
parts
.
append
(
parsed_template
.
render
(
context
))
parts
.
append
(
self
.
_render_parsed
(
parsed_template
,
context
))
context
.
pop
()
return
unicode
(
''
.
join
(
parts
))
return
get_section_value
def
_render_parsed
(
self
,
parsed_template
,
context_stack
):
return
parsed_template
.
render
(
self
,
context_stack
)
def
_render_value
(
self
,
val
,
context
,
delimiters
=
None
):
"""
Render an arbitrary value.
...
...
@@ -262,4 +241,4 @@ class RenderEngine(object):
parser
=
Parser
(
self
,
delimiters
=
delimiters
)
parsed_template
=
parser
.
parse
(
template
)
return
parsed_template
.
render
(
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