Link articles to the current site
This commit is contained in:
parent
e577d67b16
commit
b5fcc92d19
|
@ -17,10 +17,10 @@ def generate_site(input_path):
|
||||||
|
|
||||||
LOGGER.info("Processing static content to {}".format(root_directory))
|
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):
|
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:
|
if "index.md" in files:
|
||||||
with open(os.path.join(root, "index.md")) as md_file:
|
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 = Article(content, base_name)
|
||||||
article.write(root)
|
article.write(root)
|
||||||
|
site.add(article)
|
||||||
else:
|
else:
|
||||||
LOGGER.warning(
|
LOGGER.warning(
|
||||||
"{}: No `index.md` or `_index.md` files found in this directory.".format(
|
"{}: No `index.md` or `_index.md` files found in this directory.".format(
|
||||||
|
@ -50,3 +51,7 @@ def run(args=None):
|
||||||
|
|
||||||
args = parser.parse_args()
|
args = parser.parse_args()
|
||||||
generate_site(args.source)
|
generate_site(args.source)
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
run("/home/fred/Sources/dev-blog/content/")
|
||||||
|
|
|
@ -38,7 +38,9 @@ class Article:
|
||||||
self.relative_file_path = relative_file_path
|
self.relative_file_path = relative_file_path
|
||||||
|
|
||||||
if self.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:
|
else:
|
||||||
self.sections = []
|
self.sections = []
|
||||||
|
|
||||||
|
@ -54,26 +56,34 @@ class Article:
|
||||||
return self.html
|
return self.html
|
||||||
|
|
||||||
def write(self, root_output_path):
|
def write(self, root_output_path):
|
||||||
with open(
|
output_file_path = os.path.join(root_output_path, "index.html")
|
||||||
os.path.join(root_output_path, self.relative_file_path, "index.html"), "w"
|
|
||||||
) as output_file:
|
LOGGER.warning("Writing file to {}".format(output_file_path))
|
||||||
|
|
||||||
|
with open(output_file_path, "w") as output_file:
|
||||||
output_file.write(self.to_prose())
|
output_file.write(self.to_prose())
|
||||||
|
|
||||||
|
|
||||||
class Section:
|
class Section:
|
||||||
def __init__(self, key, path=None):
|
def __init__(self, key, parent=None):
|
||||||
self.key = key
|
self.key = key
|
||||||
self.path = path
|
self.parent = parent
|
||||||
self.parent = None
|
|
||||||
self.sections = {}
|
self.sections = {}
|
||||||
self.articles = []
|
self.articles = []
|
||||||
|
|
||||||
def add(self, section):
|
def add(self, article):
|
||||||
if section.key not in self.sections.keys():
|
self.articles.append(article)
|
||||||
section.parent = self
|
|
||||||
self.sections[section.key] = section
|
|
||||||
|
|
||||||
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:
|
class Tag:
|
||||||
|
@ -116,19 +126,30 @@ class Site:
|
||||||
tag_key = self.tags.setdefault(tag, [])
|
tag_key = self.tags.setdefault(tag, [])
|
||||||
tag_key.append(article)
|
tag_key.append(article)
|
||||||
|
|
||||||
|
self._fill_sections(article)
|
||||||
|
|
||||||
return article
|
return article
|
||||||
|
|
||||||
def add_section(self, section: Section):
|
def _fill_sections(self, article):
|
||||||
self.sections.setdefault(section.key, section)
|
if article.sections:
|
||||||
self.sections[section.key].articles.extend(section.articles)
|
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):
|
def write(self):
|
||||||
LOGGER.info("Writing site articles")
|
LOGGER.info("Writing site articles")
|
||||||
|
|
||||||
for article in self.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")
|
LOGGER.info("Writing site categories")
|
||||||
for section in self.sections:
|
for section in self.sections.values():
|
||||||
section.write(self.root_directory)
|
section.write(self.root_directory)
|
||||||
|
|
||||||
LOGGER.info("Writing site tags")
|
LOGGER.info("Writing site tags")
|
||||||
|
|
|
@ -43,28 +43,9 @@ def test_article_fenced_code():
|
||||||
assert """<pre><code class="language-python">""" in article.to_prose()
|
assert """<pre><code class="language-python">""" in article.to_prose()
|
||||||
|
|
||||||
|
|
||||||
def test_sections():
|
def test_article_sections():
|
||||||
article = Article(content, "a/b/c")
|
article = Article(content, "a/b/c")
|
||||||
|
assert article.sections == ["a", "b"]
|
||||||
|
|
||||||
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
|
|
||||||
|
|
||||||
|
|
||||||
def test_site_append_article():
|
def test_site_append_article():
|
||||||
|
|
Loading…
Reference in New Issue