1- #!/usr/bin/env python
1+ #!/usr/bin/env python2
22# -*- coding: utf-8 -*-
33
44'''
4747typeset with a different package.
4848
4949
50- Copyright (c) 2013, Geoffrey M. Poore
50+ Copyright (c) 2013-2014 , Geoffrey M. Poore
5151All rights reserved.
5252Licensed under the BSD 3-Clause License:
5353 http://www.opensource.org/licenses/BSD-3-Clause
7272from collections import defaultdict
7373from re import match , sub , search
7474import textwrap
75+ import codecs
7576
7677
7778# Script parameters
7879# Version
79- version = 'v0.12 '
80+ version = 'v0.13-beta '
8081
8182
8283
@@ -680,10 +681,10 @@ def replace_print_env(name, arglist, linenum,
680681 help = 'line of commands to add to output preamble' )
681682parser .add_argument ('--graphicspath' , default = False , action = 'store_true' ,
682683 help = r'Add the outputdir to the graphics path, by modifying an existing \graphicspath command or adding one.' )
684+ parser .add_argument ('-o' , '--output' , default = None ,
685+ help = 'output file' )
683686parser .add_argument ('TEXNAME' ,
684687 help = 'LaTeX file' )
685- parser .add_argument ('OUTFILE' , nargs = '?' , default = None ,
686- help = 'output file; by default, <filename>.<ext> is converted into depythontex_<filename>.<ext>' )
687688args = parser .parse_args ()
688689
689690# Process argv
@@ -717,8 +718,9 @@ def replace_print_env(name, arglist, linenum,
717718
718719
719720# Let the user know things have started
720- print ('This is DePythonTeX {0}' .format (version ))
721- sys .stdout .flush ()
721+ if args .output is not None :
722+ print ('This is DePythonTeX {0}' .format (version ))
723+ sys .stdout .flush ()
722724
723725
724726
@@ -738,17 +740,14 @@ def replace_print_env(name, arglist, linenum,
738740 print (' Could not locate file "' + texfile_name + '"' )
739741 sys .exit (1 )
740742# Make sure we have a valid outfile
741- if args .OUTFILE is None :
742- p , f_name = os .path .split (texfile_name )
743- outfile_name = os .path .join (p , 'depythontex_' + f_name )
744- else :
745- outfile_name = os .path .expanduser (os .path .normcase (args .OUTFILE ))
746- if not args .overwrite and os .path .isfile (outfile_name ):
747- print ('* DePythonTeX warning:' )
748- print (' Output file "' + outfile_name + '" already exists' )
749- ans = input (' Do you want to overwrite this file? [y,n]\n ' )
750- if ans != 'y' :
751- sys .exit (1 )
743+ if args .output is not None :
744+ outfile_name = os .path .expanduser (os .path .normcase (args .output ))
745+ if not args .overwrite and os .path .isfile (outfile_name ):
746+ print ('* DePythonTeX warning:' )
747+ print (' Output file "' + outfile_name + '" already exists' )
748+ ans = input (' Do you want to overwrite this file? [y,n]\n ' )
749+ if ans != 'y' :
750+ sys .exit (1 )
752751# Make sure the .depytx file exists
753752depytxfile_name = texfile_name .rsplit ('.' )[0 ] + '.depytx'
754753if not os .path .isfile (depytxfile_name ):
@@ -794,7 +793,8 @@ def replace_print_env(name, arglist, linenum,
794793# Go ahead and open the outfile, even though we don't need it until the end
795794# This lets us change working directories for convenience without worrying
796795# about having to modify the outfile path
797- outfile = open (outfile_name , 'w' , encoding = encoding )
796+ if args .output is not None :
797+ outfile = open (outfile_name , 'w' , encoding = encoding )
798798
799799
800800
@@ -1362,11 +1362,15 @@ def replace_print_env(name, arglist, linenum,
13621362 if startline == n :
13631363 if bool (search (r'\\usepackage(?:\[.*?\]){0,1}\{pythontex\}' , line )):
13641364 texout [n ] = sub (r'\\usepackage(?:\[.*?\]){0,1}\{pythontex\}' , '' , line )
1365+ if texout [n ].isspace ():
1366+ texout [n ] = ''
13651367 break
13661368 else :
13671369 content = '' .join (texout [startline :n + 1 ])
13681370 if bool (search (r'(?s)\\usepackage(?:\[.*?\]\s*){0,1}\{pythontex\}' , content )):
13691371 replacement = sub (r'(?s)\\usepackage(?:\[.*?\]\s*){0,1}\{pythontex\}' , '' , content )
1372+ if replacement .isspace ():
1373+ replacement = ''
13701374 texout [startline ] = replacement
13711375 for l in range (startline + 1 , n + 1 ):
13721376 texout [l ] = ''
@@ -1401,6 +1405,16 @@ def replace_print_env(name, arglist, linenum,
14011405
14021406
14031407# Write output
1404- for line in texout :
1405- outfile .write (line )
1406- outfile .close ()
1408+ if args .output is not None :
1409+ for line in texout :
1410+ outfile .write (line )
1411+ outfile .close ()
1412+ else :
1413+ if sys .version_info [0 ] == 2 :
1414+ sys .stdout = codecs .getwriter (encoding )(sys .stdout , 'strict' )
1415+ sys .stderr = codecs .getwriter (encoding )(sys .stderr , 'strict' )
1416+ else :
1417+ sys .stdout = codecs .getwriter (encoding )(sys .stdout .buffer , 'strict' )
1418+ sys .stderr = codecs .getwriter (encoding )(sys .stderr .buffer , 'strict' )
1419+ for line in texout :
1420+ sys .stdout .write (line )
0 commit comments