From 315e3647ab7279084c0662801eefb611fda957c8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maximilian=20Ke=C3=9Fler?= Date: Fri, 17 Dec 2021 15:09:04 +0100 Subject: [PATCH 1/3] handle package naming style Option --name can now be set to either raw or prepend-author this controls the derivation of the name of the formatted package from the source file read in. --- build/utils/pytex_file.py | 8 +++++--- default_formatters/class_formatter.py | 5 +++-- default_formatters/package_formatter.py | 5 +++-- formatter/tex_formatter.py | 12 +++++++++--- 4 files changed, 20 insertions(+), 10 deletions(-) diff --git a/build/utils/pytex_file.py b/build/utils/pytex_file.py index 42a5758..044b840 100644 --- a/build/utils/pytex_file.py +++ b/build/utils/pytex_file.py @@ -21,7 +21,7 @@ class TexFileToFormat: build_all: bool = False): self.src_path = src_path self.build_path = build_dir - self.tex_name = latex_name # Still an identifier on how to name the package when being formatted + self.latex_name = latex_name # Still an identifier on how to name the package when being formatted self.current_build_info = current_build_info self.last_build_info = last_build_info self.allow_dirty = allow_dirty @@ -80,12 +80,14 @@ class TexFileToFormat: formatter = PackageFormatter( package_name=self.src_path.with_suffix('').name, author=self.current_build_info.author, - extra_header=self._header) + extra_header=self._header, + latex_name=self.latex_name) elif '.pycls' in self.src_path.name: formatter = ClassFormatter( class_name=self.src_path.with_suffix('').name, author=self.current_build_info.author, - extra_header=self._header) + extra_header=self._header, + latex_name=self.latex_name) else: raise Exception('Programming error. Please contact the developer.') pytex_msg('Writing file {}'.format(formatter.file_name)) diff --git a/default_formatters/class_formatter.py b/default_formatters/class_formatter.py index c93a8a4..3e8c541 100644 --- a/default_formatters/class_formatter.py +++ b/default_formatters/class_formatter.py @@ -4,8 +4,9 @@ import PyTeX.macros class ClassFormatter(PyTeX.formatter.TexFormatter): - def __init__(self, class_name: str, author: str, extra_header: [str] = []): - PyTeX.formatter.TexFormatter.__init__(self, class_name, author, extra_header, '.cls') + def __init__(self, class_name: str, author: str, extra_header: [str] = [], + latex_name: str = 'prepend-author'): + PyTeX.formatter.TexFormatter.__init__(self, class_name, author, extra_header, '.cls', latex_name) def make_default_macros(self): PyTeX.macros.make_default_macros(self, 'class') diff --git a/default_formatters/package_formatter.py b/default_formatters/package_formatter.py index 774e4ab..afb7555 100644 --- a/default_formatters/package_formatter.py +++ b/default_formatters/package_formatter.py @@ -4,8 +4,9 @@ import PyTeX.macros class PackageFormatter(PyTeX.formatter.TexFormatter): - def __init__(self, package_name: str, author: str, extra_header: [str] = []): - PyTeX.formatter.TexFormatter.__init__(self, package_name, author, extra_header, '.sty') + def __init__(self, package_name: str, author: str, extra_header: [str] = [], + latex_name: str = 'prepend-author'): + PyTeX.formatter.TexFormatter.__init__(self, package_name, author, extra_header, '.sty', latex_name) def make_default_macros(self): PyTeX.macros.make_default_macros(self, 'package') diff --git a/formatter/tex_formatter.py b/formatter/tex_formatter.py index 60ca700..79ff550 100644 --- a/formatter/tex_formatter.py +++ b/formatter/tex_formatter.py @@ -8,14 +8,20 @@ from PyTeX.base import Attributes, Args class TexFormatter: - def __init__(self, name: str, author: str, header: Optional[List[str]], file_extension: str): + def __init__(self, name: str, author: str, header: Optional[List[str]], file_extension: str, + latex_name: str): self.header = header self.name_raw = name self.author = author + self.latex_name = latex_name author_parts = self.author.lower().replace('ß', 'ss').split(' ') self.author_acronym = author_parts[0][0] + author_parts[-1] - self.name_lowercase = r'{prefix}-{name}'.format(prefix=self.author_acronym, - name=self.name_raw.lower().strip().replace(' ', '-')) + if self.latex_name == 'prepend-author': + self.name_lowercase = r'{prefix}-{name}'.format(prefix=self.author_acronym, + name=self.name_raw.lower().strip().replace(' ', '-')) + else: + self.name_lowercase = self.name_raw.lower().strip().replace(' ', '-') + self.prefix = self.name_lowercase.replace('-', '@') + '@' self.file_name = self.name_lowercase + file_extension self.date = datetime.now().strftime('%Y/%m/%d') From 4c5efa40872d13e33cb9047dac6a2c5133ce234f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maximilian=20Ke=C3=9Fler?= Date: Fri, 17 Dec 2021 15:26:28 +0100 Subject: [PATCH 2/3] fix error when header is empty catch case of empty header to not crash --- build/utils/pytex_file.py | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/build/utils/pytex_file.py b/build/utils/pytex_file.py index 044b840..47d806a 100644 --- a/build/utils/pytex_file.py +++ b/build/utils/pytex_file.py @@ -68,11 +68,12 @@ class TexFileToFormat: def __format_header(self): new_header = [] - for line in self.current_build_info.header: - new_header.append(line.format( - source_file=self.src_path.name, - latex_file_type='package' if '.pysty' in self.src_path.name else 'class' - )) + if self.current_build_info.header: + for line in self.current_build_info.header: + new_header.append(line.format( + source_file=self.src_path.name, + latex_file_type='package' if '.pysty' in self.src_path.name else 'class' + )) self._header = new_header def __format(self) -> dict: From e55a6728a9cebda9077f6e05954333f8ecaf352b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maximilian=20Ke=C3=9Fler?= Date: Fri, 17 Dec 2021 15:34:59 +0100 Subject: [PATCH 3/3] fix handling extra header correctly --- build/utils/build_information.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/build/utils/build_information.py b/build/utils/build_information.py index 769eabe..cfe1270 100644 --- a/build/utils/build_information.py +++ b/build/utils/build_information.py @@ -96,7 +96,8 @@ class BuildInfo: or include_pytex_info_text or include_timestamp or include_pytex_version - or include_git_version): + or include_git_version + or extra_header): self._header = None return else: @@ -113,7 +114,8 @@ class BuildInfo: self._header += PYTEX_VERSION if include_git_version: self._header += SOURCE_CODE_VERSION - self._header += [''] + if len(self._header) > 0: + self._header += [''] if extra_header: self._header += extra_header + ['']