start adding support for formatting latex3 files

This commit is contained in:
Maximilian Keßler 2022-01-11 18:39:56 +01:00
parent 494c43e52b
commit 23766e8dcd
5 changed files with 85 additions and 51 deletions

View file

@ -11,6 +11,7 @@ class Attributes(Enum):
date = 'date' date = 'date'
year = 'year' year = 'year'
source_file_name = 'source_file_name' source_file_name = 'source_file_name'
version = 'package_version'
class Args(Enum): class Args(Enum):

View file

@ -4,8 +4,14 @@ import PyTeX.macros
class ClassFormatter(PyTeX.formatter.TexFormatter): class ClassFormatter(PyTeX.formatter.TexFormatter):
def __init__(self, class_name: str, author: str, extra_header: [str] = []): def __init__(self, class_name: str, author: str, extra_header: [str] = [], tex_version: str = 'LaTeX2e'):
PyTeX.formatter.TexFormatter.__init__(self, class_name, author, extra_header, '.cls') PyTeX.formatter.TexFormatter.__init__(
self,
name=class_name,
author=author,
header=extra_header,
file_extension='.cls',
tex_version=tex_version)
def make_default_macros(self): def make_default_macros(self):
PyTeX.macros.make_default_macros(self, 'class') PyTeX.macros.make_default_macros(self, 'class')

View file

@ -4,8 +4,15 @@ import PyTeX.macros
class PackageFormatter(PyTeX.formatter.TexFormatter): class PackageFormatter(PyTeX.formatter.TexFormatter):
def __init__(self, package_name: str, author: str, extra_header: [str] = []): def __init__(self, package_name: str, author: str, extra_header: [str] = [], tex_version: str = 'LaTeX2e'):
PyTeX.formatter.TexFormatter.__init__(self, package_name, author, extra_header, '.sty') PyTeX.formatter.TexFormatter.__init__(
self,
name=package_name,
author=author,
header=extra_header,
file_extension='.sty',
tex_version=tex_version
)
def make_default_macros(self): def make_default_macros(self):
PyTeX.macros.make_default_macros(self, 'package') PyTeX.macros.make_default_macros(self, 'package')

View file

@ -8,21 +8,28 @@ from PyTeX.base import Attributes, Args
class TexFormatter: 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,
tex_version: str, version: str):
self.version = version
self.header = header self.header = header
self.name_raw = name self.name_raw = name
self.author = author self.author = author
author_parts = self.author.lower().replace('ß', 'ss').split(' ') author_parts = self.author.lower().replace('ß', 'ss').split(' ')
self.author_acronym = author_parts[0][0] + author_parts[-1] 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(' ', '-'))
self.prefix = self.name_lowercase.replace('-', '@') + '@'
self.file_name = self.name_lowercase + file_extension
self.date = datetime.now().strftime('%Y/%m/%d') self.date = datetime.now().strftime('%Y/%m/%d')
self.year = int(datetime.now().strftime('%Y')) self.year = int(datetime.now().strftime('%Y'))
self.replace_dict: Dict = {} self.replace_dict: Dict = {}
self.arg_replace_dict: Dict = {} self.arg_replace_dict: Dict = {}
self.source_file_name = "not specified" self.source_file_name = "not specified"
self.name_lowercase = r'{prefix}-{name}'.format(prefix=self.author_acronym,
name=self.name_raw.lower().strip().replace(' ', '-'))
self.file_name = self.name_lowercase + file_extension
if tex_version == 'LaTeX2e':
self.prefix = self.name_lowercase.replace('-', '@') + '@'
elif tex_version == 'LaTeX3':
self.prefix = '__' + self.name_lowercase.replace('-', '_') + '_'
else:
raise Exception('Unknown TeX version')
@staticmethod @staticmethod
def __command_name2keyword(keyword: str): def __command_name2keyword(keyword: str):

View file

@ -3,7 +3,36 @@ import PyTeX.base
import PyTeX.config import PyTeX.config
def make_default_macros(formatter: PyTeX.formatter.TexFormatter, latex_file_type: str): def make_default_macros(formatter: PyTeX.formatter.TexFormatter, latex_file_type: str, tex_version: str = 'LaTeX2e'):
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_replacement('date', '{}', PyTeX.base.Attributes.date)
formatter.add_replacement('author', '{}', PyTeX.base.Attributes.author)
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('file name', '{name}', name=PyTeX.base.Attributes.file_name)
formatter.add_arg_replacement(1, '{Type} macro'.format(Type=latex_file_type), r'\{}{}',
PyTeX.base.Attributes.prefix, PyTeX.base.Args.one)
if tex_version == 'LaTeX3':
header = r'\ProvidesExpl{Type}{{{name_lowercase}}}{{{date}}}{{{version}}}{{{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,
Type=latex_file_type.capitalize(),
version=PyTeX.base.Attributes.version
)
elif tex_version == 'LaTeX2e':
header = '\\NeedsTeXFormat{{LaTeX2e}}\n' \ header = '\\NeedsTeXFormat{{LaTeX2e}}\n' \
'\\Provides{Type}{{{name_lowercase}}}[{date} - {description}]\n\n' '\\Provides{Type}{{{name_lowercase}}}[{date} - {description}]\n\n'
formatter.add_arg_replacement( formatter.add_arg_replacement(
@ -14,13 +43,6 @@ def make_default_macros(formatter: PyTeX.formatter.TexFormatter, latex_file_type
description=PyTeX.base.Args.one, description=PyTeX.base.Args.one,
Type=latex_file_type.capitalize(), Type=latex_file_type.capitalize(),
) )
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}', 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, prefix=PyTeX.base.Attributes.prefix, condition=PyTeX.base.Args.one,
value=PyTeX.base.Args.two) value=PyTeX.base.Args.two)
@ -41,15 +63,6 @@ def make_default_macros(formatter: PyTeX.formatter.TexFormatter, latex_file_type
r'\DeclareOption{{ngerman}}{{\{prefix}englishfalse}}' + '\n' + r'\DeclareOption{{ngerman}}{{\{prefix}englishfalse}}' + '\n' +
r'\DeclareOption{{english}}{{\{prefix}englishtrue}}', r'\DeclareOption{{english}}{{\{prefix}englishtrue}}',
prefix=PyTeX.base.Attributes.prefix) 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', formatter.add_replacement('end options x',
r"\DeclareOptionX*{{\{Type}Warning{{{name_lowercase}}}" r"\DeclareOptionX*{{\{Type}Warning{{{name_lowercase}}}"
r"{{Unknown '\CurrentOption'}}}}" + '\n' + r'\ProcessOptionsX*\relax' + '\n', r"{{Unknown '\CurrentOption'}}}}" + '\n' + r'\ProcessOptionsX*\relax' + '\n',