pytex/PyTeX/format/dtx_formatter.py

100 lines
3.3 KiB
Python

from .constants import INS_FILE, DRV_FILE
from .enums import TeXFlavour, FormatterProperty, TeXType, FormatterMode
from .generic_text import GenericText
from .tex_formatter import TexFormatter
from typing import List, Tuple
from pathlib import Path
from .formatting_config import FormattingConfig
class DTXFormatter(TexFormatter):
@property
def dependencies(self) -> List[str]:
return [] # TODO
@property
def future_config(self) -> List[Tuple[str, FormattingConfig]]:
return [] # TODO
@property
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, comment: bool) -> 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
guards=', '.join(self.config.docstrip_guards),
padding=False,
comment=comment
)
def _get_drv_file(self, comment: bool) -> str:
g = GenericText(DRV_FILE)
return g.format(
documentclass='l3doc', # TODO
preamble='', # TODO
infile=self.name + '.dtx',
padding=False,
comment=comment
)
def format_pre_header(self) -> None:
if self.current_file_name().endswith('.dtx'):
self._shipout_line(r'% \iffalse meta-comment')
def format_post_header(self) -> None:
if self.current_file_name().endswith('.dtx'):
self._shipout_line('%</internal>')
self._shipout_line(
self._get_internal_file(comment=True)
)
self._shipout_line('%<*internal>')
self._shipout_line('%')
self._shipout_line(
'%<{outtype}>{provides}'.format(
outtype=self.config.tex_out_type.value,
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(comment=True)
)
self._shipout_line('%</driver>')
self._shipout_line(r'% \fi')
self._shipout_line('%')
self.mode = FormatterMode.meta
pass
elif self.current_file_name().endswith('.ins'):
self._shipout_line(
self._get_internal_file(comment=False)
)
elif self.current_file_name().endswith('.drv'):
self._shipout_line(
self._get_drv_file(comment=False)
)
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