From 190ff1a3398275ae5a1c36e208932fa3aa305310 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maximilian=20Ke=C3=9Fler?= Date: Wed, 9 Feb 2022 15:56:15 +0100 Subject: [PATCH] split up postprocessing of lines and shipout. add trailing % characters for latex2e packages --- PyTeX/format/simple_tex_formatter.py | 8 ++++++++ PyTeX/format/tex_formatter.py | 15 ++++++++++++--- 2 files changed, 20 insertions(+), 3 deletions(-) diff --git a/PyTeX/format/simple_tex_formatter.py b/PyTeX/format/simple_tex_formatter.py index 6958764..625cb2c 100644 --- a/PyTeX/format/simple_tex_formatter.py +++ b/PyTeX/format/simple_tex_formatter.py @@ -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() diff --git a/PyTeX/format/tex_formatter.py b/PyTeX/format/tex_formatter.py index 4980ce6..3777389 100644 --- a/PyTeX/format/tex_formatter.py +++ b/PyTeX/format/tex_formatter.py @@ -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)