From b2939d19260aca2a308e2ff5f135d36e5bdecc60 Mon Sep 17 00:00:00 2001 From: Waylan Limberg Date: Thu, 9 Aug 2012 05:47:23 -0400 Subject: 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. --- markdown/__init__.py | 14 ++++++++++---- 1 file 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): -- cgit v1.2.3