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
0f986093
Commit
0f986093
authored
Mar 16, 2011
by
Pieter van de Bruggen
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Significant improvements to Section handling.
parent
327c8837
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
30 additions
and
24 deletions
+30
-24
pystache/template.py
+30
-24
No files found.
pystache/template.py
View file @
0f986093
import
re
import
cgi
def
call
(
view
):
def
_
(
x
):
return
unicode
(
callable
(
x
)
and
x
(
view
)
or
x
)
return
_
def
call
(
view
,
x
):
if
callable
(
x
):
x
=
x
(
view
)
return
unicode
(
x
)
def
sectionTag
(
name
,
template
,
delims
):
def
sectionTag
(
name
,
parsed
,
template
,
delims
):
def
func
(
view
):
if
not
view
.
get
(
name
):
data
=
view
.
get
(
name
)
if
not
data
:
return
''
print
template
tmpl
=
Template
(
template
)
tmpl
.
view
=
view
(
tmpl
.
otag
,
tmpl
.
ctag
)
=
delims
view
.
context_list
=
[
view
.
get
(
name
)]
+
view
.
context_list
string
=
''
.
join
(
map
(
call
(
view
),
tmpl
.
_parse
()))
return
string
elif
type
(
data
)
not
in
[
list
,
tuple
]:
data
=
[
data
]
parts
=
[]
for
element
in
data
:
view
.
context_list
.
insert
(
0
,
element
)
parts
.
append
(
''
.
join
(
map
(
call
,
[
view
]
*
len
(
parsed
),
parsed
)))
del
view
.
context_list
[
0
]
return
''
.
join
(
parts
)
return
func
def
inverseTag
(
name
,
template
,
delims
):
def
inverseTag
(
name
,
parsed
,
template
,
delims
):
def
func
(
view
):
if
view
.
get
(
name
):
data
=
view
.
get
(
name
)
if
data
:
return
''
tmpl
=
Template
(
template
)
tmpl
.
view
=
view
(
tmpl
.
otag
,
tmpl
.
ctag
)
=
delims
return
''
.
join
(
map
(
call
(
view
),
tmpl
.
_parse
()))
return
''
.
join
(
map
(
call
,
[
view
]
*
len
(
parsed
),
parsed
))
return
func
def
escapedTag
(
name
):
...
...
@@ -40,7 +42,8 @@ def unescapedTag(name):
return
func
class
EndOfSection
(
Exception
):
def
__init__
(
self
,
template
,
position
):
def
__init__
(
self
,
buffer
,
template
,
position
):
self
.
buffer
=
buffer
self
.
template
=
template
self
.
position
=
position
...
...
@@ -105,10 +108,11 @@ class Template(object):
# Save the literal text content.
buffer
.
append
(
captures
[
'content'
])
pos
=
match
.
end
()
tagPos
=
match
.
end
(
'content'
)
# Standalone (non-interpolation) tags consume the entire line,
# both leading whitespace and trailing newline.
tagBeganLine
=
(
not
buffer
[
-
1
]
or
buffer
[
-
1
][
-
1
]
==
'
\n
'
)
tagBeganLine
=
not
tagPos
or
template
[
tagPos
-
1
]
==
'
\n
'
tagEndedLine
=
(
pos
==
len
(
template
)
or
template
[
pos
]
==
'
\n
'
)
interpolationTag
=
captures
[
'tag'
]
in
[
''
,
'&'
,
'{'
]
...
...
@@ -116,6 +120,7 @@ class Template(object):
pos
+=
1
elif
captures
[
'whitespace'
]:
buffer
.
append
(
captures
[
'whitespace'
])
tagPos
+=
len
(
captures
[
'whitespace'
])
captures
[
'whitespace'
]
=
''
name
=
captures
[
'name'
]
...
...
@@ -130,13 +135,14 @@ class Template(object):
try
:
self
.
_parse
(
template
,
name
,
pos
)
except
EndOfSection
as
e
:
bufr
=
e
.
buffer
tmpl
=
e
.
template
pos
=
e
.
position
tag
=
{
'#'
:
sectionTag
,
'^'
:
inverseTag
}[
captures
[
'tag'
]]
buffer
.
append
(
tag
(
name
,
tmpl
,
(
self
.
otag
,
self
.
ctag
)))
buffer
.
append
(
tag
(
name
,
bufr
,
tmpl
,
(
self
.
otag
,
self
.
ctag
)))
elif
captures
[
'tag'
]
==
'/'
:
raise
EndOfSection
(
template
[
index
:
match
.
end
(
'whitespace'
)
],
pos
)
raise
EndOfSection
(
buffer
,
template
[
index
:
tagPos
],
pos
)
elif
captures
[
'tag'
]
in
[
'{'
,
'&'
]:
buffer
.
append
(
unescapedTag
(
name
))
elif
captures
[
'tag'
]
==
''
:
...
...
@@ -148,7 +154,7 @@ class Template(object):
def
render
(
self
,
encoding
=
None
):
parsed
=
self
.
_parse
()
result
=
''
.
join
(
map
(
call
(
self
.
view
),
parsed
))
result
=
''
.
join
(
map
(
call
,
[
self
.
view
]
*
len
(
parsed
),
parsed
))
if
encoding
is
not
None
:
result
=
result
.
encode
(
encoding
)
...
...
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