diff options
author | ianb <devnull@localhost> | 2007-12-18 04:21:29 +0000 |
---|---|---|
committer | ianb <devnull@localhost> | 2007-12-18 04:21:29 +0000 |
commit | 6f69fbd4a865810c1b2a7ec5e566040de74f5ca7 (patch) | |
tree | 31f63b27318ff17e1a787e8fd7abbaae152aee4f | |
parent | 630224ecd88e57faf67679ced9c42a6454221990 (diff) | |
download | tempita-6f69fbd4a865810c1b2a7ec5e566040de74f5ca7.tar.gz |
Iprove the exception catching some. Added __html__() on html literals
-rw-r--r-- | docs/index.txt | 6 | ||||
-rw-r--r-- | tempita/__init__.py | 23 |
2 files changed, 21 insertions, 8 deletions
diff --git a/docs/index.txt b/docs/index.txt index cde28e9..5d1b4cf 100644 --- a/docs/index.txt +++ b/docs/index.txt @@ -419,4 +419,8 @@ svn trunk --------- * Added ``html_quote`` to default functions provided in - ``HTMLTemplate`` + ``HTMLTemplate``. + +* HTML literals have an ``.__html__()`` method, and the presence of + that method is used to determine if values need to be quoted in + ``HTMLTemplate``. diff --git a/tempita/__init__.py b/tempita/__init__.py index aa6cd8f..865d9ae 100644 --- a/tempita/__init__.py +++ b/tempita/__init__.py @@ -47,13 +47,13 @@ class TemplateError(Exception): """ def __init__(self, message, position, name=None): - self.message = message + Exception.__init__(self, message) self.position = position self.name = name def __str__(self): msg = '%s at line %s column %s' % ( - self.message, self.position[0], self.position[1]) + ' '.join(self.args), self.position[0], self.position[1]) if self.name: msg += ' in %s' % self.name return msg @@ -224,7 +224,7 @@ class Template(object): except: exc_info = sys.exc_info() e = exc_info[1] - if getattr(e, 'args'): + if getattr(e, 'args', None): arg0 = e.args[0] else: arg0 = str(e) @@ -347,11 +347,15 @@ class html(object): self.value = value def __str__(self): return self.value + def __html__(self): + return self.value def __repr__(self): return '<%s %r>' % ( self.__class__.__name__, self.value) -def html_quote(value): +def html_quote(value, force=True): + if not force and hasattr(value, '__html__'): + return value.__html__() if value is None: return '' if not isinstance(value, basestring): @@ -397,11 +401,16 @@ class HTMLTemplate(Template): )) def _repr(self, value, pos): - plain = Template._repr(self, value, pos) - if isinstance(value, html): - return plain + if hasattr(value, '__html__'): + value = value.__html__() + quote = False else: + quote = True + plain = Template._repr(self, value, pos) + if quote: return html_quote(plain) + else: + return plain def sub_html(content, **kw): name = kw.get('__name') |