pytex/default_formatters/docstrip_formatter.py

31 lines
1.2 KiB
Python

from pathlib import Path
from typing import Dict, Optional, List
import subprocess
from PyTeX.formatter import Formatter
from PyTeX.utils import ensure_file_integrity
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)
result = subprocess.Popen(['pdflatex', self.name + '.dtx'], cwd=str(input_path.parent),
stdout=subprocess.DEVNULL,
stderr=subprocess.DEVNULL
)
if not result:
pass
sty = input_path.with_suffix('.sty').read_text()
output_dir.mkdir(parents=True, exist_ok=True)
(output_dir / self.filename).write_text(sty)
return [self.filename]