Skip to content

Commit

Permalink
Pdflatex compilation
Browse files Browse the repository at this point in the history
  • Loading branch information
adespawn committed Feb 20, 2024
1 parent 31bb4b6 commit 822a9c6
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 7 deletions.
41 changes: 36 additions & 5 deletions src/sinol_make/commands/doc/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand All @@ -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')
Expand All @@ -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:
Expand Down
9 changes: 7 additions & 2 deletions tests/commands/doc/test_unit.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 822a9c6

Please sign in to comment.