Merge branch 'latex-packages'

Formatting project correctly as a package
This commit is contained in:
Maximilian Keßler 2021-10-18 15:09:21 +02:00
commit ba0b49dbfc
15 changed files with 149 additions and 109 deletions

2
.gitignore vendored
View File

@ -1,4 +1,4 @@
__pycache__/*
**/__pycache__
.idea/*
main.py
test.py

6
__init__.py Normal file
View File

@ -0,0 +1,6 @@
from PyTeX.default_formatters import ClassFormatter, PackageFormatter
__all__ = [
"ClassFormatter",
"PackageFormatter"
]

11
base/__init__.py Normal file
View File

@ -0,0 +1,11 @@
from .config import LICENSE, DEFAULT_AUTHOR, PACKAGE_INFO_TEXT, PYTEX_INFO_TEXT
from .enums import Attributes, Args
__all__ = [
'LICENSE',
'DEFAULT_AUTHOR',
'PACKAGE_INFO_TEXT',
'PYTEX_INFO_TEXT',
'Args',
'Attributes'
]

View File

@ -1,11 +0,0 @@
from formatter import Formatter
from config import DEFAULT_AUTHOR
from replacements import make_default_commands
class ClassFormatter(Formatter):
def __init__(self, class_name: str, author: str = DEFAULT_AUTHOR, extra_header: str = ''):
Formatter.__init__(self, class_name, author, extra_header, '.cls')
def make_default_macros(self):
make_default_commands(self, 'class')

View File

@ -0,0 +1,7 @@
from .class_formatter import ClassFormatter
from .package_formatter import PackageFormatter
__all__ = [
'PackageFormatter',
'ClassFormatter'
]

View File

@ -0,0 +1,11 @@
import PyTeX.formatter
import PyTeX.base
import PyTeX.macros
class ClassFormatter(PyTeX.formatter.TexFormatter):
def __init__(self, class_name: str, author: str = PyTeX.base.DEFAULT_AUTHOR, extra_header: [str] = []):
PyTeX.formatter.TexFormatter.__init__(self, class_name, author, extra_header, '.cls')
def make_default_macros(self):
PyTeX.macros.make_default_macros(self, 'class')

View File

@ -0,0 +1,11 @@
import PyTeX.formatter
import PyTeX.base
import PyTeX.macros
class PackageFormatter(PyTeX.formatter.TexFormatter):
def __init__(self, package_name: str, author: str = PyTeX.base.DEFAULT_AUTHOR, extra_header: [str] = []):
PyTeX.formatter.TexFormatter.__init__(self, package_name, author, extra_header, '.cls')
def make_default_macros(self):
PyTeX.macros.make_default_macros(self, 'package')

5
formatter/__init__.py Normal file
View File

@ -0,0 +1,5 @@
from .tex_formatter import TexFormatter
__all__ = [
'TexFormatter'
]

View File

@ -3,11 +3,12 @@ import re
from pathlib import Path
from typing import Dict
from datetime import *
from enums import Attributes, Args
from PyTeX.base import Attributes, Args
class Formatter:
def __init__(self, name: str, author: str, extra_header: str, file_extension: str):
class TexFormatter:
def __init__(self, name: str, author: str, extra_header: [str], file_extension: str):
self.extra_header = extra_header
self.name_raw = name
self.author = author
@ -24,10 +25,10 @@ class Formatter:
self.source_file_name = "not specified"
@staticmethod
def command_name2keyword(keyword: str):
def __command_name2keyword(keyword: str):
return '__' + keyword.upper().strip().replace(' ', '_') + '__'
def parse_replacement_args(self, match_groups, *user_args, **user_kwargs):
def __parse_replacement_args(self, match_groups, *user_args, **user_kwargs):
new_args = []
for arg in user_args:
if type(arg) == Attributes:
@ -51,24 +52,12 @@ class Formatter:
new_kwargs[kw] = 'ERROR'
return new_args, new_kwargs
def add_replacement(self, keyword: str, replacement: str, *args, **kwargs):
args, kwargs = self.parse_replacement_args([], *args, **kwargs)
self.replace_dict[self.command_name2keyword(keyword)] = replacement.format(*args, **kwargs)
def add_arg_replacement(self, num_args: int, keyword: str, replacement: str, *args, **kwargs):
self.arg_replace_dict[self.command_name2keyword(keyword)] = {
'num_args': num_args,
'replacement': replacement,
'format_args': args,
'format_kwargs': kwargs
}
def format_string(self, contents: str) -> str:
def __format_string(self, contents: str) -> str:
for key in self.replace_dict.keys():
contents = contents.replace(key, self.replace_dict[key])
return contents
def format_string_with_arg(self, contents: str) -> str:
def __format_string_with_arg(self, contents: str) -> str:
for command in self.arg_replace_dict.keys():
search_regex = re.compile(r'{keyword}\({arguments}(?<!@)\)'.format(
keyword=command,
@ -76,7 +65,7 @@ class Formatter:
))
match = re.search(search_regex, contents)
while match is not None:
format_args, format_kwargs = self.parse_replacement_args(
format_args, format_kwargs = self.__parse_replacement_args(
list(map(lambda group: group.replace('@)', ')'), match.groups())),
*self.arg_replace_dict[command]['format_args'],
**self.arg_replace_dict[command]['format_kwargs']
@ -88,13 +77,25 @@ class Formatter:
match = re.search(search_regex, contents)
return contents
def add_replacement(self, keyword: str, replacement: str, *args, **kwargs):
args, kwargs = self.__parse_replacement_args([], *args, **kwargs)
self.replace_dict[self.__command_name2keyword(keyword)] = replacement.format(*args, **kwargs)
def add_arg_replacement(self, num_args: int, keyword: str, replacement: str, *args, **kwargs):
self.arg_replace_dict[self.__command_name2keyword(keyword)] = {
'num_args': num_args,
'replacement': replacement,
'format_args': args,
'format_kwargs': kwargs
}
def format_file(self, input_path: Path, output_dir: Path = None):
self.source_file_name = str(input_path.name)
input_file = input_path.open()
lines = input_file.readlines()
newlines = []
for line in lines:
newlines += self.format_string_with_arg(self.format_string(line))
newlines += self.__format_string_with_arg(self.__format_string(line))
if output_dir is None:
output_dir = input_path.parent
output_dir.mkdir(parents=True, exist_ok=True)

5
macros/__init__.py Normal file
View File

@ -0,0 +1,5 @@
from .default_macros import make_default_macros
__all__ = [
'make_default_macros'
]

70
macros/default_macros.py Normal file
View File

@ -0,0 +1,70 @@
import PyTeX.formatter
import PyTeX.base
def make_default_macros(formatter: PyTeX.formatter.TexFormatter, latex_file_type: str):
header = '%' * 80 + '\n' \
+ '\n'.join(map(lambda line: '% ' + line,
PyTeX.base.LICENSE + [''] + PyTeX.base.PACKAGE_INFO_TEXT + [
''] + PyTeX.base.PYTEX_INFO_TEXT
+ [''] + formatter.extra_header)
) \
+ '\n' + '%' * 80 + '\n\n' \
+ '\\NeedsTeXFormat{{LaTeX2e}}\n' \
'\\Provides{Type}{{{name_lowercase}}}[{date} - {description}]\n\n'
formatter.add_arg_replacement(
1, 'header',
header,
name_lowercase=PyTeX.base.Attributes.name_lowercase,
date=PyTeX.base.Attributes.date,
description=PyTeX.base.Args.one,
year=PyTeX.base.Attributes.year,
copyright_holders=PyTeX.base.Attributes.author,
source_file=PyTeX.base.Attributes.source_file_name,
Type=latex_file_type.capitalize(),
latex_file_type=latex_file_type
)
formatter.add_replacement('{Type} name'.format(Type=latex_file_type), '{}', PyTeX.base.Attributes.name_lowercase)
formatter.add_replacement('{Type} prefix'.format(Type=latex_file_type), '{}', PyTeX.base.Attributes.prefix)
formatter.add_arg_replacement(1, '{Type} macro'.format(Type=latex_file_type), r'\{}{}',
PyTeX.base.Attributes.prefix, PyTeX.base.Args.one)
formatter.add_replacement('file name', '{name}', name=PyTeX.base.Attributes.file_name)
formatter.add_replacement('date', '{}', PyTeX.base.Attributes.date)
formatter.add_replacement('author', '{}', PyTeX.base.Attributes.author)
formatter.add_arg_replacement(2, 'new if', r'\newif\if{prefix}{condition}\{prefix}{condition}{value}',
prefix=PyTeX.base.Attributes.prefix, condition=PyTeX.base.Args.one,
value=PyTeX.base.Args.two)
formatter.add_arg_replacement(2, 'set if', r'\{prefix}{condition}{value}',
prefix=PyTeX.base.Attributes.prefix, condition=PyTeX.base.Args.one,
value=PyTeX.base.Args.two)
formatter.add_arg_replacement(1, 'if', r'\if{prefix}{condition}', prefix=PyTeX.base.Attributes.prefix,
condition=PyTeX.base.Args.one)
formatter.add_replacement('language options x',
r'\newif\if{prefix}english\{prefix}englishtrue' + '\n' +
r'\DeclareOptionX{{german}}{{\{prefix}englishfalse}}' + '\n' +
r'\DeclareOptionX{{ngerman}}{{\{prefix}englishfalse}}' + '\n' +
r'\DeclareOptionX{{english}}{{\{prefix}englishtrue}}',
prefix=PyTeX.base.Attributes.prefix)
formatter.add_replacement('language options',
r'\newif\if{prefix}english\{prefix}englishtrue' + '\n' +
r'\DeclareOption{{german}}{{\{prefix}englishfalse}}' + '\n' +
r'\DeclareOption{{ngerman}}{{\{prefix}englishfalse}}' + '\n' +
r'\DeclareOption{{english}}{{\{prefix}englishtrue}}',
prefix=PyTeX.base.Attributes.prefix)
formatter.add_arg_replacement(1, 'info', r'\{Type}Info{{{name}}}{{{info}}}',
name=PyTeX.base.Attributes.name_lowercase,
info=PyTeX.base.Args.one, Type=latex_file_type.capitalize())
formatter.add_arg_replacement(1, 'warning', r'\{Type}Warning{{{name}}}{{{warning}}}',
name=PyTeX.base.Attributes.name_lowercase, warning=PyTeX.base.Args.one,
Type=latex_file_type.capitalize())
formatter.add_arg_replacement(1, 'error', r'\{Type}Error{{{name}}}{{{error}}}',
name=PyTeX.base.Attributes.name_lowercase, error=PyTeX.base.Args.one,
Type=latex_file_type.capitalize())
formatter.add_replacement('end options x',
r"\DeclareOptionX*{{\{Type}Warning{{{name_lowercase}}}"
r"{{Unknown '\CurrentOption'}}}}" + '\n' + r'\ProcessOptionsX*\relax' + '\n',
name_lowercase=PyTeX.base.Attributes.name_lowercase, Type=latex_file_type.capitalize())
formatter.add_replacement('end options',
r"\DeclareOption*{{\{Type}Warning{{{name_lowercase}}}"
r"{{Unknown '\CurrentOption'}}}}" + '\n' + r'\ProcessOptions\relax' + '\n',
name_lowercase=PyTeX.base.Attributes.name_lowercase, Type=latex_file_type.capitalize())

View File

@ -1,12 +0,0 @@
from formatter import Formatter
from config import DEFAULT_AUTHOR
from replacements import make_default_commands
class PackageFormatter(Formatter):
def __init__(self, package_name: str, author: str = DEFAULT_AUTHOR, extra_header: str = ''):
Formatter.__init__(self, package_name, author, extra_header, '.sty')
def make_default_macros(self):
make_default_commands(self, 'package')

View File

@ -1,64 +0,0 @@
from enums import Attributes, Args
from formatter import Formatter
from config import LICENSE, PACKAGE_INFO_TEXT, PYTEX_INFO_TEXT
def make_default_commands(formatter: Formatter, latex_file_type: str):
header = '%' * 80 + '\n' \
+ '\n'.join(map(lambda line: '% ' + line,
LICENSE + [''] + PACKAGE_INFO_TEXT + [''] + PYTEX_INFO_TEXT
+ [''] + formatter.extra_header)
) \
+ '\n' + '%' * 80 + '\n\n' \
+ '\\NeedsTeXFormat{{LaTeX2e}}\n' \
'\\Provides{Type}{{{name_lowercase}}}[{date} - {description}]\n\n'
formatter.add_arg_replacement(
1, 'header',
header,
name_lowercase=Attributes.name_lowercase,
date=Attributes.date,
description=Args.one,
year=Attributes.year,
copyright_holders=Attributes.author,
source_file=Attributes.source_file_name,
Type=latex_file_type.capitalize(),
latex_file_type=latex_file_type
)
formatter.add_replacement('{Type} name'.format(Type=latex_file_type), '{}', Attributes.name_lowercase)
formatter.add_replacement('{Type} prefix'.format(Type=latex_file_type), '{}', Attributes.prefix)
formatter.add_arg_replacement(1, '{Type} macro'.format(Type=latex_file_type), r'\{}{}', Attributes.prefix, Args.one)
formatter.add_replacement('file name', '{name}', name=Attributes.file_name)
formatter.add_replacement('date', '{}', Attributes.date)
formatter.add_replacement('author', '{}', Attributes.author)
formatter.add_arg_replacement(2, 'new if', r'\newif\if{prefix}{condition}\{prefix}{condition}{value}',
prefix=Attributes.prefix, condition=Args.one, value=Args.two)
formatter.add_arg_replacement(2, 'set if', r'\{prefix}{condition}{value}',
prefix=Attributes.prefix, condition=Args.one, value=Args.two)
formatter.add_arg_replacement(1, 'if', r'\if{prefix}{condition}', prefix=Attributes.prefix,
condition=Args.one)
formatter.add_replacement('language options x',
r'\newif\if{prefix}english\{prefix}englishtrue' + '\n' +
r'\DeclareOptionX{{german}}{{\{prefix}englishfalse}}' + '\n' +
r'\DeclareOptionX{{ngerman}}{{\{prefix}englishfalse}}' + '\n' +
r'\DeclareOptionX{{english}}{{\{prefix}englishtrue}}',
prefix=Attributes.prefix)
formatter.add_replacement('language options',
r'\newif\if{prefix}english\{prefix}englishtrue' + '\n' +
r'\DeclareOption{{german}}{{\{prefix}englishfalse}}' + '\n' +
r'\DeclareOption{{ngerman}}{{\{prefix}englishfalse}}' + '\n' +
r'\DeclareOption{{english}}{{\{prefix}englishtrue}}',
prefix=Attributes.prefix)
formatter.add_arg_replacement(1, 'info', r'\{Type}Info{{{name}}}{{{info}}}', name=Attributes.name_lowercase,
info=Args.one, Type=latex_file_type.capitalize())
formatter.add_arg_replacement(1, 'warning', r'\{Type}Warning{{{name}}}{{{warning}}}',
name=Attributes.name_lowercase, warning=Args.one, Type=latex_file_type.capitalize())
formatter.add_arg_replacement(1, 'error', r'\{Type}Error{{{name}}}{{{error}}}',
name=Attributes.name_lowercase, error=Args.one, Type=latex_file_type.capitalize())
formatter.add_replacement('end options x',
r"\DeclareOptionX*{{\{Type}Warning{{{name_lowercase}}}"
r"{{Unknown '\CurrentOption'}}}}" + '\n' + r'\ProcessOptionsX*\relax' + '\n',
name_lowercase=Attributes.name_lowercase, Type=latex_file_type.capitalize())
formatter.add_replacement('end options',
r"\DeclareOption*{{\{Type}Warning{{{name_lowercase}}}"
r"{{Unknown '\CurrentOption'}}}}" + '\n' + r'\ProcessOptions\relax' + '\n',
name_lowercase=Attributes.name_lowercase, Type=latex_file_type.capitalize())