Skip to content

Commit

Permalink
v0.13-beta; contextual information such as page dimensions can be pas…
Browse files Browse the repository at this point in the history
…sed to Python; fixes #32 and fixes #33
  • Loading branch information
gpoore committed Feb 6, 2014
1 parent b397981 commit 1e90bb1
Show file tree
Hide file tree
Showing 23 changed files with 1,344 additions and 942 deletions.
383 changes: 383 additions & 0 deletions NEWS.rst

Large diffs are not rendered by default.

369 changes: 41 additions & 328 deletions README.rst

Large diffs are not rendered by default.

9 changes: 6 additions & 3 deletions pythontex/depythontex.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,14 @@
# -*- coding: utf-8 -*-

'''
This is the PythonTeX wrapper script. It automatically detects the version
This is the depythontex wrapper script. It automatically detects the version
of Python, and then imports the correct code from depythontex2.py or
depythontex3.py.
depythontex3.py. It is intended for use with the default Python installation
on your system. If you wish to use a different version of Python, you could
launch depythontex2.py or depythontex3.py directly. The version of Python
does not matter for depythontex, since no code is executed.
Copyright (c) 2013, Geoffrey M. Poore
Copyright (c) 2013-2014, Geoffrey M. Poore
All rights reserved.
Licensed under the BSD 3-Clause License:
http://www.opensource.org/licenses/BSD-3-Clause
Expand Down
58 changes: 36 additions & 22 deletions pythontex/depythontex2.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/usr/bin/env python
#!/usr/bin/env python2
# -*- coding: utf-8 -*-

'''
Expand Down Expand Up @@ -47,7 +47,7 @@
typeset with a different package.
Copyright (c) 2013, Geoffrey M. Poore
Copyright (c) 2013-2014, Geoffrey M. Poore
All rights reserved.
Licensed under the BSD 3-Clause License:
http://www.opensource.org/licenses/BSD-3-Clause
Expand All @@ -72,11 +72,12 @@
from collections import defaultdict
from re import match, sub, search
import textwrap
import codecs


# Script parameters
# Version
version = 'v0.12'
version = 'v0.13-beta'



Expand Down Expand Up @@ -680,10 +681,10 @@ def replace_print_env(name, arglist, linenum,
help='line of commands to add to output preamble')
parser.add_argument('--graphicspath', default=False, action='store_true',
help=r'Add the outputdir to the graphics path, by modifying an existing \graphicspath command or adding one.')
parser.add_argument('-o', '--output', default=None,
help='output file')
parser.add_argument('TEXNAME',
help='LaTeX file')
parser.add_argument('OUTFILE', nargs='?', default=None,
help='output file; by default, <filename>.<ext> is converted into depythontex_<filename>.<ext>')
args = parser.parse_args()

