lots of default macros

This commit is contained in:
Maximilian Keßler 2022-02-09 21:11:46 +01:00
parent a50c4935be
commit f1dbc45f9c
6 changed files with 110 additions and 23 deletions

View file

@ -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

View file

@ -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

View file

@ -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'

View file

@ -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:

View file

@ -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

View file

@ -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
)
)
)