implement correct dropping modes

This commit is contained in:
Maximilian Keßler 2022-02-18 00:23:01 +01:00
parent 74b185e35c
commit d1d6252e8b
4 changed files with 57 additions and 8 deletions

View file

@ -32,6 +32,7 @@ def get_default_macros(tex_flavour: TeXFlavour):
ConfigBeginMacro(),
MacroCodeBeginMacro(),
MacroCodeEndMacro(),
GuardMacro(),
]
tex2 = [
ArgumentMacro(

View file

@ -2,15 +2,37 @@ from __future__ import annotations
from enum import Enum
from typing import Optional
from PyTeX.logger import logger
class FormatterMode(Enum):
normal = 0
drop = 1
normal_drop = 1
macrocode = 2
macrocode_drop = 3
meta = 4
meta_drop = 5
def to_drop(self):
switcher = {
FormatterMode.normal: FormatterMode.normal_drop,
FormatterMode.macrocode: FormatterMode.macrocode_drop,
FormatterMode.meta: FormatterMode.meta_drop
}
return switcher[self]
def to_undrop(self):
switcher = {
FormatterMode.normal_drop: FormatterMode.normal,
FormatterMode.macrocode_drop: FormatterMode.macrocode,
FormatterMode.meta_drop: FormatterMode.meta
}
return switcher[self]
def is_drop(self) -> bool:
return self.value in [
FormatterMode.normal_drop.value,
FormatterMode.macrocode_drop.value,
FormatterMode.meta_drop.value
]
class NamingScheme(Enum):

View file

@ -133,6 +133,23 @@ class SingleLineMacro(Macro, ABC):
return True
class RegexSingleLineMacro(SingleLineMacro, ABC):
def __init__(
self,
regex: str,
strip: str = ' %\n'
):
self.regex = regex
super(RegexSingleLineMacro, self).__init__(strip)
def _matches(self, line: str) -> Optional[str]:
match = re.search(self.regex, line)
if match is not None:
return match.group()
else:
return None
class SimpleSingleLineMacro(SingleLineMacro, ABC):
def __init__(
self,
@ -146,14 +163,23 @@ class SimpleSingleLineMacro(SingleLineMacro, ABC):
return self.chars if self.chars in line else None
class GuardMacro(RegexSingleLineMacro):
def __init__(self):
super(GuardMacro, self).__init__(r'<(\*|/|@@=)[a-zA-Z]*>')
def _apply(self, line, formatter) -> Union[str, List[str]]:
match = re.search(self.regex, line)
return '%' + match.group()
class ConfigBeginMacro(SimpleSingleLineMacro):
def __init__(self):
super(ConfigBeginMacro, self).__init__(FORMATTER_PREFIX + INFILE_CONFIG_BEGIN_CONFIG)
def _apply(self, line: str, formatter) -> Union[str, List[str]]:
if formatter.mode not in [FormatterMode.normal, FormatterMode.meta]:
if formatter.mode.is_drop():
raise NotImplementedError # invalid config begin
formatter.mode = FormatterMode.drop
formatter.mode = formatter.mode.to_drop()
return []
@ -162,9 +188,9 @@ class ConfigEndMacro(SimpleSingleLineMacro):
super(ConfigEndMacro, self).__init__(FORMATTER_PREFIX + INFILE_CONFIG_END_CONFIG)
def _apply(self, line: str, formatter) -> Union[str, List[str]]:
if not formatter.mode == FormatterMode.drop:
if not formatter.mode.is_drop():
raise NotImplementedError # invalid
formatter.mode = FormatterMode.normal
formatter.mode = formatter.mode.to_undrop()
return []

View file

@ -152,7 +152,7 @@ class TexFormatter(PyTeXFormatter, ABC):
def write_line(self, line: str):
if self._output_file is None:
raise NotImplementedError
if not self._mode == FormatterMode.drop or self._mode == FormatterMode.macrocode_drop:
if not self.mode.is_drop():
self._output_file.write(line)
def format_pre_header(self) -> None: