Serialize categories (and articles slugs) when serializing site.

This commit is contained in:
Fred Pauchet 2020-06-21 21:26:03 +02:00
parent 9e2d1451bc
commit e4abf63e9c
3 changed files with 35 additions and 21 deletions

1
.gitignore vendored
View File

@ -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.

View File

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

13
main.py Normal file
View File

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