from pathlib import Path from typing import Dict, Optional, List import subprocess from PyTeX.formatter import Formatter from PyTeX.utils import ensure_file_integrity from PyTeX.errors import FileNotGeneratedFromDTXFileError, LatexMKError class DocstripFormatter(Formatter): def __init__(self, name: str): self.name = name self.filename = self.name + '.sty' Formatter.__init__(self) def expected_file_name(self) -> str: return self.filename def format_file(self, input_path: Path, output_dir: Path, relative_name: Optional[str] = None, last_build_info: Optional[List[Dict]] = None) -> List[str]: ensure_file_integrity(output_dir / self.filename, str(Path(relative_name).parent / self.filename), last_build_info) try: result = subprocess.run(['latexmk', '-gg', '-output-directory=/tmp'], cwd=str(input_path.parent), stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL ) except subprocess.CalledProcessError: raise LatexMKError(self.filename) if not result: pass sty_file = (Path('/tmp') / input_path.with_suffix('.sty').name) if sty_file.exists(): sty = sty_file.read_text() else: raise FileNotGeneratedFromDTXFileError(self.filename, 'style') output_dir.mkdir(parents=True, exist_ok=True) (output_dir / self.filename).write_text(sty) return [self.filename]