From b5fcc92d19617ef917e82a4c820426d2ff627891 Mon Sep 17 00:00:00 2001 From: Fred Pauchet Date: Mon, 9 Jan 2023 21:42:44 +0100 Subject: [PATCH] Link articles to the current site --- jack/commands.py | 9 ++++++-- jack/models.py | 55 ++++++++++++++++++++++++++++++-------------- tests/test_models.py | 23 ++---------------- 3 files changed, 47 insertions(+), 40 deletions(-) diff --git a/jack/commands.py b/jack/commands.py index 3b84261..003b14e 100644 --- a/jack/commands.py +++ b/jack/commands.py @@ -17,10 +17,10 @@ def generate_site(input_path): LOGGER.info("Processing static content to {}".format(root_directory)) - site = Site("Site name") + site = Site(root_directory) for root, dirs, files in os.walk(root_directory): - base_name = root.replace(root_directory, "") + base_name = root.replace(root_directory + "/", "") if "index.md" in files: with open(os.path.join(root, "index.md")) as md_file: @@ -28,6 +28,7 @@ def generate_site(input_path): article = Article(content, base_name) article.write(root) + site.add(article) else: LOGGER.warning( "{}: No `index.md` or `_index.md` files found in this directory.".format( @@ -50,3 +51,7 @@ def run(args=None): args = parser.parse_args() generate_site(args.source) + + +if __name__ == "__main__": + run("/home/fred/Sources/dev-blog/content/") diff --git a/jack/models.py b/jack/models.py index 2a65741..172efe3 100644 --- a/jack/models.py +++ b/jack/models.py @@ -38,7 +38,9 @@ class Article: self.relative_file_path = relative_file_path if self.relative_file_path: - self.sections = categories = [x for x in relative_file_path.split(os.sep) if x] + self.sections = categories = [ + x for x in relative_file_path.split(os.sep) if x + ][:-1] else: self.sections = [] @@ -54,26 +56,34 @@ class Article: return self.html def write(self, root_output_path): - with open( - os.path.join(root_output_path, self.relative_file_path, "index.html"), "w" - ) as output_file: + output_file_path = os.path.join(root_output_path, "index.html") + + LOGGER.warning("Writing file to {}".format(output_file_path)) + + with open(output_file_path, "w") as output_file: output_file.write(self.to_prose()) class Section: - def __init__(self, key, path=None): + def __init__(self, key, parent=None): self.key = key - self.path = path - self.parent = None + self.parent = parent self.sections = {} self.articles = [] - def add(self, section): - if section.key not in self.sections.keys(): - section.parent = self - self.sections[section.key] = section + def add(self, article): + self.articles.append(article) - return self.sections[section.key] + def write(self, root_path): + section_path = os.path.join(root_path, self.key) + + LOGGER.warning("Writing section to {}".format(section_path)) + + with open(os.path.join(section_path, "index.html"), "w") as html_file: + html_file.write("""

{}

""".format(self.articles)) + + for child in self.sections.values(): + child.write(section_path) class Tag: @@ -116,19 +126,30 @@ class Site: tag_key = self.tags.setdefault(tag, []) tag_key.append(article) + self._fill_sections(article) + return article - def add_section(self, section: Section): - self.sections.setdefault(section.key, section) - self.sections[section.key].articles.extend(section.articles) + def _fill_sections(self, article): + if article.sections: + section = article.sections[0] + section_key = self.sections.setdefault(section, Section(section)) + section_key.add(article) + + if len(article.sections) > 1: + for section in article.sections[1:]: + section_key = section_key.sections.setdefault(section, Section(section)) + section_key.add(article) def write(self): LOGGER.info("Writing site articles") + for article in self.articles: - article.write(self.root_directory) + with open(os.path.join(self.root_directory, "index.html"), "w") as html_file: + html_file.write("""

{}

""".format(self.articles)) LOGGER.info("Writing site categories") - for section in self.sections: + for section in self.sections.values(): section.write(self.root_directory) LOGGER.info("Writing site tags") diff --git a/tests/test_models.py b/tests/test_models.py index 15e2ec8..6e06b7c 100644 --- a/tests/test_models.py +++ b/tests/test_models.py @@ -43,28 +43,9 @@ def test_article_fenced_code(): assert """
""" in article.to_prose()
 
 
-def test_sections():
+def test_article_sections():
 	article = Article(content, "a/b/c")
-
-
-def test_section_add():
-	parent = Section("dev")
-	child = Section("code")
-	parent.add(child)
-
-	assert child.parent == parent
-	assert child in parent.sections.values()
-	assert child == parent.sections.get("code")
-
-
-def test_add_existing_section():
-	parent = Section("dev")
-	child = Section("code")
-	parent.add(child)
-	other_child = Section("code")
-	parent.add(other_child)
-
-	assert len(parent.sections) == 1
+	assert article.sections == ["a", "b"]
 
 
 def test_site_append_article():