pytex/PyTeX/format/simple_tex_formatter.py

62 lines
2.1 KiB
Python
Raw Normal View History

from .formatting_config import FormattingConfig
2022-02-06 19:40:14 +01:00
from .tex_formatter import TexFormatter
2022-02-08 17:04:16 +01:00
from pathlib import Path
from typing import Optional, List, Tuple, Dict
2022-02-09 19:50:49 +01:00
from .enums import TeXFlavour, TeXType, FormatterProperty
2022-02-06 19:40:14 +01:00
class SimpleTeXFormatter(TexFormatter):
@property
def output_file(self) -> str:
switcher = {
TeXType.TeXClass: '.cls',
TeXType.TeXPackage: '.sty',
}
return self.name + switcher[self.config.tex_type]
2022-02-08 17:04:16 +01:00
def close_output_stream(self):
self._output_file.close()
2022-02-08 23:54:29 +01:00
@property
def future_config(self) -> List[Tuple[str, FormattingConfig]]:
return [] # TODO
2022-02-08 17:04:16 +01:00
2022-02-08 23:34:18 +01:00
@property
2022-02-08 17:04:16 +01:00
def dependencies(self) -> List[str]:
2022-02-08 17:40:08 +01:00
return [] # sty / cls file does not depend on anything
2022-02-08 17:04:16 +01:00
2022-02-08 18:26:25 +01:00
@property
2022-02-08 17:04:16 +01:00
def output_files(self) -> List[str]:
return [self.output_file]
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()
2022-02-09 19:50:49 +01:00
def format_post_header(self) -> None:
2022-02-09 21:11:46 +01:00
if self.config.has_description:
if self.config.tex_flavour == TeXFlavour.LaTeX2e:
self._shipout_line(
r'\Provides%s{%s}[%s - %s]'
% (
self.config.tex_type.value.capitalize(),
self.name,
self.attribute_dict[FormatterProperty.date.value],
self.attribute_dict[FormatterProperty.description.value]
)
2022-02-09 19:50:49 +01:00
)
2022-02-09 21:11:46 +01:00
else:
self._shipout_line(
'\\ProvidesExpl%s { %s } { %s } { %s }\n { %s }'
% (
self.config.tex_type.value.capitalize(),
self.name,
self.attribute_dict[FormatterProperty.date.value],
self.config.version,
self.config.description
)
2022-02-09 19:50:49 +01:00
)