Skip to content

Commit

Permalink
Add option to pass utf8 parameter to latexdiff.
Browse files Browse the repository at this point in the history
  • Loading branch information
jtkiley committed Jul 9, 2014
1 parent 1a85357 commit dc97762
Showing 1 changed file with 50 additions and 39 deletions.
89 changes: 50 additions & 39 deletions rcs_latexdiff/rcs_latexdiff.py
Expand Up @@ -61,64 +61,65 @@ def get_file(rcs, root_path, relative_path, commit, filename):
return file_content


def exec_diff(old_filename, new_filename, diff_filename):
def exec_diff(old_filename, new_filename, diff_filename, latexdiff_args=""):
""" Exec Latexdiff
:param old_filename:
:param new_filename:
:param diff_filename:
:param latexdiff_args:
"""
run_command("latexdiff %s %s > %s" % (old_filename, new_filename, diff_filename))
run_command("latexdiff %s %s %s > %s" % (latexdiff_args, old_filename, new_filename, diff_filename))

def exec_pdflatex(tex_filename, src_path):
"""
Exect pdflatex
:param tex_filename: File name of the .tex to compile.
:param src_path: Path from which pdflatex will be called (this should
make most figures work).
:return: PDF file name.
"""
tex_path = os.path.dirname(tex_filename)

tex_path = os.path.dirname(tex_filename)

aux_filename = os.path.splitext(tex_filename)[0] + ".aux"
pdf_filename = os.path.splitext(tex_filename)[0] + ".pdf"
# We enter the folder of the source to get proper relative paths to

# We enter the folder of the source to get proper relative paths to
# figures
starting_dir = os.getcwd()
os.chdir(src_path)

def single_run():
run_command("pdflatex -interaction nonstopmode -output-directory {} {}".format(tex_path, tex_filename))

# Run pdflatex and bibtex a bunch of times
try:
single_run()
single_run()

if os.path.isfile(aux_filename):
run_command("bibtex %s" % tex_filename)
run_command("bibtex %s" % aux_filename)

single_run()
single_run()

logger.info("Ran pdflatex and bibtex.")
except:
logger.debug("Problem building pdf file.")

# Return to original directory
os.chdir(starting_dir)
os.chdir(starting_dir)

return pdf_filename

def open_pdf(pdf_filename):
"""
Opens the given file in the default PDF viewing program.
:param str pdf_filename: PDF file to open.
"""

Expand All @@ -132,13 +133,13 @@ def open_pdf(pdf_filename):
elif os.name == 'posix':
os_str = "Linux"
with open('/dev/null') as output:
# When my pdf viewer closes, it spits out some errors or something I
# When my pdf viewer closes, it spits out some errors or something I
# don't care about, so make stderr not inherit from this thread.
subprocess.Popen(('xdg-open', pdf_filename),stdout=output,stderr=subprocess.STDOUT)

logger.info("Opened in default {} PDF viewer: {}".format(os_str, pdf_filename))

def make_diff(rcs, old_commit, new_commit, root_path, relative_path, src_filename, dst_filename):
def make_diff(rcs, old_commit, new_commit, root_path, relative_path, src_filename, dst_filename, latexdiff_args):
# TODO docs path root and relative
""" Make a diff for a name between two commits
Expand All @@ -149,6 +150,7 @@ def make_diff(rcs, old_commit, new_commit, root_path, relative_path, src_filenam
:param path: path of the repository
:param src_filename: name of the file
:param dst_filename: name of the output file
:param latexdiff_args: args to pass through to latexdiff
:return: destination file and temporary files
"""
Expand All @@ -175,50 +177,54 @@ def make_diff(rcs, old_commit, new_commit, root_path, relative_path, src_filenam

# Exec diff
logger.info("Execute latexdiff")
exec_diff(old_filename, new_filename, dst_filename)
exec_diff(old_filename, new_filename, dst_filename, latexdiff_args)

return dst_filename, old_filename, new_filename


def parse_arguments():

description = """\
A tool to generate LaTeX Diff between two Revision
Control System commits of a file, compile the resulting .tex, and
A tool to generate LaTeX Diff between two Revision
Control System commits of a file, compile the resulting .tex, and
display it."""

epilog = """\
EXAMPLE USAGE:
rcs-latexdiff document.tex HEAD
Display the latexdiff'd PDF of the current working version of
document.tex compared to the HEAD of the Git repository.
rcs-latexdiff document.tex master adivsor
Display the latexdiff'd PDF of the changes in the "advisor" branch
compared to the master branch.
rcs-latexdiff --no-open -o /home/myself/thediff.tex git/repo/doc.tex HEAD^^ HEAD
Create (but don't open) the difference between the HEAD and the
grandparent of HEAD as /home/myself/thediff.pdf using the git
Create (but don't open) the difference between the HEAD and the
grandparent of HEAD as /home/myself/thediff.pdf using the git
repo git/repo
"""

parser = argparse.ArgumentParser(description=description, epilog=epilog, formatter_class=argparse.RawTextHelpFormatter)

parser.add_argument('--dirty', action='store_false',
dest='clean',
help='Don\'t clean up files generated along the way (.aux, .log, etc).')

parser.add_argument('--no-pdf', action='store_false',
dest='makepdf',
help='Don\'t try to run pdflatex on the diff file.')

parser.add_argument('--no-open', action='store_false',
dest='openpdf',
help='Don\'t try to open the created pdf file.')

parser.add_argument('--utf8', action='store_true',
dest='utf8',
help='Pass "--encoding=utf8" to latexdiff.')

parser.add_argument('-o', '--output', dest='output',
help='Name of the generated diff file. If not specified, '
'default output will be "diff.tex" in the path of '
Expand All @@ -236,7 +242,7 @@ def parse_arguments():

parser.add_argument('OLD', help='Old commit (SHA1 or branch name).')

parser.add_argument('NEW',
parser.add_argument('NEW',
help='New commit (SHA1 or branch name). If omitted, '
'will use the current working copy as NEW.',
nargs='?')
Expand Down Expand Up @@ -314,20 +320,25 @@ def main():
if not rcs.is_commit(root_path, commit) and commit is not None:
logger.info("Commit does not exist: %s" % (commit))
exit(1)

# Populate the default output file
if args.output is None:
dst_filename = os.path.join(root_path, relative_path, 'diff.tex')
else:
dst_filename = args.output

# Gather arguments to pass through to latexdiff
latexdiff_args = ''
if args.utf8:
latexdiff_args += '--encoding=utf8 '

# Make the diff
make_diff(rcs, args.OLD, args.NEW, root_path, relative_path, filename, dst_filename)
make_diff(rcs, args.OLD, args.NEW, root_path, relative_path, filename, dst_filename, latexdiff_args)

# Make the pdf
if args.makepdf:
pdf_filename = exec_pdflatex(dst_filename, os.path.join(root_path, relative_path))

# Open the pdf
if args.openpdf:
open_pdf(pdf_filename)
Expand All @@ -338,7 +349,7 @@ def main():
clean_glob = glob.glob(os.path.splitext(dst_filename)[0] + '.*')
keep_ext = 'pdf' if args.makepdf else 'tex'
clean_glob = [f for f in clean_glob if f[-3:] != keep_ext]

clean_output_files(clean_glob)


Expand Down

0 comments on commit dc97762

Please sign in to comment.