Skip to content

Commit 362782d

Browse files
author
Waylan Limberg
committed
Markdown.convertFile now actually works with stdin and stdout. Previously only the commandline script did.
1 parent 9b51cc6 commit 362782d

File tree

2 files changed

+9
-5
lines changed

2 files changed

+9
-5
lines changed

docs/using_as_module.txt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -167,10 +167,11 @@ the following required options:
167167

168168
* `input` (required): The source text file.
169169

170-
`input` may be set to one of two options:
170+
`input` may be set to one of three options:
171171

172172
* a string which contains a path to a readable file on the file system,
173-
* or a readable file-like object.
173+
* a readable file-like object,
174+
* or `None` (default) which will read from `stdin`.
174175

175176
* `output`: The target which output to written to.
176177

markdown/__init__.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535

3636
import re
3737
import codecs
38+
import sys
3839
import logging
3940
import util
4041
from preprocessors import build_preprocessors
@@ -320,8 +321,8 @@ def convertFile(self, input=None, output=None, encoding=None):
320321
321322
Keyword arguments:
322323
323-
* input: File object or path of file as string.
324-
* output: Name of output file. Writes to stdout if `None`.
324+
* input: File object or path. Reads from stdin if `None`.
325+
* output: File object or path. Writes to stdout if `None`.
325326
* encoding: Encoding of input and output files. Defaults to utf-8.
326327
327328
"""
@@ -332,6 +333,7 @@ def convertFile(self, input=None, output=None, encoding=None):
332333
if isinstance(input, basestring):
333334
input_file = codecs.open(input, mode="r", encoding=encoding)
334335
else:
336+
input = input or sys.stdin
335337
input_file = codecs.getreader(encoding)(input)
336338
text = input_file.read()
337339
input_file.close()
@@ -341,13 +343,14 @@ def convertFile(self, input=None, output=None, encoding=None):
341343
html = self.convert(text)
342344

343345
# Write to file or stdout
344-
if isinstance(output, (str, unicode)):
346+
if isinstance(output, basestring):
345347
output_file = codecs.open(output, "w",
346348
encoding=encoding,
347349
errors="xmlcharrefreplace")
348350
output_file.write(html)
349351
output_file.close()
350352
else:
353+
output = output or sys.stdout
351354
output.write(html.encode(encoding, "xmlcharrefreplace"))
352355

353356
return self

0 commit comments

Comments
 (0)