From 9fc559cbafa510aeb1f1968df4fc886a5793132e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maximilian=20Ke=C3=9Fler?= Date: Fri, 18 Feb 2022 09:50:02 +0100 Subject: [PATCH] add begin / end implementation macros. fix some bugs --- PyTeX/format/constants.py | 4 +++- PyTeX/format/default_macros.py | 3 +++ PyTeX/format/dtx_formatter.py | 21 ++++++++++++----- PyTeX/format/enums.py | 3 ++- PyTeX/format/macros.py | 41 +++++++++++++++++++++++++++++++-- PyTeX/format/pytex_formatter.py | 3 ++- PyTeX/format/tex_formatter.py | 5 ++-- 7 files changed, 67 insertions(+), 13 deletions(-) diff --git a/PyTeX/format/constants.py b/PyTeX/format/constants.py index 73b1e50..331aac2 100644 --- a/PyTeX/format/constants.py +++ b/PyTeX/format/constants.py @@ -4,6 +4,8 @@ PYTEX_CONFIG_FILE_EXTENSION = '.conf' DICTIONARY_KEY_COLUMN_NAME = 'key' DICTIONARY_NAMING_PATTERN = 'translator-{dict_name}-dictionary-{language}.dict' FORMATTER_PREFIX = '!' +IMPLEMENTATION_BEGIN_MACRO = 'beginimpl' +IMPLEMENTATION_END_MACRO = 'endimpl' YAML_INFO = 'info' YAML_NAMING_SCHEME = 'name' @@ -60,7 +62,7 @@ INS_FILE = [ DRV_FILE = [ r'\documentclass{{{documentclass}}}', - r'{{{preamble}}}', + r'{{preamble}}', r'\begin{{document}}', r'\DocInput{{{infile}}}', r'\end{{document}}' diff --git a/PyTeX/format/default_macros.py b/PyTeX/format/default_macros.py index f255067..dbdbd40 100644 --- a/PyTeX/format/default_macros.py +++ b/PyTeX/format/default_macros.py @@ -28,6 +28,9 @@ def get_default_macros(tex_flavour: TeXFlavour): make_simple_macro('repocommit', FormatterProperty.repo_commit), make_simple_macro('repodirty', FormatterProperty.repo_dirty), make_simple_macro('sourcename', FormatterProperty.source_file_name), + make_simple_macro('outtype', FormatterProperty.tex_out_type), + ImplementationBeginMacro(), + ImplementationEndMacro(), ConfigEndMacro(), ConfigBeginMacro(), MacroCodeBeginMacro(), diff --git a/PyTeX/format/dtx_formatter.py b/PyTeX/format/dtx_formatter.py index 0063925..bf18ec3 100644 --- a/PyTeX/format/dtx_formatter.py +++ b/PyTeX/format/dtx_formatter.py @@ -63,13 +63,18 @@ class DTXFormatter(TexFormatter): ) self._shipout_line('%') self._shipout_line('%') + provides = self._get_provides_text( + self.config.tex_out_type.value.capitalize() + ) + parts = provides.split('\n') + parts = [ + '%<{outtype}>'.format( + outtype=self.config.tex_out_type.value + ) + part + for part in parts + ] self._shipout_line( - '%<{outtype}>{provides}'.format( - outtype=self.config.tex_out_type.value, - provides=self._get_provides_text( - self.config.tex_out_type.value.capitalize() - ) - ) + '\n'.join(parts) ) self._shipout_line('%') self._shipout_line('%<*driver>') @@ -92,6 +97,10 @@ class DTXFormatter(TexFormatter): def _post_process_line(self, line: str) -> str: line = line.rstrip(' %\n') + if self.mode == FormatterMode.meta: + line = line.lstrip('%') + if line.startswith(' '): + line = line[1:] if self.mode == FormatterMode.meta: line = '% ' + line if self.mode == FormatterMode.macrocode: diff --git a/PyTeX/format/enums.py b/PyTeX/format/enums.py index 05efc25..df741d4 100644 --- a/PyTeX/format/enums.py +++ b/PyTeX/format/enums.py @@ -135,7 +135,8 @@ class FormatterProperty(MacroReplacementAtomIF, Enum): pytex_commit = 'pytex_commit' pytex_dirty = 'pytex_dirty' tex_type = 'tex_type' - Tex_type= 'Tex_type' + Tex_type = 'Tex_type' + tex_out_type = 'tex_outtype' tex_flavour = 'latex_flavour' description = 'description' diff --git a/PyTeX/format/macros.py b/PyTeX/format/macros.py index aac4e25..be6ea35 100644 --- a/PyTeX/format/macros.py +++ b/PyTeX/format/macros.py @@ -113,11 +113,15 @@ class SingleLineMacro(Macro, ABC): self.strip = strip @abstractmethod - def _apply(self, line, formatter) -> Union[str, List[str]]: + def _apply(self, line, formatter) -> Union[Union[str, List[str]], Tuple[Union[str, List[str]], bool]]: pass def apply(self, line: str, formatter) -> Tuple[Union[str, List[str]], bool]: - return self._apply(line, formatter), True + replacement = self._apply(line, formatter) + if isinstance(replacement, tuple): + return replacement + else: + return replacement, True @abstractmethod def _matches(self, line: str) -> Optional[str]: @@ -194,6 +198,39 @@ class ConfigEndMacro(SimpleSingleLineMacro): return [] +class ImplementationBeginMacro(SimpleSingleLineMacro): + def __init__(self): + super(ImplementationBeginMacro, self).__init__(FORMATTER_PREFIX + IMPLEMENTATION_BEGIN_MACRO) + + def _apply(self, line, formatter) -> Tuple[Union[str, List[str]], bool]: + return [ + r'% \begin{implementation}', + r'', + r'\section{\pkg{!name} implementation}', + r'\begin{macrocode}', + r'<*!outtype>', + r'\end{macrocode}', + r'', + r'\begin{macrocode}', + r'<@@=!!>', + r'\end{macrocode}', + ], False + + +class ImplementationEndMacro(SimpleSingleLineMacro): + def __init__(self): + super(ImplementationEndMacro, self).__init__(FORMATTER_PREFIX + IMPLEMENTATION_END_MACRO) + + def _apply(self, line, formatter) -> Tuple[Union[str, List[str]], bool]: + return [ + r'\begin{macrocode}', + r'', + r'\end{macrocode}', + r'', + r'% \end{implementation}' + ], False + + class MacroCodeBeginMacro(SimpleSingleLineMacro): def __init__(self): super(MacroCodeBeginMacro, self).__init__(r'\begin{macrocode}') diff --git a/PyTeX/format/pytex_formatter.py b/PyTeX/format/pytex_formatter.py index e50d2f7..878de82 100644 --- a/PyTeX/format/pytex_formatter.py +++ b/PyTeX/format/pytex_formatter.py @@ -154,7 +154,8 @@ class PyTeXFormatter(FormatterIF, ABC): FormatterProperty.tex_flavour.value: self.config.tex_flavour.value, FormatterProperty.file_prefix.value: self.file_prefix, FormatterProperty.description.value: self.config.description, - FormatterProperty.Tex_type.value: self.config.tex_type.value.capitalize() + FormatterProperty.Tex_type.value: self.config.tex_type.value.capitalize(), + FormatterProperty.tex_out_type.value: self.config.tex_out_type.value, } @property diff --git a/PyTeX/format/tex_formatter.py b/PyTeX/format/tex_formatter.py index aa42c50..62f89ba 100644 --- a/PyTeX/format/tex_formatter.py +++ b/PyTeX/format/tex_formatter.py @@ -123,8 +123,9 @@ class TexFormatter(PyTeXFormatter, ABC): if isinstance(replacement, str): self._shipout_line(replacement) else: - for line in replacement: - self._shipout_line(line) + if len(replacement) >= 1: + self._shipout_line(replacement[0]) + self._line_stream.push_lines(replacement[1:]) else: if isinstance(replacement, str): self.line_stream.set_line(replacement)