aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorWaylan Limberg <waylan@gmail.com>2012-08-09 05:47:23 -0400
committerWaylan Limberg <waylan@gmail.com>2012-08-09 05:47:23 -0400
commitb2939d19260aca2a308e2ff5f135d36e5bdecc60 (patch)
treec2998a7b58b09afd3c48963c635a37a08562ea87
parent1ff936d015b7eb125bf6bb373246b5b0a7291412 (diff)
downloadmarkdown-b2939d19260aca2a308e2ff5f135d36e5bdecc60.tar.gz
markdown-b2939d19260aca2a308e2ff5f135d36e5bdecc60.tar.bz2
markdown-b2939d19260aca2a308e2ff5f135d36e5bdecc60.zip
Improved `Markdown.set_output_format()`
Specificaly, `self.output_format` is defined and contains a string of the output format used on the instance. This is more useful that an instance of the searializer when determining alternate behavior elsewhere in the parser. For example, see Issue #129. Also cleaned up the error when an invalid format is provided. We now re-raise the original error (with a custom message) rather than raising a new error.
-rw-r--r--markdown/__init__.py14
1 files changed, 10 insertions, 4 deletions
diff --git a/markdown/__init__.py b/markdown/__init__.py
index b637c88..58993dd 100644
--- a/markdown/__init__.py
+++ b/markdown/__init__.py
@@ -238,11 +238,17 @@ class Markdown:
def set_output_format(self, format):
""" Set the output format for the class instance. """
+ self.output_format = format.lower()
try:
- self.serializer = self.output_formats[format.lower()]
- except KeyError:
- raise KeyError('Invalid Output Format: "%s". Use one of %s.' \
- % (format, self.output_formats.keys()))
+ self.serializer = self.output_formats[self.output_format]
+ except KeyError, e:
+ valid_formats = self.output_formats.keys()
+ valid_formats.sort()
+ message = 'Invalid Output Format: "%s". Use one of %s.' \
+ % (self.output_format,
+ '"' + '", "'.join(valid_formats) + '"')
+ e.args = (message,) + e.args[1:]
+ raise
return self
def convert(self, source):