Skip to content

Commit 02d2888

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 874c920 commit 02d2888

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
@@ -392,6 +394,57 @@ def build(self):
392394
raise BuildEnvironmentError('No TeX files were found')
393395

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

readthedocs/projects/models.py

+2
Original file line numberDiff line numberDiff line change
@@ -1313,10 +1313,12 @@ def add_features(sender, **kwargs):
13131313
API_LARGE_DATA = 'api_large_data'
13141314
DONT_SHALLOW_CLONE = 'dont_shallow_clone'
13151315
USE_TESTING_BUILD_IMAGE = 'use_testing_build_image'
1316+
USE_PDF_LATEXMK = 'use_pdf_latexmk'
13161317

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

0 commit comments

Comments
 (0)