Skip to content

Commit

Permalink
Fixed issues/3
Browse files Browse the repository at this point in the history
The Python 3 patch broke writing to standard output. This fixes the problem by
moving the call to pconv.convert() back to its proper place, and by accounting
for the different approaches to encoded output on sys.stdout in Python 2 and
Python 3 when opening the `out` filehandle for the first time.

Bug report: #3
  • Loading branch information
Johannes Grassler committed Jan 18, 2016
1 parent e3db3a1 commit abe0f26
Showing 1 changed file with 12 additions and 5 deletions.
17 changes: 12 additions & 5 deletions mkdocs_pandoc/cli/mkdocs2pandoc.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,13 @@ def main():

args = opts.parse_args()

out = codecs.getwriter(args.encoding)(sys.stdout)
# Python 2 and Python 3 have mutually incompatible approaches to writing
# encoded data to sys.stdout, so we'll have to pick the appropriate one.

if sys.version_info.major == 2:
out = codecs.getwriter(args.encoding)(sys.stdout)
elif sys.version_info.major >= 3:
out = open(sys.stdout.fileno(), mode='w', encoding=args.encoding, buffering=1)

try:
pconv = mkdocs_pandoc.PandocConverter(
Expand All @@ -67,9 +73,10 @@ def main():
return(e.status)
if args.outfile:
try:
with codecs.open(args.outfile, 'w', encoding=args.encoding) as out:
for line in pconv.convert():
out.write(line + '\n')
out.close()
out = codecs.open(args.outfile, 'w', encoding=args.encoding)
except IOError as e:
print("Couldn't open %s for writing: %s" % (args.outfile, e.strerror), file=sys.stderr)

for line in pconv.convert():
out.write(line + '\n')
out.close()

0 comments on commit abe0f26

Please sign in to comment.