let macros indicate if their replacement is done directly -> useful for singleLine macros

This commit is contained in:
Maximilian Keßler 2022-02-17 21:13:27 +01:00
parent e1a663e301
commit 09bfa89dfe
3 changed files with 34 additions and 14 deletions

View file

@ -69,6 +69,8 @@ class DTXFormatter(TexFormatter):
self._shipout_line('%</driver>') self._shipout_line('%</driver>')
self._shipout_line(r'% \fi') self._shipout_line(r'% \fi')
self._shipout_line('%') self._shipout_line('%')
self.mode = FormatterMode.meta
pass
def _post_process_line(self, line: str) -> str: def _post_process_line(self, line: str) -> str:
line = line.rstrip(' %\n') line = line.rstrip(' %\n')

View file

@ -74,7 +74,13 @@ class Macro(ABC):
raise NotImplementedError raise NotImplementedError
@abstractmethod @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 raise NotImplementedError
@ -90,12 +96,12 @@ class SimpleMacro(Macro):
def matches(self, line: str) -> bool: def matches(self, line: str) -> bool:
return line.find(FORMATTER_PREFIX + self.macroname) != -1 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( return line.replace(
FORMATTER_PREFIX + self.macroname, FORMATTER_PREFIX + self.macroname,
self.macro_replacement.format( self.macro_replacement.format(
formatter formatter
)) )), False
class SingleLineMacro(Macro, ABC): class SingleLineMacro(Macro, ABC):
@ -115,12 +121,19 @@ class SingleLineMacro(Macro, ABC):
else: else:
return False 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): class ConfigBeginMacro(SingleLineMacro):
def __init__(self): def __init__(self):
super(ConfigBeginMacro, self).__init__(FORMATTER_PREFIX + INFILE_CONFIG_BEGIN_CONFIG) 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: if not formatter.mode == FormatterMode.normal:
raise NotImplementedError # invalid config begin raise NotImplementedError # invalid config begin
formatter.mode = FormatterMode.drop formatter.mode = FormatterMode.drop
@ -131,7 +144,7 @@ class ConfigEndMacro(SingleLineMacro):
def __init__(self): def __init__(self):
super(ConfigEndMacro, self).__init__(FORMATTER_PREFIX + INFILE_CONFIG_END_CONFIG) 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: if not formatter.mode == FormatterMode.drop:
raise NotImplementedError # invalid raise NotImplementedError # invalid
formatter.mode = FormatterMode.normal formatter.mode = FormatterMode.normal
@ -142,7 +155,7 @@ class MacroCodeBeginMacro(SingleLineMacro):
def __init__(self): def __init__(self):
super(MacroCodeBeginMacro, self).__init__(r'\begin{macrocode}') 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: if not formatter.mode == FormatterMode.meta:
raise NotImplementedError raise NotImplementedError
formatter.mode = FormatterMode.macrocode formatter.mode = FormatterMode.macrocode
@ -153,7 +166,7 @@ class MacroCodeEndMacro(SingleLineMacro):
def __init__(self): def __init__(self):
super(MacroCodeEndMacro, self).__init__(r'\end{macrocode}') 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: if not formatter.mode == FormatterMode.macrocode:
raise NotImplementedError raise NotImplementedError
formatter.mode = FormatterMode.meta formatter.mode = FormatterMode.meta
@ -184,7 +197,7 @@ class ArgumentMacro(Macro):
else: else:
return True 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) match = re.search(self._search_regex, line)
if match is None: if match is None:
raise NotImplementedError raise NotImplementedError
@ -194,4 +207,4 @@ class ArgumentMacro(Macro):
return line.replace( return line.replace(
match.group(), match.group(),
replacement replacement
) ), False

View file

@ -6,6 +6,7 @@ from .formatting_config import FormattingConfig
from .macros import Macro from .macros import Macro
from .pytex_formatter import PyTeXFormatter from .pytex_formatter import PyTeXFormatter
from .enums import * from .enums import *
from ..logger import logger
class LineStream: class LineStream:
@ -101,15 +102,19 @@ class TexFormatter(PyTeXFormatter, ABC):
self._macros = macros self._macros = macros
def _handle_macro(self, macro: Macro): def _handle_macro(self, macro: Macro):
res = macro.apply( replacement, shipout = macro.apply(
self.line_stream.current_line(), self.line_stream.current_line(),
self self
) )
if isinstance(res, str): if shipout:
self.line_stream.set_line(res)
else:
self.line_stream.pop_line() 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: def _post_process_line(self, line: str) -> str:
""" """