diff options
author | Waylan Limberg <waylan@gmail.com> | 2011-06-23 11:36:00 -0400 |
---|---|---|
committer | Waylan Limberg <waylan@gmail.com> | 2011-06-23 12:16:37 -0400 |
commit | 41194b5f020aef6aa3d980e415162ab2ad541299 (patch) | |
tree | 5acea6c0f15e4b4647cc71d496976e40249550f4 | |
parent | 8525f57ba7fc10b89c1a679aea1400d258fd0a02 (diff) | |
download | markdown-41194b5f020aef6aa3d980e415162ab2ad541299.tar.gz markdown-41194b5f020aef6aa3d980e415162ab2ad541299.tar.bz2 markdown-41194b5f020aef6aa3d980e415162ab2ad541299.zip |
Cleaned up backward compatability of old positional arguments. Some people may be using them, so we should continue to support them. Also adjusted docs to encourage using keyword args only. However, if existing code was using positional args in previous versions, it should still work.
-rw-r--r-- | docs/using_as_module.txt | 17 | ||||
-rw-r--r-- | markdown/__init__.py | 56 |
2 files changed, 53 insertions, 20 deletions
diff --git a/docs/using_as_module.txt b/docs/using_as_module.txt index c7c6da2..6ac82d8 100644 --- a/docs/using_as_module.txt +++ b/docs/using_as_module.txt @@ -22,7 +22,7 @@ functions will serve your needs. However, if you need to process multiple documents, it may be advantageous to create a single instance of the `markdown.Markdown` class and pass multiple documents through it. -### `markdown.markdown(text [, extensions][, **kwargs])` +### `markdown.markdown(text [, **kwargs])` The following options are available on the `markdown.markdown` function: @@ -154,7 +154,7 @@ The following options are available on the `markdown.markdown` function: </ol> -### `markdown.markdownFromFile(input [, output] [, extensions] [, encoding] [, **kwargs])` +### `markdown.markdownFromFile(**kwargs)` With a few exceptions, `markdown.markdownFromFile` accepts the same options as `markdown.markdown`. It does **not** accept a `text` string. Instead, it accepts @@ -183,7 +183,7 @@ the following required options: meet your special needs, it is suggested that you write your own code to handle your specific encoding/decoding needs. -### `markdown.Markdown([extensions][, **kwargs])` +### `markdown.Markdown([**kwargs])` The same options are available when initializing the `markdown.Markdown` class as on the `markdown.markdown` function, except that the class does **not** @@ -209,10 +209,11 @@ must be passed to one of two instance methods: md.reset() html2 = md.convert(text2) -* `Markdown.convertFile(input, output, encoding)` +* `Markdown.convertFile(**kwargs)` The arguments of this method are identical to the arguments of the same - name on the `markdown.markdownFromFile` function. As with the `convert` - method, this method should be used to process multiple files without - creating a new instance of the class for each document. State may need to - be `reset` between each call to `convertFile` as with `convert`. + name on the `markdown.markdownFromFile` function (`input`, `output`, and + `encoding`). As with the `convert` method, this method should be used to + process multiple files without creating a new instance of the class for + each document. State may need to be `reset` between each call to + `convertFile` as with `convert`. diff --git a/markdown/__init__.py b/markdown/__init__.py index 5c6fe08..89b7f9a 100644 --- a/markdown/__init__.py +++ b/markdown/__init__.py @@ -70,7 +70,7 @@ class Markdown: 'xhtml1': to_xhtml_string, } - def __init__(self, extensions=[], **kwargs): + def __init__(self, *args, **kwargs): """ Creates a new Markdown instance. @@ -93,11 +93,23 @@ class Markdown: * html_replacement_text: Text used when safe_mode is set to "replace". * tab_length: Length of tabs in the source. Default: 4 * enable_attributes: Enable the conversion of attributes. Default: True - * smart_emphsasis: Treat `_connected_words_` intelegently Default: True + * smart_emphasis: Treat `_connected_words_` intelegently Default: True * lazy_ol: Ignore number of first item of ordered lists. Default: True """ + # For backward compatability, loop through old positional args + pos = ['extensions', 'extension_configs', 'safe_mode', 'output_format'] + c = 0 + for arg in args: + if not kwargs.has_key(pos[c]): + kwargs[pos[c]] = arg + c += 1 + if c == len(pos): + # ignore any additional args + break + + # Loop throu kwargs and assign defaults for option, default in self.option_defaults.items(): setattr(self, option, kwargs.get(option, default)) @@ -110,8 +122,8 @@ class Markdown: self.references = {} self.htmlStash = util.HtmlStash() - self.registerExtensions(extensions = extensions, - configs = kwargs.get('extension_configs', {})) + self.registerExtensions(extensions=kwargs.get('extensions', []), + configs=kwargs.get('extension_configs', {})) self.set_output_format(kwargs.get('output_format', 'xhtml1')) self.reset() @@ -361,14 +373,34 @@ def markdown(text, *args, **kwargs): return md.convert(text) -def markdownFromFile(input = None, - output = None, - extensions = [], - encoding = None, - *args, **kwargs): - """Read markdown code from a file and write it to a file or a stream.""" - md = Markdown(extensions=extensions, *args, **kwargs) - md.convertFile(input, output, encoding) +def markdownFromFile(*args, **kwargs): + """Read markdown code from a file and write it to a file or a stream. + + This is a shortcut function which initializes an instance of Markdown, + cand calls the convertFile method rather than convert. + + Keyword arguments: + + * input: a file name or readable object. + * output: a file name or writable object. + * encoding: Encoding of input and output. + * Any arguments accepted by the Markdown class. + + """ + # For backward compatability loop through positional args + pos = ['input', 'output', 'extensions', 'encoding'] + c = 0 + for arg in args: + if not kwargs.has_key(pos[c]): + kwargs[pos[c]] = arg + c += 1 + if c == len(pos): + break + + md = Markdown(**kwargs) + md.convertFile(kwargs.get('input', None), + kwargs.get('output', None), + kwargs.get('encoding', None)) |