diff options
Diffstat (limited to 'markdown/__init__.py')
-rw-r--r-- | markdown/__init__.py | 59 |
1 files changed, 33 insertions, 26 deletions
diff --git a/markdown/__init__.py b/markdown/__init__.py index 086fde9..26314f6 100644 --- a/markdown/__init__.py +++ b/markdown/__init__.py @@ -39,8 +39,8 @@ Copyright 2004 Manfred Stienstra (the original version) License: BSD (see docs/LICENSE for details). """ -version = "2.0.1" -version_info = (2,0,1, "Final") +version = "2.0.3" +version_info = (2,0,3, "Final") import re import codecs @@ -182,7 +182,7 @@ class Markdown: def __init__(self, extensions=[], extension_configs={}, - safe_mode = False, + safe_mode = False, output_format=DEFAULT_OUTPUT_FORMAT): """ Creates a new Markdown instance. @@ -200,12 +200,12 @@ class Markdown: * "xhtml": Outputs latest supported version of XHTML (currently XHTML 1.1). * "html4": Outputs HTML 4 * "html": Outputs latest supported version of HTML (currently HTML 4). - Note that it is suggested that the more specific formats ("xhtml1" + Note that it is suggested that the more specific formats ("xhtml1" and "html4") be used as "xhtml" or "html" may change in the future - if it makes sense at that time. + if it makes sense at that time. """ - + self.safeMode = safe_mode self.registeredExtensions = [] self.docType = "" @@ -300,9 +300,9 @@ class Markdown: # Map format keys to serializers self.output_formats = { - 'html' : html4.to_html_string, + 'html' : html4.to_html_string, 'html4' : html4.to_html_string, - 'xhtml' : etree.tostring, + 'xhtml' : etree.tostring, 'xhtml1': etree.tostring, } @@ -327,12 +327,14 @@ class Markdown: for ext in extensions: if isinstance(ext, basestring): ext = load_extension(ext, configs.get(ext, [])) - try: - ext.extendMarkdown(self, globals()) - except AttributeError: - message(ERROR, "Incorrect type! Extension '%s' is " - "neither a string or an Extension." %(repr(ext))) - + if isinstance(ext, Extension): + try: + ext.extendMarkdown(self, globals()) + except NotImplementedError, e: + message(ERROR, e) + else: + message(ERROR, 'Extension "%s.%s" must be of type: "markdown.Extension".' \ + % (ext.__class__.__module__, ext.__class__.__name__)) def registerExtension(self, extension): """ This gets called by the extension """ @@ -346,7 +348,8 @@ class Markdown: self.references.clear() for extension in self.registeredExtensions: - extension.reset() + if hasattr(extension, 'reset'): + extension.reset() def set_output_format(self, format): """ Set the output format for the class instance. """ @@ -395,7 +398,7 @@ class Markdown: root = newRoot # Serialize _properly_. Strip top-level tags. - output, length = codecs.utf_8_decode(self.serializer(root, encoding="utf8")) + output, length = codecs.utf_8_decode(self.serializer(root, encoding="utf-8")) if self.stripTopLevelTags: try: start = output.index('<%s>'%DOC_TAG)+len(DOC_TAG)+2 @@ -429,7 +432,7 @@ class Markdown: Keyword arguments: - * input: Name of source text file. + * input: File object or path of file as string. * output: Name of output file. Writes to stdout if `None`. * encoding: Encoding of input and output files. Defaults to utf-8. @@ -438,7 +441,10 @@ class Markdown: encoding = encoding or "utf-8" # Read the source - input_file = codecs.open(input, mode="r", encoding=encoding) + if isinstance(input, basestring): + input_file = codecs.open(input, mode="r", encoding=encoding) + else: + input_file = input text = input_file.read() input_file.close() text = text.lstrip(u'\ufeff') # remove the byte-order mark @@ -447,7 +453,7 @@ class Markdown: html = self.convert(text) # Write to file or stdout - if isinstance(output, (str, unicode)): + if isinstance(output, basestring): output_file = codecs.open(output, "w", encoding=encoding) output_file.write(html) output_file.close() @@ -499,7 +505,8 @@ class Extension: * md_globals: Global variables in the markdown module namespace. """ - pass + raise NotImplementedError, 'Extension "%s.%s" must define an "extendMarkdown"' \ + 'method.' % (self.__class__.__module__, self.__class__.__name__) def load_extension(ext_name, configs = []): @@ -540,8 +547,8 @@ def load_extension(ext_name, configs = []): # function called makeExtension() try: return module.makeExtension(configs.items()) - except AttributeError: - message(CRITICAL, "Failed to initiate extension '%s'" % ext_name) + except AttributeError, e: + message(CRITICAL, "Failed to initiate extension '%s': %s" % (ext_name, e)) def load_extensions(ext_names): @@ -582,15 +589,15 @@ def markdown(text, * "xhtml": Outputs latest supported version of XHTML (currently XHTML 1.1). * "html4": Outputs HTML 4 * "html": Outputs latest supported version of HTML (currently HTML 4). - Note that it is suggested that the more specific formats ("xhtml1" + Note that it is suggested that the more specific formats ("xhtml1" and "html4") be used as "xhtml" or "html" may change in the future - if it makes sense at that time. + if it makes sense at that time. Returns: An HTML document as a string. """ md = Markdown(extensions=load_extensions(extensions), - safe_mode=safe_mode, + safe_mode=safe_mode, output_format=output_format) return md.convert(text) @@ -602,7 +609,7 @@ def markdownFromFile(input = None, safe_mode = False, output_format = DEFAULT_OUTPUT_FORMAT): """Read markdown code from a file and write it to a file or a stream.""" - md = Markdown(extensions=load_extensions(extensions), + md = Markdown(extensions=load_extensions(extensions), safe_mode=safe_mode, output_format=output_format) md.convertFile(input, output, encoding) |