# Process argv
Expand Down Expand Up @@ -717,8 +718,9 @@ def replace_print_env(name, arglist, linenum,


# Let the user know things have started
print('This is DePythonTeX {0}'.format(version))
sys.stdout.flush()
if args.output is not None:
print('This is DePythonTeX {0}'.format(version))
sys.stdout.flush()



Expand All @@ -738,17 +740,14 @@ def replace_print_env(name, arglist, linenum,
print(' Could not locate file "' + texfile_name + '"')
sys.exit(1)
# Make sure we have a valid outfile
if args.OUTFILE is None:
p, f_name = os.path.split(texfile_name)
outfile_name = os.path.join(p, 'depythontex_' + f_name)
else:
outfile_name = os.path.expanduser(os.path.normcase(args.OUTFILE))
if not args.overwrite and os.path.isfile(outfile_name):
print('* DePythonTeX warning:')
print(' Output file "' + outfile_name + '" already exists')
ans = input(' Do you want to overwrite this file? [y,n]\n ')
if ans != 'y':
sys.exit(1)
if args.output is not None:
outfile_name = os.path.expanduser(os.path.normcase(args.output))
if not args.overwrite and os.path.isfile(outfile_name):
print('* DePythonTeX warning:')
print(' Output file "' + outfile_name + '" already exists')
ans = input(' Do you want to overwrite this file? [y,n]\n ')
if ans != 'y':
sys.exit(1)
# Make sure the .depytx file exists
depytxfile_name = texfile_name.rsplit('.')[0] + '.depytx'
if not os.path.isfile(depytxfile_name):
Expand Down Expand Up @@ -794,7 +793,8 @@ def replace_print_env(name, arglist, linenum,
# Go ahead and open the outfile, even though we don't need it until the end
# This lets us change working directories for convenience without worrying
# about having to modify the outfile path
outfile = open(outfile_name, 'w', encoding=encoding)
if args.output is not None:
outfile = open(outfile_name, 'w', encoding=encoding)



Expand Down Expand Up @@ -1362,11 +1362,15 @@ def replace_print_env(name, arglist, linenum,
if startline == n:
if bool(search(r'\\usepackage(?:\[.*?\]){0,1}\{pythontex\}', line)):
texout[n] = sub(r'\\usepackage(?:\[.*?\]){0,1}\{pythontex\}', '', line)
if texout[n].isspace():
texout[n] = ''
break
else:
content = ''.join(texout[startline:n+1])
if bool(search(r'(?s)\\usepackage(?:\[.*?\]\s*){0,1}\{pythontex\}', content)):
replacement = sub(r'(?s)\\usepackage(?:\[.*?\]\s*){0,1}\{pythontex\}', '', content)
if replacement.isspace():
replacement = ''
texout[startline] = replacement
for l in range(startline+1, n+1):
texout[l] = ''
Expand Down Expand Up @@ -1401,6 +1405,16 @@ def replace_print_env(name, arglist, linenum,


# Write output
for line in texout:
outfile.write(line)
outfile.close()
if args.output is not None:
for line in texout:
outfile.write(line)
outfile.close()
else:
if sys.version_info[0] == 2:
sys.stdout = codecs.getwriter(encoding)(sys.stdout, 'strict')
sys.stderr = codecs.getwriter(encoding)(sys.stderr, 'strict')
else:
sys.stdout = codecs.getwriter(encoding)(sys.stdout.buffer, 'strict')
sys.stderr = codecs.getwriter(encoding)(sys.stderr.buffer, 'strict')
for line in texout:
sys.stdout.write(line)
58 changes: 36 additions & 22 deletions pythontex/depythontex3.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/usr/bin/env python
#!/usr/bin/env python3
# -*- coding: utf-8 -*-

'''
Expand Down Expand Up @@ -47,7 +47,7 @@
typeset with a different package.
Copyright (c) 2013, Geoffrey M. Poore
Copyright (c) 2013-2014, Geoffrey M. Poore
All rights reserved.
Licensed under the BSD 3-Clause License:
http://www.opensource.org/licenses/BSD-3-Clause
Expand All @@ -72,11 +72,12 @@
from collections import defaultdict
from re import match, sub, search
import textwrap
import codecs


# Script parameters
# Version
version = 'v0.12'
version = 'v0.13-beta'



Expand Down Expand Up @@ -680,10 +681,10 @@ def replace_print_env(name, arglist, linenum,
help='line of commands to add to output preamble')
parser.add_argument('--graphicspath', default=False, action='store_true',
help=r'Add the outputdir to the graphics path, by modifying an existing \graphicspath command or adding one.')
parser.add_argument('-o', '--output', default=None,
help='output file')
parser.add_argument('TEXNAME',
help='LaTeX file')
parser.add_argument('OUTFILE', nargs='?', default=None,
help='output file; by default, <filename>.<ext> is converted into depythontex_<filename>.<ext>')
args = parser.parse_args()

# Process argv
Expand Down Expand Up @@ -717,8 +718,9 @@ def replace_print_env(name, arglist, linenum,


# Let the user know things have started
print('This is DePythonTeX {0}'.format(version))
sys.stdout.flush()
if args.output is not None:
print('This is DePythonTeX {0}'.format(version))
sys.stdout.flush()



Expand All @@ -738,17 +740,14 @@ def replace_print_env(name, arglist, linenum,
print(' Could not locate file "' + texfile_name + '"')
sys.exit(1)
# Make sure we have a valid outfile
if args.OUTFILE is None:
p, f_name = os.path.split(texfile_name)
outfile_name = os.path.join(p, 'depythontex_' + f_name)
else:
outfile_name = os.path.expanduser(os.path.normcase(args.OUTFILE))
if not args.overwrite and os.path.isfile(outfile_name):
print('* DePythonTeX warning:')
print(' Output file "' + outfile_name + '" already exists')
ans = input(' Do you want to overwrite this file? [y,n]\n ')
if ans != 'y':
sys.exit(1)
if args.output is not None:
outfile_name = os.path.expanduser(os.path.normcase(args.output))
if not args.overwrite and os.path.isfile(outfile_name):
print('* DePythonTeX warning:')
print(' Output file "' + outfile_name + '" already exists')
ans = input(' Do you want to overwrite this file? [y,n]\n ')
if ans != 'y':
sys.exit(1)
# Make sure the .depytx file exists
depytxfile_name = texfile_name.rsplit('.')[0] + '.depytx'
if not os.path.isfile(depytxfile_name):
Expand Down Expand Up @@ -794,7 +793,8 @@ def replace_print_env(name, arglist, linenum,
# Go ahead and open the outfile, even though we don't need it until the end
# This lets us change working directories for convenience without worrying
# about having to modify the outfile path
outfile = open(outfile_name, 'w', encoding=encoding)
if args.output is not None:
outfile = open(outfile_name, 'w', encoding=encoding)



Expand Down Expand Up @@ -1362,11 +1362,15 @@ def replace_print_env(name, arglist, linenum,
if startline == n:
if bool(search(r'\\usepackage(?:\[.*?\]){0,1}\{pythontex\}', line)):
texout[n] = sub(r'\\usepackage(?:\[.*?\]){0,1}\{pythontex\}', '', line)
if texout[n].isspace():
texout[n] = ''
break
else:
content = ''.join(texout[startline:n+1])
if bool(search(r'(?s)\\usepackage(?:\[.*?\]\s*){0,1}\{pythontex\}', content)):
replacement = sub(r'(?s)\\usepackage(?:\[.*?\]\s*){0,1}\{pythontex\}', '', content)
if replacement.isspace():
replacement = ''
texout[startline] = replacement
for l in range(startline+1, n+1):
texout[l] = ''
Expand Down Expand Up @@ -1401,6 +1405,16 @@ def replace_print_env(name, arglist, linenum,


# Write output
for line in texout:
outfile.write(line)
outfile.close()
if args.output is not None:
for line in texout:
outfile.write(line)
outfile.close()
else:
if sys.version_info[0] == 2:
sys.stdout = codecs.getwriter(encoding)(sys.stdout, 'strict')
sys.stderr = codecs.getwriter(encoding)(sys.stderr, 'strict')
else:
sys.stdout = codecs.getwriter(encoding)(sys.stdout.buffer, 'strict')
sys.stderr = codecs.getwriter(encoding)(sys.stderr.buffer, 'strict')
for line in texout:
sys.stdout.write(line)
Loading

0 comments on commit 1e90bb1

Please sign in to comment.