let pytex source files auto select formatter if not given
This commit is contained in:
parent
f89ccf245b
commit
92e69a0d77
2 changed files with 14 additions and 6 deletions
|
@ -6,6 +6,7 @@ from PyTeX.format.formatterif import FormatterIF
|
||||||
from PyTeX.build.build.enums import PyTeXFileType
|
from PyTeX.build.build.enums import PyTeXFileType
|
||||||
from .hashing import md5
|
from .hashing import md5
|
||||||
from ...format.formatting_config import FormattingConfig
|
from ...format.formatting_config import FormattingConfig
|
||||||
|
from ...format.auto_format import formatter_from_file_extension
|
||||||
|
|
||||||
|
|
||||||
class PyTeXSourceFile:
|
class PyTeXSourceFile:
|
||||||
|
@ -13,15 +14,22 @@ class PyTeXSourceFile:
|
||||||
self,
|
self,
|
||||||
relative_path: RelativePath,
|
relative_path: RelativePath,
|
||||||
formatter: Optional[FormatterIF] = None,
|
formatter: Optional[FormatterIF] = None,
|
||||||
|
default_config: Optional[FormattingConfig] = None,
|
||||||
pytex_file_type: Optional[PyTeXFileType] = None
|
pytex_file_type: Optional[PyTeXFileType] = None
|
||||||
):
|
):
|
||||||
self._relative_path: RelativePath = relative_path
|
self._relative_path: RelativePath = relative_path
|
||||||
self._pytex_file_type: Optional[PyTeXFileType] = None
|
if formatter is not None:
|
||||||
self._formatter: Optional[FormatterIF] = None
|
self._formatter: FormatterIF = formatter
|
||||||
|
else:
|
||||||
|
self._formatter = formatter_from_file_extension(
|
||||||
|
relative_path.path,
|
||||||
|
config=default_config,
|
||||||
|
locate_file_config=True,
|
||||||
|
allow_infile_config=True
|
||||||
|
)
|
||||||
|
self._pytex_file_type: Optional[PyTeXFileType] = pytex_file_type
|
||||||
self._file_hash: Optional[str] = None
|
self._file_hash: Optional[str] = None
|
||||||
|
|
||||||
# TODO: select formatter automatically...?
|
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def file_hash(self) -> str:
|
def file_hash(self) -> str:
|
||||||
if self._file_hash is None:
|
if self._file_hash is None:
|
||||||
|
|
|
@ -9,8 +9,7 @@ from .dtx_formatter import DTXFormatter
|
||||||
from .pytex_formatter import PyTeXFormatter
|
from .pytex_formatter import PyTeXFormatter
|
||||||
|
|
||||||
|
|
||||||
def from_file_extension(
|
def formatter_from_file_extension(
|
||||||
cls,
|
|
||||||
input_file: Path,
|
input_file: Path,
|
||||||
config: Optional[FormattingConfig] = None,
|
config: Optional[FormattingConfig] = None,
|
||||||
locate_file_config: bool = True,
|
locate_file_config: bool = True,
|
||||||
|
@ -22,6 +21,7 @@ def from_file_extension(
|
||||||
'.cls.pytex': SimpleTeXFormatter,
|
'.cls.pytex': SimpleTeXFormatter,
|
||||||
'.dict.pytex': DictFormatter
|
'.dict.pytex': DictFormatter
|
||||||
}
|
}
|
||||||
|
# TODO: other formatters
|
||||||
try:
|
try:
|
||||||
[name, extension] = input_file.name.split('.', maxsplit=1)
|
[name, extension] = input_file.name.split('.', maxsplit=1)
|
||||||
except:
|
except:
|
||||||
|
|
Loading…
Reference in a new issue