mirror of
https://github.com/dtomlinson91/tembo.git
synced 2025-12-22 07:55:45 +00:00
moving get template logic into own method
This commit is contained in:
@@ -51,13 +51,13 @@ class PageCreator:
|
|||||||
return path_to_file.with_suffix(f".{extension}")
|
return path_to_file.with_suffix(f".{extension}")
|
||||||
|
|
||||||
def _load_template(self, base_path: str, template_filename: str) -> str:
|
def _load_template(self, base_path: str, template_filename: str) -> str:
|
||||||
if tembo.CONFIG.template_path is not None:
|
|
||||||
# check for overriden template_path
|
# check for overriden template_path
|
||||||
|
if tembo.CONFIG.template_path is not None:
|
||||||
template_path = self._convert_to_path(
|
template_path = self._convert_to_path(
|
||||||
"", tembo.CONFIG.template_path, "", ""
|
"", tembo.CONFIG.template_path, "", ""
|
||||||
)
|
)
|
||||||
else:
|
else:
|
||||||
# default template_path is base_path / templates
|
# default template_path is base_path / .templates
|
||||||
template_path = self._convert_to_path(base_path, ".templates", "", "")
|
template_path = self._convert_to_path(base_path, ".templates", "", "")
|
||||||
# load the template folder
|
# load the template folder
|
||||||
file_loader = jinja2.FileSystemLoader(template_path)
|
file_loader = jinja2.FileSystemLoader(template_path)
|
||||||
@@ -75,6 +75,15 @@ class PageCreator:
|
|||||||
|
|
||||||
|
|
||||||
class ScopedPageCreator(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:
|
def __init__(self) -> None:
|
||||||
self.base_path = ""
|
self.base_path = ""
|
||||||
self.page_path = ""
|
self.page_path = ""
|
||||||
@@ -111,15 +120,12 @@ class ScopedPageCreator(PageCreator):
|
|||||||
path = pathlib.Path(self._substitute_tokens(str(path), user_input, name))
|
path = pathlib.Path(self._substitute_tokens(str(path), user_input, name))
|
||||||
|
|
||||||
# get the template file
|
# get the template file
|
||||||
|
template_contents = self._get_template_contents(template_filename)
|
||||||
|
# substitute tokens in template_contents
|
||||||
if template_filename is not None:
|
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 = self._substitute_tokens(
|
||||||
template_contents, user_input, name
|
template_contents, user_input, name
|
||||||
)
|
)
|
||||||
else:
|
|
||||||
template_contents = ""
|
|
||||||
|
|
||||||
return ScopedPage(path, template_contents)
|
return ScopedPage(path, template_contents)
|
||||||
|
|
||||||
def _get_input_tokens(self, template_filename: str | None) -> list[str]:
|
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
|
self.base_path, self.page_path, self.filename, self.extension
|
||||||
).expanduser()
|
).expanduser()
|
||||||
)
|
)
|
||||||
if template_filename is not None:
|
template_contents = self._get_template_contents(template_filename)
|
||||||
template_contents = self._load_template(self.base_path, template_filename)
|
|
||||||
else:
|
|
||||||
template_contents = ""
|
|
||||||
# get the input tokens from both the path and the template
|
# get the input tokens from both the path and the template
|
||||||
all_input_tokens = []
|
all_input_tokens = []
|
||||||
for tokenified_string in (path, template_contents):
|
for tokenified_string in (path, template_contents):
|
||||||
@@ -147,6 +150,13 @@ class ScopedPageCreator(PageCreator):
|
|||||||
)
|
)
|
||||||
raise SystemExit(1)
|
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(
|
def _substitute_tokens(
|
||||||
self,
|
self,
|
||||||
tokenified_string: str,
|
tokenified_string: str,
|
||||||
|
|||||||
Reference in New Issue
Block a user