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
e4108154
Commit
e4108154
authored
May 05, 2012
by
Chris Jerdonek
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Parser no longer requires a RenderEngine instance to parse.
parent
a3999641
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
63 additions
and
65 deletions
+63
-65
pystache/parsed.py
+1
-3
pystache/parser.py
+53
-15
pystache/renderengine.py
+9
-47
No files found.
pystache/parsed.py
View file @
e4108154
...
...
@@ -45,9 +45,7 @@ class ParsedTemplate(object):
"""
# We avoid use of the ternary operator for Python 2.4 support.
def
get_unicode
(
val
):
if
callable
(
val
):
return
val
(
context
)
if
isinstance
(
val
,
basestring
):
if
type
(
val
)
is
unicode
:
return
val
return
val
.
render
(
engine
,
context
)
parts
=
map
(
get_unicode
,
self
.
_parse_tree
)
...
...
pystache/parser.py
View file @
e4108154
...
...
@@ -71,7 +71,7 @@ class ChangeNode(object):
return
u''
class
VariableNode
(
object
):
class
Tag
(
object
):
def
__init__
(
self
,
key
):
self
.
key
=
key
...
...
@@ -119,28 +119,65 @@ class InvertedNode(object):
# per the spec.
if
data
:
return
u''
return
engine
.
_
render_parsed
(
self
.
parsed_section
,
context
)
return
engine
.
render_parsed
(
self
.
parsed_section
,
context
)
class
Parser
(
object
):
class
SectionNode
(
object
):
_delimiters
=
None
_template_re
=
None
# TODO: the template_ and parsed_template_ arguments don't both seem
# to be necessary. Can we remove one of them? For example, if
# callable(data) is True, then the initial parsed_template isn't used.
def
__init__
(
self
,
key
,
parsed_contents
,
delimiters
,
template
,
section_begin_index
,
section_end_index
):
self
.
delimiters
=
delimiters
self
.
key
=
key
self
.
parsed_contents
=
parsed_contents
self
.
template
=
template
self
.
section_begin_index
=
section_begin_index
self
.
section_end_index
=
section_end_index
def
__init__
(
self
,
engine
,
delimiters
=
None
):
"""
Construct an instance.
def
render
(
self
,
engine
,
context
):
data
=
engine
.
fetch_section_data
(
context
,
self
.
key
)
Arguments:
parts
=
[]
for
val
in
data
:
if
callable
(
val
):
# Lambdas special case section rendering and bypass pushing
# the data value onto the context stack. From the spec--
#
# When used as the data value for a Section tag, the
# lambda MUST be treatable as an arity 1 function, and
# invoked as such (passing a String containing the
# unprocessed section contents). The returned value
# MUST be rendered against the current delimiters, then
# interpolated in place of the section.
#
# Also see--
#
# https://github.com/defunkt/pystache/issues/113
#
# TODO: should we check the arity?
val
=
val
(
self
.
template
[
self
.
section_begin_index
:
self
.
section_end_index
])
val
=
engine
.
_render_value
(
val
,
context
,
delimiters
=
self
.
delimiters
)
parts
.
append
(
val
)
continue
engine: a RenderEngine instance.
context
.
push
(
val
)
parts
.
append
(
engine
.
render_parsed
(
self
.
parsed_contents
,
context
))
context
.
pop
()
"""
return
unicode
(
''
.
join
(
parts
))
class
Parser
(
object
):
_delimiters
=
None
_template_re
=
None
def
__init__
(
self
,
delimiters
=
None
):
if
delimiters
is
None
:
delimiters
=
DEFAULT_DELIMITERS
self
.
_delimiters
=
delimiters
self
.
engine
=
engine
def
_compile_delimiters
(
self
):
self
.
_template_re
=
_compile_template_re
(
self
.
_delimiters
)
...
...
@@ -242,7 +279,8 @@ class Parser(object):
parsed_template
.
add
(
node
)
# Add the remainder of the template.
# Avoid adding spurious empty strings to the parse tree.
if
start_index
!=
len
(
template
):
parsed_template
.
add
(
template
[
start_index
:])
return
parsed_template
...
...
@@ -262,7 +300,7 @@ class Parser(object):
return
ChangeNode
(
delimiters
)
if
tag_type
==
''
:
return
VariableNode
(
tag_key
)
return
Tag
(
tag_key
)
if
tag_type
==
'&'
:
return
LiteralNode
(
tag_key
)
...
...
@@ -279,7 +317,7 @@ class Parser(object):
"""
if
tag_type
==
'#'
:
return
self
.
engine
.
_make_get_section
(
tag_key
,
parsed_section
,
self
.
_delimiters
,
return
SectionNode
(
tag_key
,
parsed_section
,
self
.
_delimiters
,
template
,
section_start_index
,
section_end_index
)
if
tag_type
==
'^'
:
...
...
pystache/renderengine.py
View file @
e4108154
...
...
@@ -87,12 +87,12 @@ class RenderEngine(object):
# The returned value MUST be rendered against the default delimiters,
# then interpolated in place of the lambda.
#
def
fetch_string
(
self
,
context
,
tag_
name
):
def
fetch_string
(
self
,
context
,
name
):
"""
Get a value from the given context as a basestring instance.
"""
val
=
self
.
resolve_context
(
context
,
tag_
name
)
val
=
self
.
resolve_context
(
context
,
name
)
if
callable
(
val
):
# Return because _render_value() is already a string.
...
...
@@ -103,16 +103,7 @@ class RenderEngine(object):
return
val
# TODO: the template_ and parsed_template_ arguments don't both seem
# to be necessary. Can we remove one of them? For example, if
# callable(data) is True, then the initial parsed_template isn't used.
def
_make_get_section
(
self
,
name
,
parsed_template
,
delims
,
template
,
section_start_index
,
section_end_index
):
def
get_section_value
(
context
):
"""
Returns: a string of type unicode.
"""
def
fetch_section_data
(
self
,
context
,
name
):
data
=
self
.
resolve_context
(
context
,
name
)
# From the spec:
...
...
@@ -145,39 +136,7 @@ class RenderEngine(object):
data
=
[
data
]
# Otherwise, treat the value as a list.
parts
=
[]
for
val
in
data
:
if
callable
(
val
):
# Lambdas special case section rendering and bypass pushing
# the data value onto the context stack. From the spec--
#
# When used as the data value for a Section tag, the
# lambda MUST be treatable as an arity 1 function, and
# invoked as such (passing a String containing the
# unprocessed section contents). The returned value
# MUST be rendered against the current delimiters, then
# interpolated in place of the section.
#
# Also see--
#
# https://github.com/defunkt/pystache/issues/113
#
# TODO: should we check the arity?
val
=
val
(
template
[
section_start_index
:
section_end_index
])
val
=
self
.
_render_value
(
val
,
context
,
delimiters
=
delims
)
parts
.
append
(
val
)
continue
context
.
push
(
val
)
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
)
return
data
def
_render_value
(
self
,
val
,
context
,
delimiters
=
None
):
"""
...
...
@@ -191,6 +150,9 @@ class RenderEngine(object):
val
=
self
.
literal
(
val
)
return
self
.
render
(
val
,
context
,
delimiters
)
def
render_parsed
(
self
,
parsed_template
,
context_stack
):
return
parsed_template
.
render
(
self
,
context_stack
)
def
render
(
self
,
template
,
context_stack
,
delimiters
=
None
):
"""
Render a unicode template string, and return as unicode.
...
...
@@ -203,7 +165,7 @@ class RenderEngine(object):
context_stack: a ContextStack instance.
"""
parser
=
Parser
(
self
,
delimiters
=
delimiters
)
parser
=
Parser
(
delimiters
=
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