diff --git a/.gitignore b/.gitignore index 13d1490..58a20ca 100644 --- a/.gitignore +++ b/.gitignore @@ -84,6 +84,7 @@ ipython_config.py # pyenv .python-version +.vscode/ # pipenv # According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control. diff --git a/grnx/models.py b/grnx/models.py index 6e26f00..986a699 100644 --- a/grnx/models.py +++ b/grnx/models.py @@ -1,4 +1,4 @@ -# coding: utf-8 + import datetime import json @@ -6,7 +6,6 @@ import re import os import logging -from slugify import slugify logger = logging.getLogger(__name__) @@ -39,6 +38,19 @@ def json_handler(obj): raise TypeError('Object of type %s with value of %s is not JSON serializable' % (type(obj), repr(obj))) +def to_json(obj, indent): + """Serialize the content of the current structure to JSON format.""" + + json_dumps = json.dumps( + obj, + default=json_handler, + sort_keys=True, + indent=indent + ) + + return json_dumps + + def split(file_path): return os.path.normpath(file_path).split(os.sep) @@ -65,8 +77,6 @@ class Article(object): if self.slug and self.slug.startswith('-'): self.slug = self.slug[1:] - self.slug = slugify(self.slug) - try: self.category = split_path[1] # self.keywords = split_path[2:-1] @@ -153,26 +163,16 @@ class Site(object): self.articles.append(article) - if article.category not in self.categories: - self.categories[article.category] = [] - - self.categories[article.category].append(article) + category = categories.setdefault(article.category, []) + category.append(article.slug) return article - def to_json(self): - """Serialize the content of the current structure to JSON format.""" - - json_dumps = json.dumps( - self, - default=json_handler, - sort_keys=True, - indent=4 - ) - return json_dumps - - def serialize(self): + def serialize(self, indent=None): """Serialize the current files structure to index.json""" with open('index.json', 'w') as json_serialized_file: - json_serialized_file.write(self.to_json()) + json_serialized_file.write(to_json(self, indent)) + + with open("categories.json", "w") as json_serialized_file: + json_serialized_file.write(to_json(self.categories, indent)) diff --git a/main.py b/main.py new file mode 100644 index 0000000..b60ffeb --- /dev/null +++ b/main.py @@ -0,0 +1,13 @@ + +from grnx.models import Site + + +from clize import run + + +def generate_static_site(input_folder, output_folder): + Site(input_folder) + + +if __name__ == "__main__": + run(generate_static_site)