Commit 05c9ee86 authored by berumuron's avatar berumuron
Browse files

Extract function to build a page from filepath

parent bba6db12
...@@ -13,7 +13,6 @@ import boop.boopfeed as boopfeed ...@@ -13,7 +13,6 @@ import boop.boopfeed as boopfeed
import boop.environment as environment import boop.environment as environment
import boop.configuration as config import boop.configuration as config
import boop.utils as utils import boop.utils as utils
from boop.page import Page
locale.setlocale(locale.LC_ALL, "") locale.setlocale(locale.LC_ALL, "")
...@@ -53,20 +52,20 @@ def main(environment): ...@@ -53,20 +52,20 @@ def main(environment):
if not os.path.exists(index_filepath): if not os.path.exists(index_filepath):
raise ProgramError("./index.html file does not exist") raise ProgramError("./index.html file does not exist")
with open(index_filepath, "r") as index_file: index_page = utils.build_page_from_filepath(
page = Page("index", index_file.read(), configuration=configuration) index_filepath, configuration, basename=os.curdir
pages.append(page) )
pages.append(index_page)
# Check that pages directory exists and load Pages from it # Check that pages directory exists and load Pages from it
pages_path = os.path.join(os.curdir, "pages") pages_path = os.path.join(os.curdir, "pages")
if os.path.isdir(pages_path): if os.path.isdir(pages_path):
for filename in utils.dir_tree(pages_path, only=["html"]): for filename in utils.dir_tree(pages_path, only=["html"]):
# We read the page file and initialize a Page from it
slug, _ = os.path.splitext(filename)
page_filepath = os.path.join(pages_path, filename) page_filepath = os.path.join(pages_path, filename)
with open(page_filepath, "r") as page_file: page = utils.build_page_from_filepath(
page = Page(slug, page_file.read(), configuration=configuration) page_filepath, configuration, basename=pages_path
pages.append(page) )
pages.append(page)
# Check that articles directory exists and load Articles from it # Check that articles directory exists and load Articles from it
articles_path = os.path.join(os.curdir, "articles") articles_path = os.path.join(os.curdir, "articles")
...@@ -84,10 +83,11 @@ def main(environment): ...@@ -84,10 +83,11 @@ def main(environment):
# We have a serie Page! Each article of the directory must be # We have a serie Page! Each article of the directory must be
# considered part of the serie. # considered part of the serie.
slug = f"serie/{dirname}" slug = f"serie/{dirname}"
with open(serie_filepath, "r") as serie_file: serie = utils.build_page_from_filepath(
serie = Page(slug, serie_file.read(), configuration=configuration) serie_filepath, configuration, slug=slug
pages.append(serie) )
articles_by_series[serie] = [] pages.append(serie)
articles_by_series[serie] = []
for filename in utils.list_files(dirpath, only=["md"]): for filename in utils.list_files(dirpath, only=["md"]):
article_filepath = os.path.join(dirpath, filename) article_filepath = os.path.join(dirpath, filename)
......
...@@ -2,6 +2,7 @@ import os ...@@ -2,6 +2,7 @@ import os
import shutil import shutil
from boop.article import Article from boop.article import Article
from boop.page import Page
def init_output_folder(output_path): def init_output_folder(output_path):
...@@ -49,6 +50,22 @@ def build_article_from_filepath(filepath, configuration, serie=None): ...@@ -49,6 +50,22 @@ def build_article_from_filepath(filepath, configuration, serie=None):
) )
def build_page_from_filepath(filepath, configuration, basename=None, slug=None):
"""Read a file and initialize a Page from it.
"""
filepath_no_ext, _ = os.path.splitext(filepath)
if slug:
final_slug = slug
elif basename and filepath_no_ext.startswith(basename):
index_basename = len(basename) + 1
final_slug = filepath_no_ext[index_basename:]
else:
final_slug = os.path.basename(filepath_no_ext)
with open(filepath, "r") as page_file:
return Page(final_slug, page_file.read(), configuration=configuration)
def list_dirs(path): def list_dirs(path):
"""List all the directories within a path. """List all the directories within a path.
""" """
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment