diff --git a/PyTeX/format/docstrip_formatter.py b/PyTeX/format/docstrip_formatter.py new file mode 100644 index 0000000..511b5ae --- /dev/null +++ b/PyTeX/format/docstrip_formatter.py @@ -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