Link articles to the current site

This commit is contained in:
Fred Pauchet 2023-01-09 21:42:44 +01:00
parent e577d67b16
commit b5fcc92d19
3 changed files with 47 additions and 40 deletions

View File

@ -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/")

View File

@ -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("""<html><body><p>{}</p></body></html>""".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("""<html><body><p>{}</p></body></html>""".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")

View File

@ -43,28 +43,9 @@ def test_article_fenced_code():
assert """<pre><code class="language-python">""" 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():