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

View file

@ -25,7 +25,7 @@ class DTXFormatter(TexFormatter):
files.append(self.name + '.ins') files.append(self.name + '.ins')
return files return files
def _get_internal_file(self) -> str: def _get_internal_file(self, comment: bool) -> str:
g = GenericText(INS_FILE) g = GenericText(INS_FILE)
switcher = { switcher = {
TeXType.TeXPackage: '.sty', TeXType.TeXPackage: '.sty',
@ -37,46 +37,58 @@ class DTXFormatter(TexFormatter):
preamble='', # TODO preamble='', # TODO
postamble='', # TODO postamble='', # TODO
guards=', '.join(self.config.docstrip_guards), 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) g = GenericText(DRV_FILE)
return g.format( return g.format(
documentclass='l3doc', # TODO documentclass='l3doc', # TODO
preamble='', # TODO preamble='', # TODO
infile=self.name + '.dtx', infile=self.name + '.dtx',
padding=False padding=False,
comment=comment
) )
def format_pre_header(self) -> None: def format_pre_header(self) -> None:
self._shipout_line(r'% \iffalse meta-comment') if self.current_file_name().endswith('.dtx'):
self._shipout_line(r'% \iffalse meta-comment')
def format_post_header(self) -> None: def format_post_header(self) -> None:
self._shipout_line('%</internal>') if self.current_file_name().endswith('.dtx'):
self._shipout_line( self._shipout_line('%</internal>')
self._get_internal_file() self._shipout_line(
) self._get_internal_file(comment=True)
self._shipout_line('%<*internal>') )
self._shipout_line('%') self._shipout_line('%<*internal>')
self._shipout_line( self._shipout_line('%')
'%<{outtype}>{provides}'.format( self._shipout_line(
outtype=self.config.tex_out_type.value, '%<{outtype}>{provides}'.format(
provides=self._get_provides_text( outtype=self.config.tex_out_type.value,
self.config.tex_out_type.value.capitalize() provides=self._get_provides_text(
self.config.tex_out_type.value.capitalize()
)
) )
) )
) self._shipout_line('%')
self._shipout_line('%') self._shipout_line('%<*driver>')
self._shipout_line('%<*driver>') self._shipout_line(
self._shipout_line( self._get_drv_file(comment=True)
self._get_drv_file() )
) self._shipout_line('%</driver>')
self._shipout_line('%</driver>') self._shipout_line(r'% \fi')
self._shipout_line(r'% \fi') self._shipout_line('%')
self._shipout_line('%') self.mode = FormatterMode.meta
self.mode = FormatterMode.meta pass
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: def _post_process_line(self, line: str) -> str:
line = line.rstrip(' %\n') line = line.rstrip(' %\n')

View file

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

View file

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