moving get template logic into own method

This commit is contained in:
2021-10-25 14:34:15 +01:00
parent d5831d1b75
commit 5348fb72e4

View File

@@ -51,13 +51,13 @@ class PageCreator:
return path_to_file.with_suffix(f".{extension}")
def _load_template(self, base_path: str, template_filename: str) -> str:
if tembo.CONFIG.template_path is not None:
# check for overriden template_path
if tembo.CONFIG.template_path is not None:
template_path = self._convert_to_path(
"", tembo.CONFIG.template_path, "", ""
)
else:
# default template_path is base_path / templates
# default template_path is base_path / .templates
template_path = self._convert_to_path(base_path, ".templates", "", "")
# load the template folder
file_loader = jinja2.FileSystemLoader(template_path)
@@ -75,6 +75,15 @@ class PageCreator:
class ScopedPageCreator(PageCreator):
"""Factory to create a scoped page.
Attributes:
base_path (str): base path of tembo.
page_path (str): path of the page relative to the base path.
filename (str): filename relative to the page path
extension (str): extension of file
"""
def __init__(self) -> None:
self.base_path = ""
self.page_path = ""
@@ -111,15 +120,12 @@ class ScopedPageCreator(PageCreator):
path = pathlib.Path(self._substitute_tokens(str(path), user_input, name))
# get the template file
template_contents = self._get_template_contents(template_filename)
# substitute tokens in template_contents
if template_filename is not None:
# load the template file contents and substitute tokens
template_contents = self._load_template(self.base_path, template_filename)
template_contents = self._substitute_tokens(
template_contents, user_input, name
)
else:
template_contents = ""
return ScopedPage(path, template_contents)
def _get_input_tokens(self, template_filename: str | None) -> list[str]:
@@ -128,10 +134,7 @@ class ScopedPageCreator(PageCreator):
self.base_path, self.page_path, self.filename, self.extension
).expanduser()
)
if template_filename is not None:
template_contents = self._load_template(self.base_path, template_filename)
else:
template_contents = ""
template_contents = self._get_template_contents(template_filename)
# get the input tokens from both the path and the template
all_input_tokens = []
for tokenified_string in (path, template_contents):
@@ -147,6 +150,13 @@ class ScopedPageCreator(PageCreator):
)
raise SystemExit(1)
def _get_template_contents(self, template_filename: str | None) -> str:
return (
self._load_template(self.base_path, template_filename)
if template_filename is not None
else ""
)
def _substitute_tokens(
self,
tokenified_string: str,