Skip to content

Commit

Permalink
Merge pull request #44 from zopefoundation/issue_43
Browse files Browse the repository at this point in the history
Make rendering encoding configurable
  • Loading branch information
dataflake authored Apr 25, 2019
2 parents fe55d7d + 44a1c60 commit 40021f7
Show file tree
Hide file tree
Showing 16 changed files with 140 additions and 88 deletions.
3 changes: 3 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ Changelog
3.0b7 (unreleased)
------------------

- Make the rendering encoding configurable to fix rendering on Zope 4
(`#43 <https://github.com/zopefoundation/DocumentTemplate/issues/43>`_)

- Add unit tests for ``dtml-if``, ``dtml-unless`` and ``dtml-in`` variables
(`#7 <https://github.com/zopefoundation/DocumentTemplate/issues/7>`_)

Expand Down
6 changes: 4 additions & 2 deletions src/DocumentTemplate/DT_If.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,12 +89,13 @@ class If(object):
elses = None
expr = ''

def __init__(self, blocks):
def __init__(self, blocks, encoding=None):

tname, args, section = blocks[0]
args = parse_params(args, name='', expr='')
name, expr = name_param(args, 'if', 1)
self.__name__ = name
self.encoding = encoding
if expr is None:
cond = name
else:
Expand Down Expand Up @@ -136,7 +137,8 @@ class Unless(object):
name = 'unless'
blockContinuations = ()

def __init__(self, blocks):
def __init__(self, blocks, encoding=None):
self.encoding = encoding
tname, args, section = blocks[0]
args = parse_params(args, name='', expr='')
name, expr = name_param(args, 'unless', 1)
Expand Down
31 changes: 17 additions & 14 deletions src/DocumentTemplate/DT_In.py
Original file line number Diff line number Diff line change
Expand Up @@ -335,6 +335,8 @@
import re
import functools

import six

from DocumentTemplate.DT_Util import ParseError, parse_params, name_param
from DocumentTemplate.DT_Util import str, join_unicode
from DocumentTemplate.DT_Util import render_blocks, InstanceDict
Expand All @@ -350,15 +352,15 @@ def cmp(a, b):
return (a > b) - (a < b)

TupleType = tuple
StringTypes = (str, unicode)
StringTypes = six.string_types + (six.binary_type,)


class InFactory(object):
blockContinuations = ('else', )
name = 'in'

def __call__(self, blocks):
i = InClass(blocks)
def __call__(self, blocks, encoding=None):
i = InClass(blocks, encoding)
if i.batch:
return i.renderwb
else:
Expand All @@ -375,7 +377,7 @@ class InClass(object):
reverse = None
sort_expr = reverse_expr = None

def __init__(self, blocks):
def __init__(self, blocks, encoding=None):
tname, args, section = blocks[0]
args = parse_params(args, name='', start='1', end='-1', size='10',
orphan='0', overlap='1', mapping=1,
Expand All @@ -385,6 +387,7 @@ def __init__(self, blocks):
reverse=1, sort_expr='', reverse_expr='',
prefix='')
self.args = args
self.encoding = encoding

if 'sort' in args:
self.sort = sort = args['sort']
Expand Down Expand Up @@ -461,7 +464,7 @@ def renderwb(self, md):

if not sequence:
if self.elses:
return render_blocks(self.elses, md)
return render_blocks(self.elses, md, encoding=self.encoding)
return ''

if isinstance(sequence, str):
Expand Down Expand Up @@ -545,10 +548,10 @@ def renderwb(self, md):
pkw['previous-sequence-start-index'] = pstart - 1
pkw['previous-sequence-end-index'] = pend - 1
pkw['previous-sequence-size'] = pend + 1 - pstart
result = render(section, md)
result = render(section, md, encoding=self.encoding)

elif self.elses:
result = render(self.elses, md)
result = render(self.elses, md, encoding=self.encoding)
else:
result = ''
elif next:
Expand All @@ -559,7 +562,7 @@ def renderwb(self, md):
sequence[end]
except IndexError:
if self.elses:
result = render(self.elses, md)
result = render(self.elses, md, encoding=self.encoding)
else:
result = ''
else:
Expand All @@ -569,7 +572,7 @@ def renderwb(self, md):
pkw['next-sequence-start-index'] = pstart - 1
pkw['next-sequence-end-index'] = pend - 1
pkw['next-sequence-size'] = pend + 1 - pstart
result = render(section, md)
result = render(section, md, encoding=self.encoding)
else:
result = []
append = result.append
Expand Down Expand Up @@ -639,15 +642,15 @@ def renderwb(self, md):
push(InstanceDict(client, md))

