Commit a0bacd96 authored by Denis Salem's avatar Denis Salem
Browse files

done with JSON-LD for categories

parent 6817cabf
......@@ -187,6 +187,7 @@
TODO | Concatenate user defined blog keywords with entries keywords.
TODO | Add more informations into JSON-LD document.
TODO | Optimize and refine JSON-LD processing.
TODO | Fix encoding issue in JSON-LD document.
TODO | Embed as much as possible microdata/JSON-LD into blog.
TODO | Automatically add entries categories to entries tags.
TODO | Make entry URL nicer when filename is index.html and has subfolder.
......
......@@ -191,13 +191,15 @@ class DataStore:
**self.optionals_schemadotorg
}
def categories_to_jsonld(self, category_value):
def categories_to_jsonld(self, category_value, leaf_name):
blog_url = self.blog_configuration["blog_url"]
blog_name = self.blog_configuration["blog_name"]
self.categories_as_jsonld[category_value] = {
"@context": "http://schema.org",
"@type": ["Blog","WebPage"],
"@id" : blog_url+'/'+category_value+"/categories.jsonld",
"name": blog_name,
"url": blog_url+'/'+category_value,
"name": blog_name + ' | ' + leaf_name,
"description": self.blog_configuration["blog_description"],
"author": {
"@type" : "Person",
......@@ -222,7 +224,7 @@ class DataStore:
"blogPost" : [],
**self.optionals_schemadotorg
}
def archives_to_jsonld(self, archive_value):
blog_url = self.blog_configuration["blog_url"]
blog_name = self.blog_configuration["blog_name"]
......@@ -230,9 +232,7 @@ class DataStore:
self.archives_as_jsonld[archive_value] = {
"@context": "http://schema.org",
"@type": ["Blog","WebPage"],
"@id" : blog_url+'/'+archive_value+"/archives.jsonld",
"name": blog_name,
"url": blog_url+'/'+archive_value,
"name": blog_name + ' | ' + archive_value,
"description": self.blog_configuration["blog_description"],
"author": {
"@type" : "Person",
......@@ -265,18 +265,15 @@ class DataStore:
else:
optionals = {}
author = [{"name":author["value"], "@type": "Person"} for author in entry.authors]
authors = [{"name":author["value"], "@type": "Person"} for author in entry.authors]
if "publisher" in entry.schemadotorg.keys():
publisher = entry.schemadotorg["publisher"]
elif "publisher" in self.blog_configuration["https://schema.org"].keys():
publisher = self.blog_configuration["https://schema.org"]["publisher"]
elif author != []:
publisher = [{
"@type":"Person",
"name": v.strip(),
} for v in author.split(',')]
elif authors != []:
publisher = authors
else:
publisher = {
......@@ -295,7 +292,7 @@ class DataStore:
"name" : entry.title,
"datePublished" : entry.date.isoformat(),
"inLanguage" : self.blog_configuration["blog_language"],
"author" : author if author != [] else self.blog_configuration["author_name"],
"author" : authors if authors != [] else self.blog_configuration["author_name"],
"publisher" : publisher,
"url" : entry.url.replace(".:GetRelativeOrigin:.", self.blog_configuration["blog_url"]+"/"),
"breadcrumb" : {
......@@ -339,14 +336,20 @@ class DataStore:
# Setup archives as jsonld if any
entry_formatted_date = entry.formatted_date
if entry_formatted_date not in self.archives_as_jsonld.keys():
self.archives_to_jsonld(entry_formatted_date)
for category in entry.categories_leaves:
path = category["path"].replace('.:GetRelativeOrigin:.','')
self.categories
self.archives_to_jsonld(entry_formatted_date)
self.archives_as_jsonld[entry.formatted_date]["blogPost"].append(blog_post)
# Setup categories as jsonld if any
for category in entry.categories_leaves:
complete_path = category["path"].replace('.:GetRelativeOrigin:.','')
path = ''
for sub_path in complete_path.split('/')[:-1]:
path += sub_path+'/'
if path not in self.categories_as_jsonld.keys():
self.categories_to_jsonld(path, sub_path)
self.categories_as_jsonld[path]["blogPost"].append(blog_post)
def get_chapters(self, argv):
key = ''.join(argv)
if not key in self.html_chapters.keys():
......
......@@ -62,7 +62,8 @@ class ArchivesThread(Thread):
super().do()
if self.datastore.enable_jsonld:
blog_url = self.datastore.blog_configuration["blog_url"]
self.datastore.archives_as_jsonld[thread.value]["breadcrumb"]["itemListElement"].append({
archive_as_jsonld = self.datastore.archives_as_jsonld[thread.value]
archive_as_jsonld["breadcrumb"]["itemListElement"].append({
"@type": "ListItem",
"position": 2,
"item": {
......@@ -71,7 +72,9 @@ class ArchivesThread(Thread):
"name": self.datastore.blog_configuration["blog_name"] +' | '+thread.value
}
})
dump = json.dumps(self.datastore.archives_as_jsonld[thread.value])
archive_as_jsonld["@id"] = blog_url+'/'+self.sub_folders+thread.value+"/archives.jsonld"
archive_as_jsonld["url"] = blog_url+'/'+self.sub_folders+thread.value
dump = json.dumps(archive_as_jsonld)
f = open("blog/"+self.sub_folders+'/'+thread.value+"/archives.jsonld", 'w')
f.write(dump)
......
......@@ -27,7 +27,11 @@ class CategoriesThread(Thread):
def __init__(self, prompt, datastore, theme, patterns, forbidden):
super().__init__(prompt, datastore, theme, patterns, forbidden)
self.filename = self.datastore.blog_configuration["path"]["index_file_name"]
self.export_path = "blog/"+self.datastore.blog_configuration["path"]["categories_sub_folders"]+'/'
self.sub_folders = self.datastore.blog_configuration["path"]["categories_sub_folders"]
if len(self.sub_folders) and self.sub_folders[-1] != '/':
self.sub_folders += '/'
self.export_path = "blog/"+self.sub_folders
self.category_value = ""
self.relative_origin = ""
self.in_thread = True
self.disable_rss_feed = self.datastore.blog_configuration["disable_rss_feed"]
......@@ -64,6 +68,8 @@ class CategoriesThread(Thread):
notify(self.indentation_level+tree_special_char+"─ "+node.value+"...")
export_path = self.export_path
category_value = self.category_value
self.category_value += node.value+'/'
self.export_path += str(node.value+'/').replace(' ','-')
self.relative_origin = ''.join([ '../' for f in self.export_path.split("/")[1:] if f != '' ]).replace("//",'/')
......@@ -92,12 +98,38 @@ class CategoriesThread(Thread):
self.indentation_level += " "
else:
self.indentation_level += "│ "
self.do(root=node.childs)
self.indentation_level = self.indentation_level[:-3]
if self.datastore.enable_jsonld:
import json
blog_url = self.datastore.blog_configuration["blog_url"]
category_as_jsonld = self.datastore.categories_as_jsonld[self.category_value]
position = 2
category_breadcrumb_path = ''
for sub_category in self.category_value.split('/'):
category_breadcrumb_path += sub_category+'/'
category_as_jsonld["breadcrumb"]["itemListElement"].append({
"@type": "ListItem",
"position": position,
"item": {
"@id": blog_url+'/'+self.sub_folders+category_breadcrumb_path+"categories.jsonld",
"url": blog_url+'/'+self.sub_folders+category_breadcrumb_path,
"name": self.datastore.blog_configuration["blog_name"] +' | '+ sub_category
}
})
position += 1
category_as_jsonld["@id"] = blog_url+'/'+self.sub_folders+self.category_value+"categories.jsonld"
category_as_jsonld["url"] = blog_url+'/'+self.sub_folders+self.category_value
dump = json.dumps(category_as_jsonld)
f = open(("blog/"+self.sub_folders+self.category_value+"categories.jsonld").replace(' ','-'), 'w')
f.write(dump)
# Restore path
self.export_path = export_path
self.category_value = category_value
def JSONLD(self, argv):
if self.current_page == 0:
......
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