add more abstract version of single line macro

This commit is contained in:
Maximilian Keßler 2022-02-18 00:01:16 +01:00
parent c73cbfffcf
commit 74b185e35c

View file

@ -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}')