Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions example_package/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,13 @@ sinol_task_id: __ID__
# See README.md for more information.
sinol_contest_type: oi

# You can configure how sinol-make will compile the LaTeX in `doc/`. By default,
# it will attempt to choose an option that makes sense based on the presence
# of *.ps/*.eps figures. You can choose between `pdflatex`, `lualatex` and
# `latex_dvi`.
# This option can be overridden by passing `--latex_compiler` to `sinol-make doc`.
sinol_latex_compiler: auto

# You can specify which tests are static (handwritten). This allows sinol-make to differentiate between
# old and handwritten tests. If this key is not present old tests won't be removed.
# This key is optional and should be a list of tests.
Expand Down
30 changes: 20 additions & 10 deletions src/sinol_make/commands/doc/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import subprocess

from sinol_make import util
from sinol_make.helpers import paths
from sinol_make.helpers import package_util, paths
from sinol_make.interfaces.BaseCommand import BaseCommand


Expand Down Expand Up @@ -37,11 +37,11 @@ def compile_file_latex_div(self, file_path):
print(util.info(f'Compilation successful for file {os.path.basename(file_path)}.'))
return True

def compile_pdf_latex(self, file_path):
print(f'Compiling {os.path.basename(file_path)} (pdflatex)...')
def compile_pdf_latex(self, file_path, compiler='pdflatex'):
print(f'Compiling {os.path.basename(file_path)} ({compiler})...')
os.chdir(os.path.dirname(file_path))
for _ in range(3):
subprocess.run(['pdflatex', file_path])
subprocess.run([compiler, file_path])
pdf_file = os.path.splitext(file_path)[0] + '.pdf'
pdf_file_path = os.path.join(os.path.dirname(file_path), pdf_file)
if not os.path.exists(pdf_file_path):
Expand All @@ -53,8 +53,8 @@ def make_file(self, file_path):
"""
Compile the file two times to get the references right.
"""
if self.compilation_method == 'pdflatex':
return self.compile_pdf_latex(file_path)
if self.compilation_method in ('pdflatex', 'lualatex'):
return self.compile_pdf_latex(file_path, self.compilation_method)
else:
if not self.compile_file_latex_div(file_path):
return False
Expand All @@ -73,22 +73,32 @@ def configure_subparser(self, subparser: argparse.ArgumentParser):
help='Compile latex files to pdf',
description='Compiles latex files to pdf. By default compiles all files in the `doc` directory.\n'
'You can also specify files to compile.')
parser.add_argument('--latex-compiler', dest='latex_compiler', choices=['auto', 'pdflatex', 'latex_dvi'],
parser.add_argument('--latex-compiler', dest='latex_compiler', choices=['auto', 'pdflatex', 'latex_dvi', 'lualatex'],
help='Compiler used to compile documents. Available options:\n'
' auto - uses the compiler based on the image types (default option).\n'
' auto - uses the compiler based on the image types (default option, if not configured in config.yml).\n'
' pdflatex - uses pdflatex. Works with .png and .jpg images.\n'
' latex_dvi - uses latex and dvipdf. Works with .ps and .eps images.', default='auto')
' lualatex - uses lualatex. Like pdflatex, but supports the graph drawing library of TikZ.\n'
' latex_dvi - uses latex and dvipdf. Works with .ps and .eps images.', default=argparse.SUPPRESS)
parser.add_argument('files', type=str, nargs='*', help='files to compile')
return parser

def run(self, args: argparse.Namespace):
args = util.init_package_command(args)

# Note: when other commands call DocCommand as a subroutine, they provide
# their own argparse.Namespace instead of going through the argparse
# configuration in configure_subparser. To match their behavior,
# we configure argparse to omit the latex_compiler attribute entirely
# when it is not provided by the user, instead of using the default
# behavior of defaulting to None.
if not hasattr(args, 'latex_compiler'):
args.latex_compiler = 'auto'
config = package_util.get_config()
args.latex_compiler = config.get('sinol_latex_compiler', 'auto')

if args.latex_compiler == 'pdflatex':
self.compilation_method = 'pdflatex'
elif args.latex_compiler == 'lualatex':
self.compilation_method = 'lualatex'
elif args.latex_compiler == 'latex_dvi':
self.compilation_method = 'latex_dvi'
elif args.latex_compiler == 'auto':
Expand Down
1 change: 1 addition & 0 deletions src/sinol_make/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ def save_config(config):
"title_en",
"sinol_task_id",
"sinol_contest_type",
"sinol_latex_compiler",
"sinol_static_tests",
"sinol_undocumented_time_tool",
"sinol_undocumented_test_limits",
Expand Down
40 changes: 40 additions & 0 deletions tests/commands/doc/test_integration.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,3 +95,43 @@ def test_compilation_mode_2(capsys, create_package):
expected="latex to dvi",
not_expected="pdflatex"
)


@pytest.mark.parametrize("create_package", [util.get_doc_package_path()], indirect=True)
def test_compilation_mode_3(capsys, create_package):
"""
Test `doc` with compilation mode directly specified.
"""
run_doc(
capsys=capsys,
command_args=["doc", "doc/doczad.tex", "--latex-compiler", "lualatex"],
expected="lualatex",
not_expected="pdflatex"
)


@pytest.mark.parametrize("create_package", [util.get_luadoc_package_path()], indirect=True)
def test_compilation_mode_config(capsys, create_package):
"""
Test `doc` with compilation mode specified in the configuration file.
"""
run_doc(
capsys=capsys,
command_args=["doc"],
expected="lualatex",
not_expected="pdflatex"
)


@pytest.mark.parametrize("create_package", [util.get_luadoc_package_path()], indirect=True)
def test_compilation_mode_config_override(capsys, create_package):
"""
Test `doc` with compilation mode specified in the configuration file, and
then overridden on the command-line.
"""
run_doc(
capsys=capsys,
command_args=["doc", "--latex-compiler", "pdflatex"],
expected="pdflatex",
not_expected="lualatex"
)
5 changes: 5 additions & 0 deletions tests/packages/luadoc/config.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
title: Package for testing `doc` command
time_limit: 1000
memory_limit: 1024
sinol_task_id: luadoc
sinol_latex_compiler: lualatex
4 changes: 4 additions & 0 deletions tests/packages/luadoc/doc/doczad.tex
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
\documentclass{article}
\begin{document}
Hello World!
\end{document}
Empty file.
Empty file.
7 changes: 7 additions & 0 deletions tests/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,13 @@ def get_ps_doc_package_path():
return os.path.join(os.path.dirname(__file__), "packages", "ps_doc")


def get_luadoc_package_path():
"""
Get path to package for testing `doc` command (version that requests lualatex in config.yml) (/test/packages/luadoc)
"""
return os.path.join(os.path.dirname(__file__), "packages", "luadoc")


def get_long_name_package_path():
"""
Get path to package with long name (/test/packages/long_package_name)
Expand Down