54 lines
1.8 KiB
Python
54 lines
1.8 KiB
Python
from pathlib import Path
|
|
from typing import Optional, Dict, Union, Type
|
|
|
|
from .formatting_config import FormattingConfig
|
|
|
|
from .dict_formatter import DictFormatter
|
|
from .simple_tex_formatter import SimpleTeXFormatter
|
|
from .dtx_formatter import DTXFormatter
|
|
from .pytex_formatter import PyTeXFormatter
|
|
from .git_version_info import GitVersionInfo
|
|
from .default_macros import get_default_macros
|
|
from .enums import TeXType
|
|
|
|
|
|
def formatter_from_file_extension(
|
|
input_file: Path,
|
|
config: Optional[FormattingConfig] = None,
|
|
git_version_info: Optional[GitVersionInfo] = None,
|
|
locate_file_config: bool = True,
|
|
allow_infile_config: bool = True,
|
|
default_macros: bool = True,
|
|
) -> PyTeXFormatter:
|
|
switcher: Dict[str, Type[Union[DTXFormatter, SimpleTeXFormatter, DictFormatter]]] = {
|
|
'dtx.pytex': DTXFormatter,
|
|
'sty.pytex': SimpleTeXFormatter,
|
|
'cls.pytex': SimpleTeXFormatter,
|
|
'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
|
|
try:
|
|
[name, extension] = input_file.name.split('.', maxsplit=1)
|
|
except:
|
|
raise NotImplementedError
|
|
|
|
config.tex_type = switcher2[extension] # This sets the textype from file extension
|
|
|
|
formatter = switcher[extension](
|
|
input_file=input_file,
|
|
config=config,
|
|
git_version_info=git_version_info,
|
|
locate_file_config=locate_file_config,
|
|
allow_infile_config=allow_infile_config
|
|
)
|
|
if default_macros:
|
|
formatter.macros = get_default_macros(formatter.config.tex_flavour)
|
|
return formatter
|
|
|
|
|