correct file extension handling for simple tex formatters

This commit is contained in:
Maximilian Keßler 2022-02-09 17:42:56 +01:00
parent 7a3fded422
commit 6798962880
3 changed files with 21 additions and 4 deletions

View file

@ -9,7 +9,7 @@ from .dtx_formatter import DTXFormatter
from .pytex_formatter import PyTeXFormatter from .pytex_formatter import PyTeXFormatter
from .git_version_info import GitVersionInfo from .git_version_info import GitVersionInfo
from .default_macros import get_default_macros from .default_macros import get_default_macros
from .enums import TeXType
def formatter_from_file_extension( def formatter_from_file_extension(
input_file: Path, input_file: Path,
@ -25,12 +25,20 @@ def formatter_from_file_extension(
'cls.pytex': SimpleTeXFormatter, 'cls.pytex': SimpleTeXFormatter,
'dict.pytex': DictFormatter 'dict.pytex': DictFormatter
} }
switcher2: Dict[str, TeXType] = {
'dtx.pytex': TeXType.TeXDocstrip,
'sty.pytex': TeXType.TeXPackage,
'cls.pytex': TeXType.TeXClass,
'dict.pytex': TeXType.TeXDictionary
}
# TODO: other formatters # TODO: other formatters
try: try:
[name, extension] = input_file.name.split('.', maxsplit=1) [name, extension] = input_file.name.split('.', maxsplit=1)
except: except:
raise NotImplementedError raise NotImplementedError
config.tex_type = switcher2[extension] # This sets the textype from file extension
formatter = switcher[extension]( formatter = switcher[extension](
input_file=input_file, input_file=input_file,
config=config, config=config,

View file

@ -12,6 +12,7 @@ from abc import ABC, abstractmethod
from .enums import * from .enums import *
from datetime import * from datetime import *
class PyTeXFormatter(FormatterIF, ABC): class PyTeXFormatter(FormatterIF, ABC):
def __init__( def __init__(
self, self,

View file

@ -2,12 +2,20 @@ from .formatting_config import FormattingConfig
from .tex_formatter import TexFormatter from .tex_formatter import TexFormatter
from pathlib import Path from pathlib import Path
from typing import Optional, List, Tuple, Dict from typing import Optional, List, Tuple, Dict
from .enums import TeXFlavour from .enums import TeXFlavour, TeXType
class SimpleTeXFormatter(TexFormatter): class SimpleTeXFormatter(TexFormatter):
@property
def output_file(self) -> str:
switcher = {
TeXType.TeXClass: '.cls',
TeXType.TeXPackage: '.sty',
}
return self.name + switcher[self.config.tex_type]
def open_output_stream(self, build_dir: Path): def open_output_stream(self, build_dir: Path):
out_file = build_dir / self.input_file.with_suffix('').name out_file = build_dir / self.output_file
if out_file.exists(): if out_file.exists():
raise NotImplementedError raise NotImplementedError
else: else:
@ -26,7 +34,7 @@ class SimpleTeXFormatter(TexFormatter):
@property @property
def output_files(self) -> List[str]: def output_files(self) -> List[str]:
return [self.input_file.with_suffix('').name] return [self.output_file]
def _post_process_line(self, line: str) -> str: def _post_process_line(self, line: str) -> str:
if self.config.tex_flavour == TeXFlavour.LaTeX2e: if self.config.tex_flavour == TeXFlavour.LaTeX2e: