From e661ceee64bd5070c40b448bbbbc6981585df978 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maximilian=20Ke=C3=9Fler?= Date: Mon, 24 Jan 2022 01:05:43 +0100 Subject: [PATCH] integrate docstrip source files --- __init__.py | 5 ++-- build/build.py | 4 +++ build/utils/pytex_file.py | 6 ++++- default_formatters/__init__.py | 4 ++- default_formatters/docstrip_formatter.py | 31 ++++++++++++++++++++++++ 5 files changed, 46 insertions(+), 4 deletions(-) create mode 100644 default_formatters/docstrip_formatter.py diff --git a/__init__.py b/__init__.py index 42cfc1e..9585c64 100644 --- a/__init__.py +++ b/__init__.py @@ -1,7 +1,8 @@ -from PyTeX.default_formatters import ClassFormatter, PackageFormatter, DictionaryFormatter +from PyTeX.default_formatters import ClassFormatter, PackageFormatter, DictionaryFormatter, DocstripFormatter __all__ = [ "ClassFormatter", "PackageFormatter", - "DictionaryFormatter" + "DictionaryFormatter", + 'DocstripFormatter' ] diff --git a/build/build.py b/build/build.py index 2a0f1b0..5a152bb 100644 --- a/build/build.py +++ b/build/build.py @@ -76,6 +76,8 @@ def build( files.append(file) for file in src_dir.rglob('*.pycls3'): files.append(file) + for file in src_dir.rglob('*.dtx'): + files.append(file) else: for file in src_dir.glob('*.pysty'): files.append(file) @@ -87,6 +89,8 @@ def build( files.append(file) for file in src_dir.glob('*.pycls3'): files.append(file) + for file in src_dir.glob('*.dtx'): + files.append(file) sources_to_build = [] for file in files: diff --git a/build/utils/pytex_file.py b/build/utils/pytex_file.py index 0010381..f0a5f61 100644 --- a/build/utils/pytex_file.py +++ b/build/utils/pytex_file.py @@ -2,7 +2,7 @@ from pathlib import Path from typing import Optional, List from PyTeX.build.git_hook import is_recent, get_latest_commit -from PyTeX import PackageFormatter, ClassFormatter, DictionaryFormatter +from PyTeX import PackageFormatter, ClassFormatter, DictionaryFormatter, DocstripFormatter from PyTeX.errors import * from .pytex_msg import pytex_msg from PyTeX.utils import md5 @@ -79,6 +79,8 @@ class TexFileToFormat: latex_file_type = 'class' elif '.pydict' in self.src_path.name: latex_file_type = 'dictionary' + elif '.dtx' in self.src_path.name: + latex_file_type = 'ERROR' else: raise ProgrammingError new_header.append(line.format( @@ -126,6 +128,8 @@ class TexFileToFormat: author=self.current_build_info.author, header=self._header ) + elif self.src_path.name.endswith('.dtx'): + formatter = DocstripFormatter(name=self.src_path.with_suffix('').name) else: raise ProgrammingError formatter.make_default_macros() diff --git a/default_formatters/__init__.py b/default_formatters/__init__.py index 21f3150..bd6fa82 100644 --- a/default_formatters/__init__.py +++ b/default_formatters/__init__.py @@ -1,9 +1,11 @@ from .class_formatter import ClassFormatter from .package_formatter import PackageFormatter from .dictionary_formatter import DictionaryFormatter +from .docstrip_formatter import DocstripFormatter __all__ = [ 'PackageFormatter', 'ClassFormatter', - 'DictionaryFormatter' + 'DictionaryFormatter', + 'DocstripFormatter' ] diff --git a/default_formatters/docstrip_formatter.py b/default_formatters/docstrip_formatter.py new file mode 100644 index 0000000..69ceec5 --- /dev/null +++ b/default_formatters/docstrip_formatter.py @@ -0,0 +1,31 @@ +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]