add docstrip formatter

This commit is contained in:
Maximilian Keßler 2022-02-18 15:56:57 +01:00
parent 55f1af8bb8
commit ea744304b0

View file

@ -0,0 +1,53 @@
import tempfile
from .enums import TeXType
from .formatting_config import FormattingConfig
from .pytex_formatter import PyTeXFormatter
from pathlib import Path
import subprocess
import shutil
from typing import List, Tuple
class DocStripFormatter(PyTeXFormatter):
def output_files(self) -> List[str]:
if self.config.tex_out_type == TeXType.TeXClass:
return self.name + '.cls'
elif self.config.tex_out_type == TeXType.TeXClass:
return self.name + '.sty'
else:
raise NotImplementedError
def format(self, build_dir: Path, overwrite: bool = False) -> List[Tuple[str, FormattingConfig]]:
tmp_dir: Path = Path(tempfile.mkdtemp())
shutil.copy(
self.input_file,
tmp_dir
)
if self.input_file.with_suffix('.ins').exists():
result = subprocess.run(
['tex', self.input_file.with_suffix('.ins').name],
cwd=tmp_dir,
stderr=subprocess.STDOUT
)
if not result.returncode == 0:
raise NotImplementedError
else:
result = subprocess.run(
['pdflatex', self.input_file.name],
cwd=tmp_dir,
stderr=subprocess.STDOUT
)
if not result.returncode == 0:
raise NotImplementedError
for file in self.output_files():
outfile = tmp_dir / file
if not outfile.exists():
raise NotImplementedError
shutil.copy(outfile, build_dir)
shutil.rmtree(tmp_dir)
return [] # No future config
def dependencies(self) -> List[str]:
return [] # TODO