diff --git a/src/sinol_make/commands/doc/__init__.py b/src/sinol_make/commands/doc/__init__.py index 28b1f765..87b604a4 100644 --- a/src/sinol_make/commands/doc/__init__.py +++ b/src/sinol_make/commands/doc/__init__.py @@ -17,8 +17,8 @@ class Command(BaseCommand): def get_name(self): return "doc" - def compile_file(self, file_path): - print(util.info(f'Compiling {os.path.basename(file_path)}...')) + def compile_file_latex_div(self, file_path): + print(util.info(f'Compiling {os.path.basename(file_path)} (latex to dvi)...')) os.chdir(os.path.dirname(file_path)) subprocess.run(['latex', file_path]) dvi_file = os.path.splitext(file_path)[0] + '.dvi' @@ -34,13 +34,27 @@ def compile_file(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(util.info(f'Compiling {os.path.basename(file_path)} (pdflatex)...')) + os.chdir(os.path.dirname(file_path)) + subprocess.run(['pdflatex', 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): + print(util.error('Compilation failed.')) + return False + return True + def make_file(self, file_path): """ Compile the file two times to get the references right. """ - if not self.compile_file(file_path): - return False - return self.compile_file(file_path) + if self.compilation_method == 'pdflatex': + return self.compile_pdf_latex(file_path) + else: + if not self.compile_file_latex_div(file_path): + return False + return self.compile_file_latex_div(file_path) def move_logs(self): output_dir = paths.get_cache_path('doc_logs') @@ -56,11 +70,28 @@ 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'], + help='Compiler used to compile documents (default: auto)', default='auto') parser.add_argument('files', type=str, nargs='*', help='files to compile') def run(self, args: argparse.Namespace): util.exit_if_not_package() + if not hasattr(args, 'latex_compiler'): + args.latex_compiler = 'auto' + + if args.latex_compiler == 'pdflatex': + self.compilation_method = 'pdflatex' + elif args.latex_compiler == 'latex_dvi': + self.compilation_method = 'latex_dvi' + elif args.latex_compiler == 'auto': + self.compilation_method = 'pdflatex' + for extension in ['ps', 'eps']: + if glob.glob(os.path.join(os.getcwd(), 'doc', f'*.{extension}')) != []: + self.compilation_method = 'latex_dvi' + else: + util.exit_with_error("Unrecognized latex compiler") + if args.files == []: self.files = glob.glob(os.path.join(os.getcwd(), 'doc', '*.tex')) else: diff --git a/tests/commands/doc/test_unit.py b/tests/commands/doc/test_unit.py index d4f4545c..aae11708 100644 --- a/tests/commands/doc/test_unit.py +++ b/tests/commands/doc/test_unit.py @@ -7,6 +7,11 @@ @pytest.mark.parametrize("create_package", [util.get_doc_package_path()], indirect=True) -def test_compile_file(create_package): +def test_compile_pdf_latex(create_package): command = Command() - assert command.compile_file(os.path.abspath(os.path.join(os.getcwd(), "doc/doczad.tex"))) is True + assert command.compile_pdf_latex(os.path.abspath(os.path.join(os.getcwd(), "doc/doczad.tex"))) is True + +@pytest.mark.parametrize("create_package", [util.get_doc_package_path()], indirect=True) +def test_compile_file_latex_div(create_package): + command = Command() + assert command.compile_file_latex_div(os.path.abspath(os.path.join(os.getcwd(), "doc/doczad.tex"))) is True