add begin / end implementation macros. fix some bugs

This commit is contained in:
Maximilian Keßler 2022-02-18 09:50:02 +01:00
parent d1d6252e8b
commit 9fc559cbaf
7 changed files with 67 additions and 13 deletions

View file

@ -4,6 +4,8 @@ PYTEX_CONFIG_FILE_EXTENSION = '.conf'
DICTIONARY_KEY_COLUMN_NAME = 'key' DICTIONARY_KEY_COLUMN_NAME = 'key'
DICTIONARY_NAMING_PATTERN = 'translator-{dict_name}-dictionary-{language}.dict' DICTIONARY_NAMING_PATTERN = 'translator-{dict_name}-dictionary-{language}.dict'
FORMATTER_PREFIX = '!' FORMATTER_PREFIX = '!'
IMPLEMENTATION_BEGIN_MACRO = 'beginimpl'
IMPLEMENTATION_END_MACRO = 'endimpl'
YAML_INFO = 'info' YAML_INFO = 'info'
YAML_NAMING_SCHEME = 'name' YAML_NAMING_SCHEME = 'name'
@ -60,7 +62,7 @@ INS_FILE = [
DRV_FILE = [ DRV_FILE = [
r'\documentclass{{{documentclass}}}', r'\documentclass{{{documentclass}}}',
r'{{{preamble}}}', r'{{preamble}}',
r'\begin{{document}}', r'\begin{{document}}',
r'\DocInput{{{infile}}}', r'\DocInput{{{infile}}}',
r'\end{{document}}' r'\end{{document}}'

View file

@ -28,6 +28,9 @@ def get_default_macros(tex_flavour: TeXFlavour):
make_simple_macro('repocommit', FormatterProperty.repo_commit), make_simple_macro('repocommit', FormatterProperty.repo_commit),
make_simple_macro('repodirty', FormatterProperty.repo_dirty), make_simple_macro('repodirty', FormatterProperty.repo_dirty),
make_simple_macro('sourcename', FormatterProperty.source_file_name), make_simple_macro('sourcename', FormatterProperty.source_file_name),
make_simple_macro('outtype', FormatterProperty.tex_out_type),
ImplementationBeginMacro(),
ImplementationEndMacro(),
ConfigEndMacro(), ConfigEndMacro(),
ConfigBeginMacro(), ConfigBeginMacro(),
MacroCodeBeginMacro(), MacroCodeBeginMacro(),

View file

@ -63,13 +63,18 @@ class DTXFormatter(TexFormatter):
) )
self._shipout_line('%</internal>') self._shipout_line('%</internal>')
self._shipout_line('%') self._shipout_line('%')
self._shipout_line( provides = self._get_provides_text(
'%<{outtype}>{provides}'.format(
outtype=self.config.tex_out_type.value,
provides=self._get_provides_text(
self.config.tex_out_type.value.capitalize() self.config.tex_out_type.value.capitalize()
) )
) parts = provides.split('\n')
parts = [
'%<{outtype}>'.format(
outtype=self.config.tex_out_type.value
) + part
for part in parts
]
self._shipout_line(
'\n'.join(parts)
) )
self._shipout_line('%') self._shipout_line('%')
self._shipout_line('%<*driver>') self._shipout_line('%<*driver>')
@ -92,6 +97,10 @@ class DTXFormatter(TexFormatter):
def _post_process_line(self, line: str) -> str: def _post_process_line(self, line: str) -> str:
line = line.rstrip(' %\n') line = line.rstrip(' %\n')
if self.mode == FormatterMode.meta:
line = line.lstrip('%')
if line.startswith(' '):
line = line[1:]
if self.mode == FormatterMode.meta: if self.mode == FormatterMode.meta:
line = '% ' + line line = '% ' + line
if self.mode == FormatterMode.macrocode: if self.mode == FormatterMode.macrocode:

View file

@ -135,7 +135,8 @@ class FormatterProperty(MacroReplacementAtomIF, Enum):
pytex_commit = 'pytex_commit' pytex_commit = 'pytex_commit'
pytex_dirty = 'pytex_dirty' pytex_dirty = 'pytex_dirty'
tex_type = 'tex_type' tex_type = 'tex_type'
Tex_type= 'Tex_type' Tex_type = 'Tex_type'
tex_out_type = 'tex_outtype'
tex_flavour = 'latex_flavour' tex_flavour = 'latex_flavour'
description = 'description' description = 'description'

View file

@ -113,11 +113,15 @@ class SingleLineMacro(Macro, ABC):
self.strip = strip self.strip = strip
@abstractmethod @abstractmethod
def _apply(self, line, formatter) -> Union[str, List[str]]: def _apply(self, line, formatter) -> Union[Union[str, List[str]], Tuple[Union[str, List[str]], bool]]:
pass pass
def apply(self, line: str, formatter) -> Tuple[Union[str, List[str]], bool]: def apply(self, line: str, formatter) -> Tuple[Union[str, List[str]], bool]:
return self._apply(line, formatter), True replacement = self._apply(line, formatter)
if isinstance(replacement, tuple):
return replacement
else:
return replacement, True
@abstractmethod @abstractmethod
def _matches(self, line: str) -> Optional[str]: def _matches(self, line: str) -> Optional[str]:
@ -194,6 +198,39 @@ class ConfigEndMacro(SimpleSingleLineMacro):
return [] return []
class ImplementationBeginMacro(SimpleSingleLineMacro):
def __init__(self):
super(ImplementationBeginMacro, self).__init__(FORMATTER_PREFIX + IMPLEMENTATION_BEGIN_MACRO)
def _apply(self, line, formatter) -> Tuple[Union[str, List[str]], bool]:
return [
r'% \begin{implementation}',
r'',
r'\section{\pkg{!name} implementation}',
r'\begin{macrocode}',
r'<*!outtype>',
r'\end{macrocode}',
r'',
r'\begin{macrocode}',
r'<@@=!!>',
r'\end{macrocode}',
], False
class ImplementationEndMacro(SimpleSingleLineMacro):
def __init__(self):
super(ImplementationEndMacro, self).__init__(FORMATTER_PREFIX + IMPLEMENTATION_END_MACRO)
def _apply(self, line, formatter) -> Tuple[Union[str, List[str]], bool]:
return [
r'\begin{macrocode}',
r'</!outtype>',
r'\end{macrocode}',
r'',
r'% \end{implementation}'
], False
class MacroCodeBeginMacro(SimpleSingleLineMacro): class MacroCodeBeginMacro(SimpleSingleLineMacro):
def __init__(self): def __init__(self):
super(MacroCodeBeginMacro, self).__init__(r'\begin{macrocode}') super(MacroCodeBeginMacro, self).__init__(r'\begin{macrocode}')

View file

@ -154,7 +154,8 @@ class PyTeXFormatter(FormatterIF, ABC):
FormatterProperty.tex_flavour.value: self.config.tex_flavour.value, FormatterProperty.tex_flavour.value: self.config.tex_flavour.value,
FormatterProperty.file_prefix.value: self.file_prefix, FormatterProperty.file_prefix.value: self.file_prefix,
FormatterProperty.description.value: self.config.description, FormatterProperty.description.value: self.config.description,
FormatterProperty.Tex_type.value: self.config.tex_type.value.capitalize() FormatterProperty.Tex_type.value: self.config.tex_type.value.capitalize(),
FormatterProperty.tex_out_type.value: self.config.tex_out_type.value,
} }
@property @property

View file

@ -123,8 +123,9 @@ class TexFormatter(PyTeXFormatter, ABC):
if isinstance(replacement, str): if isinstance(replacement, str):
self._shipout_line(replacement) self._shipout_line(replacement)
else: else:
for line in replacement: if len(replacement) >= 1:
self._shipout_line(line) self._shipout_line(replacement[0])
self._line_stream.push_lines(replacement[1:])
else: else:
if isinstance(replacement, str): if isinstance(replacement, str):
self.line_stream.set_line(replacement) self.line_stream.set_line(replacement)