From f1dbc45f9c8fb5e5baf74b314f5b67a42b96fa27 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maximilian=20Ke=C3=9Fler?= Date: Wed, 9 Feb 2022 21:11:46 +0100 Subject: [PATCH] lots of default macros --- PyTeX/format/auto_format.py | 3 +- PyTeX/format/default_macros.py | 83 +++++++++++++++++++++++++++- PyTeX/format/enums.py | 1 + PyTeX/format/formatting_config.py | 4 ++ PyTeX/format/pytex_formatter.py | 3 +- PyTeX/format/simple_tex_formatter.py | 39 ++++++------- 6 files changed, 110 insertions(+), 23 deletions(-) diff --git a/PyTeX/format/auto_format.py b/PyTeX/format/auto_format.py index d4dd30a..6130870 100644 --- a/PyTeX/format/auto_format.py +++ b/PyTeX/format/auto_format.py @@ -11,6 +11,7 @@ from .git_version_info import GitVersionInfo from .default_macros import get_default_macros from .enums import TeXType + def formatter_from_file_extension( input_file: Path, config: Optional[FormattingConfig] = None, @@ -47,7 +48,7 @@ def formatter_from_file_extension( allow_infile_config=allow_infile_config ) if default_macros: - formatter.macros = get_default_macros() + formatter.macros = get_default_macros(formatter.config.tex_flavour) return formatter diff --git a/PyTeX/format/default_macros.py b/PyTeX/format/default_macros.py index d059171..a5c8d9d 100644 --- a/PyTeX/format/default_macros.py +++ b/PyTeX/format/default_macros.py @@ -1,4 +1,5 @@ from .macros import * +from .enums import TeXFlavour, Argument def make_simple_macro(name: str, arg): @@ -11,10 +12,88 @@ def make_simple_macro(name: str, arg): ) -def get_default_macros(): - return [ +def get_default_macros(tex_flavour: TeXFlavour): + both = [ make_simple_macro('!', FormatterProperty.file_prefix), make_simple_macro('name', FormatterProperty.name), + make_simple_macro('author', FormatterProperty.author), + make_simple_macro('date', FormatterProperty.date), + make_simple_macro('year', FormatterProperty.year), + make_simple_macro('shortauthor', FormatterProperty.shortauthor), + make_simple_macro('version', FormatterProperty.version), + make_simple_macro('filename', FormatterProperty.file_name), + make_simple_macro('prefix', FormatterProperty.file_prefix), + make_simple_macro('repoversion', FormatterProperty.repo_version), + make_simple_macro('repobranch', FormatterProperty.repo_branch), + make_simple_macro('repocommit', FormatterProperty.repo_commit), + make_simple_macro('repodirty', FormatterProperty.repo_dirty), + make_simple_macro('sourcename', FormatterProperty.source_file_name), ConfigEndMacro(), ConfigBeginMacro() ] + tex2 = [ + ArgumentMacro( + 'newif', + 2, + MacroReplacement( + r'\newif\if%s@%s\%s@%s%s', + FormatterProperty.file_prefix, + Argument.one, + FormatterProperty.file_prefix, + Argument.one, + Argument.two + ) + ), + ArgumentMacro( + 'setif', + 2, + MacroReplacement( + r'\%s@%s%s', + FormatterProperty.file_prefix, + Argument.one, + Argument.two + ) + ), + ArgumentMacro( + 'if', + 1, + MacroReplacement( + r'\if%s@%s', + FormatterProperty.file_prefix, + Argument.one + ) + ), + ArgumentMacro( + 'header', + 1, + MacroReplacement( + r'\Provides%s{%s}[%s - %s (%s)]', + FormatterProperty.Tex_type, + FormatterProperty.name, + FormatterProperty.date, + Argument.one, + FormatterProperty.version + ) + ) + ] + + tex3 = [ + ArgumentMacro( + 'header', + 1, + MacroReplacement( + '\\ProvidesExpl%s { %s } { %s } { %s }\n { %s }', + FormatterProperty.Tex_type, + FormatterProperty.name, + FormatterProperty.date, + FormatterProperty.version, + FormatterProperty.description + ) + ) + ] + if tex_flavour == TeXFlavour.LaTeX2e: + return tex2 + both + elif tex_flavour == TeXFlavour.LaTeX3: + return tex3 + both + else: + raise NotImplementedError diff --git a/PyTeX/format/enums.py b/PyTeX/format/enums.py index e0f3da3..40857ab 100644 --- a/PyTeX/format/enums.py +++ b/PyTeX/format/enums.py @@ -110,6 +110,7 @@ class FormatterProperty(MacroReplacementAtomIF, Enum): pytex_commit = 'pytex_commit' pytex_dirty = 'pytex_dirty' tex_type = 'tex_type' + Tex_type= 'Tex_type' tex_flavour = 'latex_flavour' description = 'description' diff --git a/PyTeX/format/formatting_config.py b/PyTeX/format/formatting_config.py index 7e54af6..25384db 100644 --- a/PyTeX/format/formatting_config.py +++ b/PyTeX/format/formatting_config.py @@ -134,6 +134,10 @@ class FormattingConfig(Config): } } + @property + def has_description(self) -> bool: + return self._description is not None + @property def naming_scheme(self) -> NamingScheme: if self._naming_scheme is None: diff --git a/PyTeX/format/pytex_formatter.py b/PyTeX/format/pytex_formatter.py index 2c8daf4..e50d2f7 100644 --- a/PyTeX/format/pytex_formatter.py +++ b/PyTeX/format/pytex_formatter.py @@ -153,7 +153,8 @@ class PyTeXFormatter(FormatterIF, ABC): FormatterProperty.tex_type.value: self.config.tex_type.value, FormatterProperty.tex_flavour.value: self.config.tex_flavour.value, 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() } @property diff --git a/PyTeX/format/simple_tex_formatter.py b/PyTeX/format/simple_tex_formatter.py index bf7dd23..64bae86 100644 --- a/PyTeX/format/simple_tex_formatter.py +++ b/PyTeX/format/simple_tex_formatter.py @@ -37,24 +37,25 @@ class SimpleTeXFormatter(TexFormatter): return line.rstrip() def format_post_header(self) -> None: - if self.config.tex_flavour == TeXFlavour.LaTeX2e: - self._shipout_line( - r'\Provides%s{%s}[%s - %s]' - % ( - self.config.tex_type.value.capitalize(), - self.name, - self.attribute_dict[FormatterProperty.date.value], - self.attribute_dict[FormatterProperty.description.value] + if self.config.has_description: + if self.config.tex_flavour == TeXFlavour.LaTeX2e: + self._shipout_line( + r'\Provides%s{%s}[%s - %s]' + % ( + self.config.tex_type.value.capitalize(), + self.name, + self.attribute_dict[FormatterProperty.date.value], + self.attribute_dict[FormatterProperty.description.value] + ) ) - ) - else: - self._shipout_line( - '\\ProvidesExpl%s { %s } { %s } { %s }\n { %s }' - % ( - self.config.tex_type.value.capitalize(), - self.name, - self.attribute_dict[FormatterProperty.date.value], - self.config.version, - self.config.description + else: + self._shipout_line( + '\\ProvidesExpl%s { %s } { %s } { %s }\n { %s }' + % ( + self.config.tex_type.value.capitalize(), + self.name, + self.attribute_dict[FormatterProperty.date.value], + self.config.version, + self.config.description + ) ) - )