let macros indicate if their replacement is done directly -> useful for singleLine macros
This commit is contained in:
parent
e1a663e301
commit
09bfa89dfe
3 changed files with 34 additions and 14 deletions
|
@ -69,6 +69,8 @@ class DTXFormatter(TexFormatter):
|
|||
self._shipout_line('%</driver>')
|
||||
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')
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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:
|
||||
"""
|
||||
|
|
Loading…
Reference in a new issue