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,44 +3,11 @@ 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'):
header = '\\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,
Type=latex_file_type.capitalize(),
)
formatter.add_replacement('{Type} name'.format(Type=latex_file_type), '{}', PyTeX.base.Attributes.name_lowercase) 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('{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('date', '{}', PyTeX.base.Attributes.date)
formatter.add_replacement('author', '{}', PyTeX.base.Attributes.author) 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}}}', formatter.add_arg_replacement(1, 'info', r'\{Type}Info{{{name}}}{{{info}}}',
name=PyTeX.base.Attributes.name_lowercase, name=PyTeX.base.Attributes.name_lowercase,
info=PyTeX.base.Args.one, Type=latex_file_type.capitalize()) info=PyTeX.base.Args.one, Type=latex_file_type.capitalize())
@ -50,11 +17,57 @@ def make_default_macros(formatter: PyTeX.formatter.TexFormatter, latex_file_type
formatter.add_arg_replacement(1, 'error', r'\{Type}Error{{{name}}}{{{error}}}', formatter.add_arg_replacement(1, 'error', r'\{Type}Error{{{name}}}{{{error}}}',
name=PyTeX.base.Attributes.name_lowercase, error=PyTeX.base.Args.one, name=PyTeX.base.Attributes.name_lowercase, error=PyTeX.base.Args.one,
Type=latex_file_type.capitalize()) Type=latex_file_type.capitalize())
formatter.add_replacement('end options x', formatter.add_replacement('file name', '{name}', name=PyTeX.base.Attributes.file_name)
r"\DeclareOptionX*{{\{Type}Warning{{{name_lowercase}}}" formatter.add_arg_replacement(1, '{Type} macro'.format(Type=latex_file_type), r'\{}{}',
r"{{Unknown '\CurrentOption'}}}}" + '\n' + r'\ProcessOptionsX*\relax' + '\n', PyTeX.base.Attributes.prefix, PyTeX.base.Args.one)
name_lowercase=PyTeX.base.Attributes.name_lowercase, Type=latex_file_type.capitalize())
formatter.add_replacement('end options', if tex_version == 'LaTeX3':
r"\DeclareOption*{{\{Type}Warning{{{name_lowercase}}}" header = r'\ProvidesExpl{Type}{{{name_lowercase}}}{{{date}}}{{{version}}}{{{description}}}' + '\n\n'
r"{{Unknown '\CurrentOption'}}}}" + '\n' + r'\ProcessOptions\relax' + '\n', formatter.add_arg_replacement(
name_lowercase=PyTeX.base.Attributes.name_lowercase, Type=latex_file_type.capitalize()) 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' \
'\\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,
Type=latex_file_type.capitalize(),
)
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_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())