Skip to content

Commit

Permalink
Improved Markdown.set_output_format()
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
Waylan Limberg committed Aug 9, 2012
1 parent 1ff936d commit b2939d1
Showing 1 changed file with 10 additions and 4 deletions.
14 changes: 10 additions & 4 deletions markdown/__init__.py
Expand Up @@ -238,11 +238,17 @@ def reset(self):

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):
Expand Down

0 comments on commit b2939d1

Please sign in to comment.