2022-02-08 18:26:25 +01:00
|
|
|
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
|
2022-02-09 14:08:51 +01:00
|
|
|
from .git_version_info import GitVersionInfo
|
2022-02-18 16:23:43 +01:00
|
|
|
from .docstrip_formatter import DocStripFormatter
|
|
|
|
from .nothing_formatter import NothingFormatter
|
|
|
|
from .copy_formatter import CopyFormatter
|
2022-02-09 17:22:38 +01:00
|
|
|
from .default_macros import get_default_macros
|
2022-02-18 16:23:43 +01:00
|
|
|
from .enums import TeXType, Target
|
2022-02-08 18:26:25 +01:00
|
|
|
|
2022-02-09 21:11:46 +01:00
|
|
|
|
2022-02-08 18:31:19 +01:00
|
|
|
def formatter_from_file_extension(
|
2022-02-08 18:26:25 +01:00
|
|
|
input_file: Path,
|
|
|
|
config: Optional[FormattingConfig] = None,
|
2022-02-09 14:08:51 +01:00
|
|
|
git_version_info: Optional[GitVersionInfo] = None,
|
2022-02-08 18:26:25 +01:00
|
|
|
locate_file_config: bool = True,
|
2022-02-09 17:22:38 +01:00
|
|
|
allow_infile_config: bool = True,
|
|
|
|
default_macros: bool = True,
|
2022-02-18 16:23:43 +01:00
|
|
|
target: Optional[Target] = None,
|
2022-02-08 18:26:25 +01:00
|
|
|
) -> PyTeXFormatter:
|
2022-02-18 16:23:43 +01:00
|
|
|
|
|
|
|
extension_switcher: Dict[str, TeXType] = {
|
|
|
|
'dtx.pytex': TeXType.TeXDocstrip,
|
|
|
|
'dtx': TeXType.TeXDocstrip,
|
|
|
|
'sty.pytex': TeXType.TeXPackage,
|
|
|
|
'sty': TeXType.TeXPackage,
|
|
|
|
'cls.pytex': TeXType.TeXClass,
|
|
|
|
'cls': TeXType.TeXClass,
|
|
|
|
'dict.pytex': TeXType.TeXDictionary,
|
|
|
|
'dict': TeXType.TeXDictionary,
|
|
|
|
'tex.pytex': TeXType.TeXDocumentation,
|
|
|
|
'tex': TeXType.TeXDocumentation,
|
|
|
|
}
|
|
|
|
source_formatter_switcher: Dict[str, Type[PyTeXFormatter]] = {
|
2022-02-08 19:01:18 +01:00
|
|
|
'dtx.pytex': DTXFormatter,
|
|
|
|
'sty.pytex': SimpleTeXFormatter,
|
|
|
|
'cls.pytex': SimpleTeXFormatter,
|
|
|
|
'dict.pytex': DictFormatter
|
2022-02-08 18:26:25 +01:00
|
|
|
}
|
2022-02-18 16:23:43 +01:00
|
|
|
tex_formatter_switcher = {
|
2022-02-18 16:27:03 +01:00
|
|
|
'ins': NothingFormatter,
|
|
|
|
'drv': NothingFormatter,
|
|
|
|
'dtx': DocStripFormatter,
|
|
|
|
'dict': CopyFormatter,
|
|
|
|
'cls': CopyFormatter,
|
|
|
|
'sty': CopyFormatter,
|
2022-02-09 17:42:56 +01:00
|
|
|
}
|
2022-02-18 16:23:43 +01:00
|
|
|
documentation_formatter_switcher = {
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
if target == Target.tex_source:
|
|
|
|
switcher = source_formatter_switcher
|
|
|
|
elif target == Target.tex:
|
|
|
|
switcher = tex_formatter_switcher
|
|
|
|
elif target == Target.documentation:
|
|
|
|
switcher = documentation_formatter_switcher
|
|
|
|
else:
|
|
|
|
switcher = source_formatter_switcher | tex_formatter_switcher # Default case
|
2022-02-08 18:26:25 +01:00
|
|
|
try:
|
|
|
|
[name, extension] = input_file.name.split('.', maxsplit=1)
|
2022-02-18 16:23:43 +01:00
|
|
|
except ValueError:
|
2022-02-08 18:26:25 +01:00
|
|
|
raise NotImplementedError
|
|
|
|
|
2022-02-18 16:23:43 +01:00
|
|
|
config.tex_type = extension_switcher[extension] # This sets the textype from file extension
|
2022-02-09 17:42:56 +01:00
|
|
|
|
2022-02-09 17:22:38 +01:00
|
|
|
formatter = switcher[extension](
|
2022-02-08 18:26:25 +01:00
|
|
|
input_file=input_file,
|
|
|
|
config=config,
|
2022-02-09 14:08:51 +01:00
|
|
|
git_version_info=git_version_info,
|
2022-02-08 18:26:25 +01:00
|
|
|
locate_file_config=locate_file_config,
|
|
|
|
allow_infile_config=allow_infile_config
|
|
|
|
)
|
2022-02-18 16:23:43 +01:00
|
|
|
if default_macros and target == Target.tex_source:
|
2022-02-18 10:05:43 +01:00
|
|
|
formatter.macros = get_default_macros(formatter.config.tex_flavour, formatter.config.tex_type)
|
2022-02-09 17:22:38 +01:00
|
|
|
return formatter
|
|
|
|
|
|
|
|
|