Skip to content

Commit 1c444e2

Browse files
committed
Use latexmk command to build PDF files
New versions of Sphinx use `latexmk` to build the PDF files. This command uses a file called `latexmkrc` (or `latexmkjarc` for Japanese) which contains all the proper commands that needs to be ran depending on different Sphinx configurations. `latexmk` will take care by itself on the amount of phases that need to be ran without us worrying about it. Currently, this is not considering LATEXMKOPTS and XINDYOPTS environment variables configured by Sphinx. This feature is implemented under a Feature flag so we can test it easily without breaking other working projects. References: - #1556 - #4454 - #5405 - toppers/tecs-docs#7 - https://github.com/sphinx-doc/sphinx/blob/master/sphinx/texinputs/Makefile_t - https://www.sphinx-doc.org/en/master/usage/builders/index.html#sphinx.builders.latex.LaTeXBuilder
1 parent b44dab4 commit 1c444e2

File tree

2 files changed

+55
-0
lines changed

2 files changed

+55
-0
lines changed

readthedocs/doc_builder/backends/sphinx.py

+53
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,14 @@
66
.. _Sphinx: http://www.sphinx-doc.org/
77
"""
88
import codecs
9+
import itertools
910
import logging
1011
import os
1112
import shutil
1213
import sys
1314
import zipfile
1415
from glob import glob
16+
from pathlib import Path
1517

1618
from django.conf import settings
1719
from django.template import loader as template_loader
@@ -395,6 +397,57 @@ def build(self):
395397
raise BuildEnvironmentError('No TeX files were found')
396398

397399
# Run LaTeX -> PDF conversions
400+
if self.project.has_feature(Feature.USE_PDF_LATEXMK):
401+
return self._build_latexmk(cwd, latex_cwd)
402+
403+
return self._build_pdflatex(tex_files, latex_cwd)
404+
405+
def _build_latexmk(self, cwd, latex_cwd):
406+
# These steps are copied from the Makefile generated by Sphinx >= 1.6
407+
# https://github.com/sphinx-doc/sphinx/blob/master/sphinx/texinputs/Makefile_t
408+
latex_path = Path(latex_cwd)
409+
images = []
410+
for extension in ('png', 'gif', 'jpg', 'jpeg'):
411+
images.extend(latex_path.glob(f'*.{extension}'))
412+
413+
# FIXME: instead of checking by language here, what we want to check if
414+
# ``latex_engine`` is ``platex``
415+
pdfs = []
416+
if self.project.language == 'ja':
417+
pdfs = latex_path.glob('*.pdf')
418+
419+
for image in itertools.chain(images, pdfs):
420+
self.run(
421+
'extractbb',
422+
image.name,
423+
cwd=latex_cwd,
424+
record=False,
425+
)
426+
427+
rcfile = 'latexmkrc'
428+
if self.project.language == 'ja':
429+
rcfile = 'latexmkjarc'
430+
431+
cmd_ret = self.run(
432+
'latexmk',
433+
'-r',
434+
rcfile,
435+
436+
# FIXME: check for platex here as well
437+
'-pdfdvi' if self.project.language == 'ja' else '-pdf',
438+
439+
'-dvi-',
440+
'-ps-',
441+
f'-jobname={self.project.slug}',
442+
warn_only=True,
443+
cwd=latex_cwd,
444+
)
445+
446+
self.pdf_file_name = f'{self.project.slug}.pdf'
447+
448+
return True # :)
449+
450+
def _build_pdflatex(self, tex_files, latex_cwd):
398451
pdflatex_cmds = [
399452
['pdflatex', '-interaction=nonstopmode', tex_file]
400453
for tex_file in tex_files

readthedocs/projects/models.py

+2
Original file line numberDiff line numberDiff line change
@@ -1314,10 +1314,12 @@ def add_features(sender, **kwargs):
13141314
DONT_SHALLOW_CLONE = 'dont_shallow_clone'
13151315
USE_TESTING_BUILD_IMAGE = 'use_testing_build_image'
13161316
SHARE_SPHINX_DOCTREE = 'share_sphinx_doctree'
1317+
USE_PDF_LATEXMK = 'use_pdf_latexmk'
13171318

13181319
FEATURES = (
13191320
(USE_SPHINX_LATEST, _('Use latest version of Sphinx')),
13201321
(USE_SETUPTOOLS_LATEST, _('Use latest version of setuptools')),
1322+
(USE_PDF_LATEXMK, _('Use latexmk to build the PDF')),
13211323
(ALLOW_DEPRECATED_WEBHOOKS, _('Allow deprecated webhook views')),
13221324
(PIP_ALWAYS_UPGRADE, _('Always run pip install --upgrade')),
13231325
(SKIP_SUBMODULES, _('Skip git submodule checkout')),

0 commit comments

Comments
 (0)