Skip to content

Commit

Permalink
Merge pull request #245 from jwgcarlson/issue-183-multiple-pdfs
Browse files Browse the repository at this point in the history
Allow for multiple LaTeX sources and/or PDF images.
  • Loading branch information
ericholscher committed Sep 27, 2012
2 parents 26ccc28 + c694957 commit 31bd5f9
Showing 1 changed file with 29 additions and 21 deletions.
50 changes: 29 additions & 21 deletions readthedocs/doc_builder/backends/sphinx_pdf.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
from glob import glob
import re
import os
import logging

Expand All @@ -10,9 +9,6 @@
from core.utils import copy_file_to_app_servers


latex_re = re.compile('the LaTeX files are in (.*)\.')
pdf_re = re.compile('Output written on (.+) \(')

log = logging.getLogger(__name__)

class Builder(BaseBuilder):
Expand All @@ -32,27 +28,39 @@ def build(self, **kwargs):

if latex_results[0] == 0:
os.chdir('_build/latex')
tex_globs = glob('*.tex')
if tex_globs:
tex_file = tex_globs[0]
pdf_results = run('pdflatex -interaction=nonstopmode %s' % tex_file)
pdf_match = pdf_re.search(pdf_results[1])
if pdf_match:
if project.whitelisted:
# For whitelisted projects, read LaTeX sources from conf.py
conf_py_file = project.conf_file(self.version.slug)
conf = {}
execfile(conf_py_file, conf, conf)
tex_files = [d[1] for d in conf.get('latex_documents', [])]
else:
# Otherwise treat all .tex files as sources
tex_files = glob('*.tex')

# Run LaTeX -> PDF conversions
pdflatex_cmds = ['pdflatex -interaction=nonstopmode %s' % tex_file
for tex_file in tex_files]
pdf_results = run(*pdflatex_cmds)

if pdf_results[0] == 0:
for tex_file in tex_files:
to_path = os.path.join(settings.MEDIA_ROOT,
'pdf',
project.slug,
self.version.slug)
from_globs = glob(os.path.join(os.getcwd(), "*.pdf"))
if from_globs:
from_file = from_globs[0]
to_file = os.path.join(to_path, '%s.pdf' % project.slug)
if getattr(settings, "MULTIPLE_APP_SERVERS", None):
copy_file_to_app_servers(from_file, to_file)
else:
if not os.path.exists(to_path):
os.makedirs(to_path)
run('mv -f %s %s' % (from_file, to_file))
else:
to_file = os.path.join(to_path, '%s.pdf' % project.slug)
# pdflatex names its output predictably: foo.tex -> foo.pdf
pdf_filename = os.path.splitext(tex_file)[0] + '.pdf'
from_file = os.path.join(os.getcwd(), pdf_filename)
if getattr(settings, "MULTIPLE_APP_SERVERS", None):
copy_file_to_app_servers(from_file, to_file)
else:
if not os.path.exists(to_path):
os.makedirs(to_path)
run('mv -f %s %s' % (from_file, to_file))

if latex_results[0] != 0 or pdf_results[0] != 0:
log.warning("PDF Building failed. Moving on.")
return (latex_results, pdf_results)

Expand Down

0 comments on commit 31bd5f9

Please sign in to comment.