From 5348fb72e41fa9bc176e74d7b434cf3de095ca4f Mon Sep 17 00:00:00 2001 From: Daniel Tomlinson Date: Mon, 25 Oct 2021 14:34:15 +0100 Subject: [PATCH] moving get template logic into own method --- tembo/journal/pages.py | 32 +++++++++++++++++++++----------- 1 file changed, 21 insertions(+), 11 deletions(-) diff --git a/tembo/journal/pages.py b/tembo/journal/pages.py index db0620f..8fbf21f 100644 --- a/tembo/journal/pages.py +++ b/tembo/journal/pages.py @@ -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: + # check for overriden template_path if tembo.CONFIG.template_path is not None: - # check for overriden template_path 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,