try:
append(render(section, md))
append(render(section, md, encoding=self.encoding))
finally:
if pushed:
pop()

if index == first:
pkw['sequence-start'] = 0

result = join_unicode(result)
result = join_unicode(result, encoding=self.encoding)

finally:
if cache:
Expand All @@ -669,7 +672,7 @@ def renderwob(self, md):

if not sequence:
if self.elses:
return render_blocks(self.elses, md)
return render_blocks(self.elses, md, encoding=self.encoding)
return ''

if isinstance(sequence, str):
Expand Down Expand Up @@ -752,14 +755,14 @@ def renderwob(self, md):
push(InstanceDict(client, md))

try:
append(render(section, md))
append(render(section, md, encoding=self.encoding))
finally:
if pushed:
pop()
if index == 0:
pkw['sequence-start'] = 0

result = join_unicode(result)
result = join_unicode(result, encoding=self.encoding)

finally:
if cache:
Expand Down
5 changes: 3 additions & 2 deletions src/DocumentTemplate/DT_Let.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,10 @@ class Let(object):
blockContinuations = ()
name = 'let'

def __init__(self, blocks):
def __init__(self, blocks, encoding=None):
tname, args, section = blocks[0]
self.__name__ = args
self.encoding = encoding
self.section = section.blocks
self.args = args = parse_let_params(args)

Expand All @@ -81,7 +82,7 @@ def render(self, md):
d[name] = md[expr]
else:
d[name] = expr(md)
return render_blocks(self.section, md)
return render_blocks(self.section, md, encoding=self.encoding)
finally:
md._pop(1)

Expand Down
5 changes: 3 additions & 2 deletions src/DocumentTemplate/DT_Raise.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,10 @@ class Raise(object):
name = 'raise'
expr = ''

def __init__(self, blocks):
def __init__(self, blocks, encoding=None):

tname, args, section = blocks[0]
self.encoding = encoding
self.section = section.blocks
args = parse_params(args, type='', expr='')
self.__name__, self.expr = name_param(args, 'raise', 1, attr='type')
Expand All @@ -63,7 +64,7 @@ def render(self, md):
t = InvalidErrorTypeExpression

try:
v = render_blocks(self.section, md)
v = render_blocks(self.section, md, encoding=self.encoding)
except Exception:
v = 'Invalid Error Value'

Expand Down
3 changes: 2 additions & 1 deletion src/DocumentTemplate/DT_Return.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,12 @@ class ReturnTag(object):
name = 'return'
expr = None

def __init__(self, args):
def __init__(self, args, encoding=None):
args = parse_params(args, name='', expr='')
name, expr = name_param(args, 'var', 1)
self.__name__ = name
self.expr = expr
self.encoding = encoding

def render(self, md):
if self.expr is None:
Expand Down
8 changes: 5 additions & 3 deletions src/DocumentTemplate/DT_String.py
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,7 @@ def parse_block(self, text, start, result, tagre,
sstart = start
else:
try:
r = scommand(blocks)
r = scommand(blocks, encoding=self.encoding)
if hasattr(r, 'simple_form'):
r = r.simple_form
result.append(r)
Expand Down Expand Up @@ -294,14 +294,15 @@ def parse_close(self, text, start, tagre, stag, sloc, scommand, sa):
shared_globals = {}

def __init__(self, source_string='', mapping=None, __name__='<string>',
**vars):
encoding=None, **vars):
"""\
Create a document template from a string.
The optional parameter, 'mapping', may be used to provide a
mapping object containing defaults for values to be inserted.
"""
self.raw = source_string
self.encoding = encoding
self.initvars(mapping, vars)
self.setName(__name__)

