Skip to content

Commit 1e90bb1

Browse files
committed
v0.13-beta; contextual information such as page dimensions can be passed to Python; fixes #32 and fixes #33
1 parent b397981 commit 1e90bb1

23 files changed

Lines changed: 1344 additions & 942 deletions

NEWS.rst

Lines changed: 383 additions & 0 deletions
Large diffs are not rendered by default.

README.rst

Lines changed: 41 additions & 328 deletions
Large diffs are not rendered by default.

pythontex/depythontex.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,14 @@
22
# -*- coding: utf-8 -*-
33

44
'''
5-
This is the PythonTeX wrapper script. It automatically detects the version
5+
This is the depythontex wrapper script. It automatically detects the version
66
of Python, and then imports the correct code from depythontex2.py or
7-
depythontex3.py.
7+
depythontex3.py. It is intended for use with the default Python installation
8+
on your system. If you wish to use a different version of Python, you could
9+
launch depythontex2.py or depythontex3.py directly. The version of Python
10+
does not matter for depythontex, since no code is executed.
811
9-
Copyright (c) 2013, Geoffrey M. Poore
12+
Copyright (c) 2013-2014, Geoffrey M. Poore
1013
All rights reserved.
1114
Licensed under the BSD 3-Clause License:
1215
http://www.opensource.org/licenses/BSD-3-Clause

pythontex/depythontex2.py

Lines changed: 36 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#!/usr/bin/env python
1+
#!/usr/bin/env python2
22
# -*- coding: utf-8 -*-
33

44
'''
@@ -47,7 +47,7 @@
4747
typeset with a different package.
4848
4949
50-
Copyright (c) 2013, Geoffrey M. Poore
50+
Copyright (c) 2013-2014, Geoffrey M. Poore
5151
All rights reserved.
5252
Licensed under the BSD 3-Clause License:
5353
http://www.opensource.org/licenses/BSD-3-Clause
@@ -72,11 +72,12 @@
7272
from collections import defaultdict
7373
from re import match, sub, search
7474
import 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')
681682
parser.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')
683686
parser.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>')
687688
args = 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
753752
depytxfile_name = texfile_name.rsplit('.')[0] + '.depytx'
754753
if 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)

pythontex/depythontex3.py

Lines changed: 36 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#!/usr/bin/env python
1+
#!/usr/bin/env python3
22
# -*- coding: utf-8 -*-
33

44
'''
@@ -47,7 +47,7 @@
4747
typeset with a different package.
4848
4949
50-
Copyright (c) 2013, Geoffrey M. Poore
50+
Copyright (c) 2013-2014, Geoffrey M. Poore
5151
All rights reserved.
5252
Licensed under the BSD 3-Clause License:
5353
http://www.opensource.org/licenses/BSD-3-Clause
@@ -72,11 +72,12 @@
7272
from collections import defaultdict
7373
from re import match, sub, search
7474
import 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')
681682
parser.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')
683686
parser.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>')
687688
args = 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
753752
depytxfile_name = texfile_name.rsplit('.')[0] + '.depytx'
754753
if 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

Comments
 (0)