diff options
author | Waylan Limberg <waylan@dev.(none)> | 2009-10-21 14:15:42 -0400 |
---|---|---|
committer | Waylan Limberg <waylan@dev.(none)> | 2009-10-21 14:15:42 -0400 |
commit | 215fa1875651547b5a1534f23cf637fb082e2a77 (patch) | |
tree | 1ad4d2359c269236113cd236681d9d5253fdeab5 | |
parent | 8d64909b296d293258910a7f67cc927630cd5302 (diff) | |
download | markdown-215fa1875651547b5a1534f23cf637fb082e2a77.tar.gz markdown-215fa1875651547b5a1534f23cf637fb082e2a77.tar.bz2 markdown-215fa1875651547b5a1534f23cf637fb082e2a77.zip |
The commandline script now accepts input from stdin. By extension markdownFromFile and Markdown.convertFile accept any file like object as input as well as a file name as string.
-rw-r--r-- | markdown/__init__.py | 9 | ||||
-rw-r--r-- | markdown/commandline.py | 5 |
2 files changed, 8 insertions, 6 deletions
diff --git a/markdown/__init__.py b/markdown/__init__.py index bd52113..211e3d5 100644 --- a/markdown/__init__.py +++ b/markdown/__init__.py @@ -431,7 +431,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. @@ -440,7 +440,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 @@ -449,7 +452,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() diff --git a/markdown/commandline.py b/markdown/commandline.py index 7acc06d..ac6d710 100644 --- a/markdown/commandline.py +++ b/markdown/commandline.py @@ -42,9 +42,8 @@ def parse_options(): (options, args) = parser.parse_args() - if not len(args) == 1: - parser.print_help() - return None, None + if len(args) == 0: + input_file = sys.stdin else: input_file = args[0] |