implement config begin and end macros to drop config at beginning of file

This commit is contained in:
Maximilian Keßler 2022-02-09 19:39:27 +01:00
parent 741662dbef
commit e760eaef6d
3 changed files with 48 additions and 2 deletions

View file

@ -14,5 +14,7 @@ def make_simple_macro(name: str, arg):
def get_default_macros():
return [
make_simple_macro('!', FormatterProperty.file_prefix),
make_simple_macro('name', FormatterProperty.name)
make_simple_macro('name', FormatterProperty.name),
ConfigEndMacro(),
ConfigBeginMacro()
]

View file

@ -2,7 +2,7 @@ import re
from typing import List, Union, Tuple, Dict
from .constants import *
from .enums import FormatterProperty, Argument
from .enums import FormatterProperty, Argument, FormatterMode
from abc import ABC, abstractmethod
@ -98,6 +98,42 @@ 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
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]]:
if not formatter.mode == FormatterMode.normal:
raise NotImplementedError # invalid config begin
formatter.mode = FormatterMode.drop
return []
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]]:
if not formatter.mode == FormatterMode.drop:
raise NotImplementedError # invalid
formatter.mode = FormatterMode.normal
return []
class ArgumentMacro(Macro):
def __init__(
self,

View file

@ -135,6 +135,14 @@ class TexFormatter(PyTeXFormatter, ABC):
def format_pre_header(self) -> None:
pass
@property
def mode(self) -> FormatterMode:
return self._mode
@mode.setter
def mode(self, mode: FormatterMode) -> None:
self._mode = mode
def format_header(self):
if self._output_file is None:
raise NotImplementedError