From 09bfa89dfe653510925a2e412c45dc50fbec871e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maximilian=20Ke=C3=9Fler?= Date: Thu, 17 Feb 2022 21:13:27 +0100 Subject: [PATCH] let macros indicate if their replacement is done directly -> useful for singleLine macros --- PyTeX/format/dtx_formatter.py | 2 ++ PyTeX/format/macros.py | 31 ++++++++++++++++++++++--------- PyTeX/format/tex_formatter.py | 15 ++++++++++----- 3 files changed, 34 insertions(+), 14 deletions(-) diff --git a/PyTeX/format/dtx_formatter.py b/PyTeX/format/dtx_formatter.py index 39e1222..7aea254 100644 --- a/PyTeX/format/dtx_formatter.py +++ b/PyTeX/format/dtx_formatter.py @@ -69,6 +69,8 @@ class DTXFormatter(TexFormatter): self._shipout_line('%') self._shipout_line(r'% \fi') self._shipout_line('%') + self.mode = FormatterMode.meta + pass def _post_process_line(self, line: str) -> str: line = line.rstrip(' %\n') diff --git a/PyTeX/format/macros.py b/PyTeX/format/macros.py index 6aede42..597e996 100644 --- a/PyTeX/format/macros.py +++ b/PyTeX/format/macros.py @@ -74,7 +74,13 @@ class Macro(ABC): raise NotImplementedError @abstractmethod - def apply(self, line: str, formatter) -> Union[str, List[str]]: + def apply(self, line: str, formatter) -> Tuple[Union[str, List[str]], bool]: + """ + + :param line: Line where macro matches + :param formatter: + :return: First: replacement. Second: indicates direct shipout if True + """ raise NotImplementedError @@ -90,12 +96,12 @@ class SimpleMacro(Macro): def matches(self, line: str) -> bool: return line.find(FORMATTER_PREFIX + self.macroname) != -1 - def apply(self, line: str, formatter) -> Union[str, List[str]]: + def apply(self, line: str, formatter) -> Tuple[Union[str, List[str]], bool]: return line.replace( FORMATTER_PREFIX + self.macroname, self.macro_replacement.format( formatter - )) + )), False class SingleLineMacro(Macro, ABC): @@ -115,12 +121,19 @@ class SingleLineMacro(Macro, ABC): else: return False + @abstractmethod + def _apply(self, line, formatter) -> Union[str, List[str]]: + pass + + def apply(self, line: str, formatter) -> Tuple[Union[str, List[str]], bool]: + return self._apply(line, formatter), True + class ConfigBeginMacro(SingleLineMacro): def __init__(self): super(ConfigBeginMacro, self).__init__(FORMATTER_PREFIX + INFILE_CONFIG_BEGIN_CONFIG) - def apply(self, line: str, formatter) -> Union[str, List[str]]: + def _apply(self, line: str, formatter) -> Union[str, List[str]]: if not formatter.mode == FormatterMode.normal: raise NotImplementedError # invalid config begin formatter.mode = FormatterMode.drop @@ -131,7 +144,7 @@ class ConfigEndMacro(SingleLineMacro): def __init__(self): super(ConfigEndMacro, self).__init__(FORMATTER_PREFIX + INFILE_CONFIG_END_CONFIG) - def apply(self, line: str, formatter) -> Union[str, List[str]]: + def _apply(self, line: str, formatter) -> Union[str, List[str]]: if not formatter.mode == FormatterMode.drop: raise NotImplementedError # invalid formatter.mode = FormatterMode.normal @@ -142,7 +155,7 @@ class MacroCodeBeginMacro(SingleLineMacro): def __init__(self): super(MacroCodeBeginMacro, self).__init__(r'\begin{macrocode}') - def apply(self, line: str, formatter) -> Union[str, List[str]]: + def _apply(self, line: str, formatter) -> Union[str, List[str]]: if not formatter.mode == FormatterMode.meta: raise NotImplementedError formatter.mode = FormatterMode.macrocode @@ -153,7 +166,7 @@ class MacroCodeEndMacro(SingleLineMacro): def __init__(self): super(MacroCodeEndMacro, self).__init__(r'\end{macrocode}') - def apply(self, line: str, formatter) -> Union[str, List[str]]: + def _apply(self, line: str, formatter) -> Union[str, List[str]]: if not formatter.mode == FormatterMode.macrocode: raise NotImplementedError formatter.mode = FormatterMode.meta @@ -184,7 +197,7 @@ class ArgumentMacro(Macro): else: return True - def apply(self, line: str, formatter) -> Union[str, List[str]]: + def apply(self, line: str, formatter) -> Tuple[Union[str, List[str]], bool]: match = re.search(self._search_regex, line) if match is None: raise NotImplementedError @@ -194,4 +207,4 @@ class ArgumentMacro(Macro): return line.replace( match.group(), replacement - ) + ), False diff --git a/PyTeX/format/tex_formatter.py b/PyTeX/format/tex_formatter.py index e526fef..f7cfcb8 100644 --- a/PyTeX/format/tex_formatter.py +++ b/PyTeX/format/tex_formatter.py @@ -6,6 +6,7 @@ from .formatting_config import FormattingConfig from .macros import Macro from .pytex_formatter import PyTeXFormatter from .enums import * +from ..logger import logger class LineStream: @@ -101,15 +102,19 @@ class TexFormatter(PyTeXFormatter, ABC): self._macros = macros def _handle_macro(self, macro: Macro): - res = macro.apply( + replacement, shipout = macro.apply( self.line_stream.current_line(), self ) - if isinstance(res, str): - self.line_stream.set_line(res) - else: + if shipout: self.line_stream.pop_line() - self.line_stream.push_lines(res) + self._shipout_line(replacement) + else: + if isinstance(replacement, str): + self.line_stream.set_line(replacement) + else: + self.line_stream.pop_line() + self.line_stream.push_lines(replacement) def _post_process_line(self, line: str) -> str: """