Serialize categories (and articles slugs) when serializing site.
This commit is contained in:
parent
9e2d1451bc
commit
e4abf63e9c
|
@ -84,6 +84,7 @@ ipython_config.py
|
||||||
|
|
||||||
# pyenv
|
# pyenv
|
||||||
.python-version
|
.python-version
|
||||||
|
.vscode/
|
||||||
|
|
||||||
# pipenv
|
# pipenv
|
||||||
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
|
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
# coding: utf-8
|
|
||||||
|
|
||||||
import datetime
|
import datetime
|
||||||
import json
|
import json
|
||||||
|
@ -6,7 +6,6 @@ import re
|
||||||
import os
|
import os
|
||||||
import logging
|
import logging
|
||||||
|
|
||||||
from slugify import slugify
|
|
||||||
|
|
||||||
logger = logging.getLogger(__name__)
|
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)))
|
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):
|
def split(file_path):
|
||||||
return os.path.normpath(file_path).split(os.sep)
|
return os.path.normpath(file_path).split(os.sep)
|
||||||
|
|
||||||
|
@ -65,8 +77,6 @@ class Article(object):
|
||||||
if self.slug and self.slug.startswith('-'):
|
if self.slug and self.slug.startswith('-'):
|
||||||
self.slug = self.slug[1:]
|
self.slug = self.slug[1:]
|
||||||
|
|
||||||
self.slug = slugify(self.slug)
|
|
||||||
|
|
||||||
try:
|
try:
|
||||||
self.category = split_path[1] #
|
self.category = split_path[1] #
|
||||||
self.keywords = split_path[2:-1]
|
self.keywords = split_path[2:-1]
|
||||||
|
@ -153,26 +163,16 @@ class Site(object):
|
||||||
|
|
||||||
self.articles.append(article)
|
self.articles.append(article)
|
||||||
|
|
||||||
if article.category not in self.categories:
|
category = categories.setdefault(article.category, [])
|
||||||
self.categories[article.category] = []
|
category.append(article.slug)
|
||||||
|
|
||||||
self.categories[article.category].append(article)
|
|
||||||
|
|
||||||
return article
|
return article
|
||||||
|
|
||||||
def to_json(self):
|
def serialize(self, indent=None):
|
||||||
"""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):
|
|
||||||
"""Serialize the current files structure to index.json"""
|
"""Serialize the current files structure to index.json"""
|
||||||
|
|
||||||
with open('index.json', 'w') as json_serialized_file:
|
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))
|
||||||
|
|
Loading…
Reference in New Issue