pytex/PyTeX/format/dtx_formatter.py

89 lines
2.7 KiB
Python
Raw Normal View History

from .constants import INS_FILE, DRV_FILE
from .enums import TeXFlavour, FormatterProperty, TeXType, FormatterMode
from .generic_text import GenericText
2022-02-06 19:40:14 +01:00
from .tex_formatter import TexFormatter
2022-02-08 18:26:25 +01:00
from typing import List, Tuple
from pathlib import Path
from .formatting_config import FormattingConfig
2022-02-06 19:40:14 +01:00
class DTXFormatter(TexFormatter):
2022-02-08 21:57:30 +01:00
@property
2022-02-08 18:26:25 +01:00
def dependencies(self) -> List[str]:
2022-02-08 21:57:30 +01:00
return [] # TODO
2022-02-08 18:26:25 +01:00
2022-02-17 21:39:01 +01:00
@property
2022-02-08 18:26:25 +01:00
def future_config(self) -> List[Tuple[str, FormattingConfig]]:
2022-02-08 21:57:30 +01:00
return [] # TODO
2022-02-08 18:26:25 +01:00
2022-02-08 21:57:30 +01:00
@property
2022-02-08 18:26:25 +01:00
def output_files(self) -> List[str]:
files = [self.name + '.dtx']
if self.config.include_drv:
files.append(self.name + '.drv')
if self.config.include_ins:
files.append(self.name + '.ins')
return files
def _get_internal_file(self) -> str:
g = GenericText(INS_FILE)
switcher = {
TeXType.TeXPackage: '.sty',
TeXType.TeXClass: '.cls'
}
return g.format(
infile=self.name + '.dtx',
outfile=self.name + switcher[self.config.tex_out_type],
preamble='', # TODO
postamble='', # TODO
2022-02-17 21:44:34 +01:00
guards=', '.join(self.config.docstrip_guards),
padding=False
)
def _get_drv_file(self) -> str:
g = GenericText(DRV_FILE)
return g.format(
documentclass='l3doc', # TODO
preamble='', # TODO
infile=self.name + '.dtx',
2022-02-17 21:44:34 +01:00
padding=False
)
def format_pre_header(self) -> None:
self._shipout_line(r'% \iffalse meta-comment')
2022-02-08 18:26:25 +01:00
def format_post_header(self) -> None:
self._shipout_line('%</internal>')
self._shipout_line(
self._get_internal_file()
)
2022-02-17 22:30:51 +01:00
self._shipout_line('%<*internal>')
self._shipout_line('%')
self._shipout_line(
'%<{outtype}>{provides}'.format(
outtype=self.config.tex_out_type.value,
2022-02-17 22:30:51 +01:00
provides=self._get_provides_text(
self.config.tex_out_type.value.capitalize()
)
)
)
self._shipout_line('%')
self._shipout_line('%<*driver>')
self._shipout_line(
self._get_drv_file()
)
self._shipout_line('%</driver>')
self._shipout_line(r'% \fi')
self._shipout_line('%')
self.mode = FormatterMode.meta
pass
2022-02-08 18:26:25 +01:00
def _post_process_line(self, line: str) -> str:
line = line.rstrip(' %\n')
if self.mode == FormatterMode.meta:
line = '% ' + line
if self.mode == FormatterMode.macrocode:
if self.config.tex_flavour == TeXFlavour.LaTeX2e:
line = line.rstrip(' %\n') + '%'
return line