From 74b185e35cc423828f3086ea71fd2bbbabe9b465 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maximilian=20Ke=C3=9Fler?= Date: Fri, 18 Feb 2022 00:01:16 +0100 Subject: [PATCH] add more abstract version of single line macro --- PyTeX/format/macros.py | 44 ++++++++++++++++++++++++++++-------------- 1 file changed, 30 insertions(+), 14 deletions(-) diff --git a/PyTeX/format/macros.py b/PyTeX/format/macros.py index c5fd41b..0b78a2c 100644 --- a/PyTeX/format/macros.py +++ b/PyTeX/format/macros.py @@ -108,20 +108,10 @@ class SimpleMacro(Macro): class SingleLineMacro(Macro, ABC): def __init__( self, - chars: str, strip: str = ' %\n' ): - self.chars = chars self.strip = strip - def matches(self, line: str) -> bool: - if line.find(self.chars) != -1: - if not line.strip(self.strip) == self.chars: - raise NotImplementedError - return True - else: - return False - @abstractmethod def _apply(self, line, formatter) -> Union[str, List[str]]: pass @@ -129,8 +119,34 @@ class SingleLineMacro(Macro, ABC): def apply(self, line: str, formatter) -> Tuple[Union[str, List[str]], bool]: return self._apply(line, formatter), True + @abstractmethod + def _matches(self, line: str) -> Optional[str]: + pass -class ConfigBeginMacro(SingleLineMacro): + def matches(self, line: str) -> bool: + match = self._matches(line.strip(self.strip)) + if match is None: + return False + else: + if not line.strip(self.strip) == match: + raise NotImplementedError + return True + + +class SimpleSingleLineMacro(SingleLineMacro, ABC): + def __init__( + self, + chars: str, + strip: str = ' %\n' + ): + self.chars = chars + super(SimpleSingleLineMacro, self).__init__(strip) + + def _matches(self, line: str) -> Optional[str]: + return self.chars if self.chars in line else None + + +class ConfigBeginMacro(SimpleSingleLineMacro): def __init__(self): super(ConfigBeginMacro, self).__init__(FORMATTER_PREFIX + INFILE_CONFIG_BEGIN_CONFIG) @@ -141,7 +157,7 @@ class ConfigBeginMacro(SingleLineMacro): return [] -class ConfigEndMacro(SingleLineMacro): +class ConfigEndMacro(SimpleSingleLineMacro): def __init__(self): super(ConfigEndMacro, self).__init__(FORMATTER_PREFIX + INFILE_CONFIG_END_CONFIG) @@ -152,7 +168,7 @@ class ConfigEndMacro(SingleLineMacro): return [] -class MacroCodeBeginMacro(SingleLineMacro): +class MacroCodeBeginMacro(SimpleSingleLineMacro): def __init__(self): super(MacroCodeBeginMacro, self).__init__(r'\begin{macrocode}') @@ -165,7 +181,7 @@ class MacroCodeBeginMacro(SingleLineMacro): return r'% \begin{macrocode}' -class MacroCodeEndMacro(SingleLineMacro): +class MacroCodeEndMacro(SimpleSingleLineMacro): def __init__(self): super(MacroCodeEndMacro, self).__init__(r'\end{macrocode}')