Expand Down Expand Up @@ -516,7 +517,8 @@ def __call__(self, client=None, mapping={}, **kw):
value = self.ZDocumentTemplate_beforeRender(md, _marker)
if value is _marker:
try:
result = render_blocks(self._v_blocks, md)
result = render_blocks(self._v_blocks, md,
encoding=self.encoding)
except DTReturn as v:
result = v.v
self.ZDocumentTemplate_afterRender(md, result)
Expand Down
15 changes: 9 additions & 6 deletions src/DocumentTemplate/DT_Try.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,11 +90,12 @@ class Try(object):
finallyBlock = None
elseBlock = None

def __init__(self, blocks):
def __init__(self, blocks, encoding=None):
tname, args, section = blocks[0]

self.args = parse_params(args)
self.section = section.blocks
self.encoding = encoding

# Find out if this is a try..finally type
if len(blocks) == 2 and blocks[1][0] == 'finally':
Expand Down Expand Up @@ -148,7 +149,7 @@ def render_try_except(self, md):

# first we try to render the first block
try:
result = render_blocks(self.section, md)
result = render_blocks(self.section, md, encoding=self.encoding)
except DTReturn:
raise
except Exception:
Expand All @@ -170,7 +171,7 @@ def render_try_except(self, md):
ns = namespace(md, error_type=errname, error_value=v,
error_tb=error_tb)[0]
md._push(InstanceDict(ns, md))
return render_blocks(handler, md)
return render_blocks(handler, md, encoding=self.encoding)
finally:
md._pop(1)

Expand All @@ -179,16 +180,18 @@ def render_try_except(self, md):
if (self.elseBlock is None):
return result
else:
return result + render_blocks(self.elseBlock, md)
return result + render_blocks(self.elseBlock, md,
encoding=self.encoding)

def render_try_finally(self, md):
result = ''
# first try to render the first block
try:
result = render_blocks(self.section, md)
result = render_blocks(self.section, md, encoding=self.encoding)
# Then handle finally block
finally:
result = result + render_blocks(self.finallyBlock, md)
result = result + render_blocks(self.finallyBlock, md,
encoding=self.encoding)
return result

def find_handler(self, exception):
Expand Down
8 changes: 5 additions & 3 deletions src/DocumentTemplate/DT_Var.py
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ class Var(object):
name = 'var'
expr = None

def __init__(self, args, fmt='s'):
def __init__(self, args, fmt='s', encoding=None):
if args[:4] == 'var ':
args = args[4:]
args = parse_params(args, name='', lower=1, upper=1, expr='',
Expand All @@ -186,6 +186,7 @@ def __init__(self, args, fmt='s'):
url_unquote_plus=1, missing='',
newline_to_br=1, url=1)
self.args = args
self.encoding = encoding

self.modifiers = tuple(
map(lambda t: t[1],
Expand Down Expand Up @@ -350,14 +351,15 @@ class Call(object):
name = 'call'
expr = None

def __init__(self, args):
def __init__(self, args, encoding=None):
args = parse_params(args, name='', expr='')
name, expr = name_param(args, 'call', 1)
if expr is None:
expr = name
else:
expr = expr.eval
self.simple_form = ('i', expr, None)
self.encoding = encoding


def url_quote(v, name='(Unknown name)', md={}):
Expand Down Expand Up @@ -586,7 +588,7 @@ class Comment(object):
name = 'comment'
blockContinuations = ()

def __init__(self, args, fmt=''):
def __init__(self, args, fmt='', encoding=None):
pass

def render(self, md):
Expand Down
5 changes: 3 additions & 2 deletions src/DocumentTemplate/DT_With.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ class With(object):
mapping = None
only = 0

def __init__(self, blocks):
def __init__(self, blocks, encoding=None):
tname, args, section = blocks[0]
args = parse_params(args, name='', expr='', mapping=1, only=1)
name, expr = name_param(args, 'with', 1)
Expand All @@ -53,6 +53,7 @@ def __init__(self, blocks):
else:
expr = expr.eval
self.__name__, self.expr = name, expr
self.encoding = encoding
self.section = section.blocks
if 'mapping' in args and args['mapping']:
self.mapping = 1
Expand Down Expand Up @@ -81,7 +82,7 @@ def render(self, md):

md._push(v)
try:
return render_blocks(self.section, md)
return render_blocks(self.section, md, encoding=self.encoding)
finally:
md._pop(1)

Expand Down
Loading

0 comments on commit 40021f7

Please sign in to comment.