aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorWaylan Limberg <waylan@gmail.com>2011-08-03 13:57:18 -0400
committerWaylan Limberg <waylan@gmail.com>2011-08-03 13:57:18 -0400
commit362782d1e08089c74f5bfcf0937ace4fa5b49eea (patch)
tree136fc3eba404708c9f414816ac5c703dec2bdf5e
parent9b51cc6aab4bbb18b925328e5d918fa56a5931e2 (diff)
downloadmarkdown-362782d1e08089c74f5bfcf0937ace4fa5b49eea.tar.gz
markdown-362782d1e08089c74f5bfcf0937ace4fa5b49eea.tar.bz2
markdown-362782d1e08089c74f5bfcf0937ace4fa5b49eea.zip
Markdown.convertFile now actually works with stdin and stdout. Previously only the commandline script did.
-rw-r--r--docs/using_as_module.txt5
-rw-r--r--markdown/__init__.py9
2 files changed, 9 insertions, 5 deletions
diff --git a/docs/using_as_module.txt b/docs/using_as_module.txt
index f50a0ec..662affd 100644
--- a/docs/using_as_module.txt
+++ b/docs/using_as_module.txt
@@ -167,10 +167,11 @@ the following required options:
* `input` (required): The source text file.
- `input` may be set to one of two options:
+ `input` may be set to one of three options:
* a string which contains a path to a readable file on the file system,
- * or a readable file-like object.
+ * a readable file-like object,
+ * or `None` (default) which will read from `stdin`.
* `output`: The target which output to written to.
diff --git a/markdown/__init__.py b/markdown/__init__.py
index 57ed587..ce68e9a 100644
--- a/markdown/__init__.py
+++ b/markdown/__init__.py
@@ -35,6 +35,7 @@ version_info = (2,1,0, "Dev")
import re
import codecs
+import sys
import logging
import util
from preprocessors import build_preprocessors
@@ -320,8 +321,8 @@ class Markdown:
Keyword arguments:
- * input: File object or path of file as string.
- * output: Name of output file. Writes to stdout if `None`.
+ * input: File object or path. Reads from stdin if `None`.
+ * output: File object or path. Writes to stdout if `None`.
* encoding: Encoding of input and output files. Defaults to utf-8.
"""
@@ -332,6 +333,7 @@ class Markdown:
if isinstance(input, basestring):
input_file = codecs.open(input, mode="r", encoding=encoding)
else:
+ input = input or sys.stdin
input_file = codecs.getreader(encoding)(input)
text = input_file.read()
input_file.close()
@@ -341,13 +343,14 @@ 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,
errors="xmlcharrefreplace")
output_file.write(html)
output_file.close()
else:
+ output = output or sys.stdout
output.write(html.encode(encoding, "xmlcharrefreplace"))
return self