split up postprocessing of lines and shipout. add trailing % characters for latex2e packages

This commit is contained in:
Maximilian Keßler 2022-02-09 15:56:15 +01:00
parent b4526f5d0c
commit 190ff1a339
2 changed files with 20 additions and 3 deletions

View file

@ -2,6 +2,7 @@ from .formatting_config import FormattingConfig
from .tex_formatter import TexFormatter
from pathlib import Path
from typing import Optional, List, Tuple, Dict
from .enums import TeXFlavour
class SimpleTeXFormatter(TexFormatter):
@ -26,3 +27,10 @@ class SimpleTeXFormatter(TexFormatter):
@property
def output_files(self) -> List[str]:
return [self.input_file.with_suffix('').name]
def _post_process_line(self, line: str) -> str:
if self.config.tex_flavour == TeXFlavour.LaTeX2e:
raw = line.rstrip(' %\n')
return '' if raw == '' else raw + '%'
else:
return line.rstrip()

View file

@ -105,8 +105,15 @@ class TexFormatter(PyTeXFormatter, ABC):
self.line_stream.pop_line()
self.line_stream.push_lines(res)
def _shipout_line(self):
line = self.line_stream.pop_line().rstrip()
def _post_process_line(self, line: str) -> str:
"""
Can strip line or add comment symbols etc.
:param line: Line, potentially with trailing newline
:return: Line without newline symbol
"""
return line.rstrip()
def _shipout_line(self, line):
self.write_line(line + '\n')
def write_line(self, line: str):
@ -135,7 +142,9 @@ class TexFormatter(PyTeXFormatter, ABC):
self._handle_macro(macro)
recent_replacement = True
break
self._shipout_line()
self._shipout_line(self._post_process_line(
self.line_stream.pop_line()
))
def format(self, build_dir: Path, overwrite: bool = False) -> List[Tuple[str, FormattingConfig]]:
self.open_output_stream(build_dir)