implement simple tex formatter

This commit is contained in:
Maximilian Keßler 2022-02-08 17:04:16 +01:00
parent b69019d814
commit 1a6989734e
4 changed files with 58 additions and 21 deletions

View file

@ -1,10 +1,11 @@
from pathlib import Path from pathlib import Path
from typing import List, Optional, Dict, Tuple from typing import List, Optional, Dict, Tuple
from abc import ABC, abstractmethod
from .config import Config from .config import Config
class FormatterIF: class FormatterIF(ABC):
""" """
A formatter is bound to a specific input file with some A formatter is bound to a specific input file with some
building configuration. building configuration.
@ -18,6 +19,7 @@ class FormatterIF:
self._input_file: Optional[Path] = input_file self._input_file: Optional[Path] = input_file
self._config: Optional[Config] = config self._config: Optional[Config] = config
@abstractmethod
def format(self, build_dir: Path, overwrite: bool = False) -> Optional[List[Tuple[str, Dict]]]: def format(self, build_dir: Path, overwrite: bool = False) -> Optional[List[Tuple[str, Dict]]]:
""" """
:param build_dir: Directory where output files are written to :param build_dir: Directory where output files are written to
@ -26,15 +28,21 @@ class FormatterIF:
build of the output files, a list of the file names and their build of the output files, a list of the file names and their
needed configurations. Else None. needed configurations. Else None.
""" """
raise NotImplementedError
@abstractmethod
@property @property
def output_files(self) -> List[str]: def output_files(self) -> List[str]:
""" """
:return: List of files that will be built when the formatter is invoked :return: List of files that will be built when the formatter is invoked
""" """
raise NotImplementedError
@abstractmethod
@property
def dependencies(self) -> List[str]:
"""
:return: List of dependencies (as str filenames)
"""
@property @property
def input_file(self) -> Path: def input_file(self) -> Path:
@ -55,7 +63,3 @@ class FormatterIF:
@config.setter @config.setter
def config(self, config: Config): def config(self, config: Config):
self._config = config self._config = config
@property
def dependencies(self) -> List[str]:
raise NotImplementedError

View file

@ -8,9 +8,10 @@ from .formatterif import FormatterIF
from .formatting_config import FormattingConfig from .formatting_config import FormattingConfig
from .generic_text import GenericText from .generic_text import GenericText
from ..logger import logger from ..logger import logger
from abc import ABC, abstractmethod
class PyTeXFormatter(FormatterIF): class PyTeXFormatter(FormatterIF, ABC):
def __init__( def __init__(
self, self,
input_file: Optional[Path] = None, input_file: Optional[Path] = None,

View file

@ -1,5 +1,24 @@
from .tex_formatter import TexFormatter from .tex_formatter import TexFormatter
from pathlib import Path
from typing import Optional, List, Tuple, Dict
class SimpleTeXFormatter(TexFormatter): class SimpleTeXFormatter(TexFormatter):
def open_output_stream(self, build_dir: Path):
out_file = build_dir / self.input_file.with_suffix('').name
if out_file.exists():
raise NotImplementedError
else:
self._output_file = out_file.open()
def close_output_stream(self):
self._output_file.close()
def future_config(self) -> Optional[List[Tuple[str, Dict]]]:
pass pass
def dependencies(self) -> List[str]:
return []
def output_files(self) -> List[str]:
return []

View file

@ -1,5 +1,6 @@
from pathlib import Path from pathlib import Path
from typing import List, TextIO, Optional, Tuple, Dict from typing import List, TextIO, Optional, Tuple, Dict
from abc import ABC, abstractmethod
from .macros import Macro from .macros import Macro
from .pytex_formatter import PyTeXFormatter from .pytex_formatter import PyTeXFormatter
@ -52,13 +53,35 @@ class LineStream:
return self._file_exhausted & len(self._cached_lines) == 0 return self._file_exhausted & len(self._cached_lines) == 0
class TexFormatter(PyTeXFormatter): class TexFormatter(PyTeXFormatter, ABC):
def __init__(self, *args, **kwargs): def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs) super().__init__(*args, **kwargs)
self._macros: List[Macro] = [] self._macros: List[Macro] = []
self._line_stream: Optional[LineStream] = None self._line_stream: Optional[LineStream] = None
self._output_file: Optional[TextIO] = None self._output_file: Optional[TextIO] = None
@abstractmethod
def open_output_stream(self, build_dir: Path) -> None:
"""
:param build_dir: Where to open output stream
"""
@abstractmethod
def close_output_stream(self) -> None:
"""
:return:
"""
@abstractmethod
@property
def future_config(self) -> Optional[List[Tuple[str, Dict]]]:
"""
# TODO
:return:
"""
@property @property
def line_stream(self) -> LineStream: def line_stream(self) -> LineStream:
if self._line_stream is None: if self._line_stream is None:
@ -89,16 +112,6 @@ class TexFormatter(PyTeXFormatter):
raise NotImplementedError raise NotImplementedError
self._output_file.write(line) self._output_file.write(line)
def open_output_stream(self):
raise NotImplementedError
def close_output_stream(self):
raise NotImplementedError
@property
def future_config(self) -> Optional[List[Tuple[str, Dict]]]:
raise NotImplementedError
def format_pre_header(self) -> None: def format_pre_header(self) -> None:
pass pass
@ -123,7 +136,7 @@ class TexFormatter(PyTeXFormatter):
self._shipout_line() self._shipout_line()
def format(self, build_dir: Path, overwrite: bool = False) -> Optional[List[Tuple[str, Dict]]]: def format(self, build_dir: Path, overwrite: bool = False) -> Optional[List[Tuple[str, Dict]]]:
self.open_output_stream() self.open_output_stream(build_dir)
self.format_pre_header() self.format_pre_header()
self.format_header() self.format_header()
self.format_post_header() self.format_post_header()