From cdcfbacb23a5b9bc5a1c1af058d90e61364d61b0 Mon Sep 17 00:00:00 2001 From: Waylan Limberg Date: Mon, 16 Mar 2009 22:20:39 -0400 Subject: Output formats are now listed in a Dict mapped to their respective serializers. Extensions can now easily add their own serializers. --- markdown/__init__.py | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/markdown/__init__.py b/markdown/__init__.py index 2b5231a..2a2e7cb 100644 --- a/markdown/__init__.py +++ b/markdown/__init__.py @@ -298,6 +298,14 @@ class Markdown: postprocessors.AndSubstitutePostprocessor() # footnote postprocessor will be inserted with ">amp_substitute" + # Map format keys to serializers + self.output_formats = { + 'html' : html4.to_html_string, + 'html4' : html4.to_html_string, + 'xhtml' : etree.tostring, + 'xhtml1': etree.tostring, + } + self.references = {} self.htmlStash = preprocessors.HtmlStash() self.registerExtensions(extensions = extensions, @@ -342,12 +350,11 @@ class Markdown: def set_output_format(self, format): """ Set the output format for the class instance. """ - if format.lower() in ['html', 'html4']: - self.serializer = html4.to_html_string - elif format.lower() in ['xhtml', 'xhtml1']: - self.serializer = etree.tostring - else: - message(CRITICAL, 'Invalid Output Format: "%s". Use one of "xhtml1" or "html4".' % format) + try: + self.serializer = self.output_formats[format.lower()] + except KeyError: + message(CRITICAL, 'Invalid Output Format: "%s". Use one of %s.' \ + % (format, self.output_formats.keys())) def convert(self, source): """ -- cgit v1.2.3