implement drv and ins file in dtx formatter

This commit is contained in:
Maximilian Keßler 2022-02-17 23:46:32 +01:00
parent bde5c63b01
commit 234e3d9983
4 changed files with 54 additions and 29 deletions

View file

@ -6,6 +6,8 @@ import shutil
from .enums import PyTeXRootDirType
from .pytex_config import PyTeXConfig
from ...exceptions import PyTeXException
from ...format.errors import PyTeXError
from ...format.formatting_config import FormattingConfig
from ...logger import logger
from .constants import *
@ -304,7 +306,11 @@ class PyTeXBuilder:
logger.info(f"Needing to build {len(self._files_to_build)} many files.")
self._check_output_directory_integrity() # 1ms
logger.info(f"Starting build")
try:
self._build_files() # 53 ms
except PyTeXError as e:
e.add_explanation('while building output files')
raise e
logger.info(f"Built files")
self._move_files()
self._write_version_info()

View file

@ -25,7 +25,7 @@ class DTXFormatter(TexFormatter):
files.append(self.name + '.ins')
return files
def _get_internal_file(self) -> str:
def _get_internal_file(self, comment: bool) -> str:
g = GenericText(INS_FILE)
switcher = {
TeXType.TeXPackage: '.sty',
@ -37,25 +37,29 @@ class DTXFormatter(TexFormatter):
preamble='', # TODO
postamble='', # TODO
guards=', '.join(self.config.docstrip_guards),
padding=False
padding=False,
comment=comment
)
def _get_drv_file(self) -> str:
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
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()
self._get_internal_file(comment=True)
)
self._shipout_line('%<*internal>')
self._shipout_line('%')
@ -70,13 +74,21 @@ class DTXFormatter(TexFormatter):
self._shipout_line('%')
self._shipout_line('%<*driver>')
self._shipout_line(
self._get_drv_file()
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')

View file

@ -63,13 +63,19 @@ class GenericText:
def format(self, **kwargs) -> str:
padding = True
comment = True
if 'padding' in kwargs.keys():
padding = kwargs['padding']
kwargs.pop('padding', None)
if 'comment' in kwargs.keys():
comment = kwargs['comment']
kwargs.pop('comment', None)
lines = []
for line in self.text:
try:
line = '% ' + line.rstrip().format(**kwargs)
line = line.rstrip().format(**kwargs)
if comment:
line = '% ' + line
if padding:
line = line.ljust(79) + '%'
if len(line) > 80:

View file

@ -82,6 +82,7 @@ class TexFormatter(PyTeXFormatter, ABC):
raise NotImplementedError
else:
self._output_file = out_file.open('w')
self.mode = FormatterMode.normal
def close_output_stream(self) -> None:
self._output_file.close()