[wip] Adds a new 'Category' class.

This commit is contained in:
Fred Pauchet 2020-06-22 21:38:26 +02:00
parent e4abf63e9c
commit 0f07b37d2d
3 changed files with 79 additions and 47 deletions

View File

@ -55,12 +55,30 @@ def split(file_path):
return os.path.normpath(file_path).split(os.sep) return os.path.normpath(file_path).split(os.sep)
class Article(object): class Category():
def __init__(self, name):
self.name = name
self.articles = []
def __init__(self, path, content, publication_date=None): def append(self, article):
self.articles.append(article.slug)
class Content():
def __init__(self, path, content):
self.path = path self.path = path
self.content = content self.content = content
class Page(Content):
pass
class Article(Content):
def __init__(self, path, content, category, keywords=[], publication_date=None):
super().__init__(path, content)
split_path = split(path) split_path = split(path)
self.filename = split_path[-1] # the last element self.filename = split_path[-1] # the last element
@ -77,12 +95,8 @@ 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:]
try: self.category_name = category
self.category = split_path[1] # self.keywords = keywords
self.keywords = split_path[2:-1]
except IndexError:
self.category = ''
self.keywords = []
try: try:
self.title = content.splitlines()[0] self.title = content.splitlines()[0]
@ -121,13 +135,15 @@ class Site(object):
Args: Args:
root_path (path): The path where articles are stored. root_path (path): The path where articles are stored.
articles (array): contain all articles.
""" """
def __init__(self, root_path='articles'): def __init__(self, root_path, output_path):
self.articles = [] self.articles = []
self.pages = []
self.categories = {} self.categories = {}
self.keywords = {} self.keywords = {}
self.root_path = root_path
self.output_path = output_path
for root, *_, files in os.walk(root_path): for root, *_, files in os.walk(root_path):
for file in [file for file in files if file.endswith(".md")]: for file in [file for file in files if file.endswith(".md")]:
@ -152,27 +168,38 @@ class Site(object):
article_file_path = os.path.join(filepath, filename) article_file_path = os.path.join(filepath, filename)
data = [
x
for x in split(article_file_path.replace(self.root_path, '').replace(filename, ''))
if x
]
category = data[0]
keywords = data[1:]
article = None article = None
with open(article_file_path, encoding="utf8") as f: with open(article_file_path, encoding="utf8") as f:
content = f.read() content = f.read()
article = Article(article_file_path, content) article = Article(article_file_path, content, category, keywords)
if article: if article:
logger.warn('article found in %s: %s', article.category, article) logger.info('article found in %s: %s', article.category_name, article)
self.articles.append(article) self.articles.append(article)
category = categories.setdefault(article.category, []) category = self.categories.setdefault(
category.append(article.slug) article.category_name, Category(article.category_name)
)
category.append(article)
return article return article
def serialize(self, indent=None): def serialize(self, indent=None):
"""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(os.path.join(self.output_path, 'index.json'), 'w') as json_serialized_file:
json_serialized_file.write(to_json(self, indent)) json_serialized_file.write(to_json({"data": self}, indent))
with open("categories.json", "w") as json_serialized_file: with open(os.path.join(self.output_path, "categories.json"), "w") as json_serialized_file:
json_serialized_file.write(to_json(self.categories, indent)) json_serialized_file.write(to_json({"data": self.categories.values()}, indent))

View File

@ -6,7 +6,9 @@ from clize import run
def generate_static_site(input_folder, output_folder): def generate_static_site(input_folder, output_folder):
Site(input_folder) site = Site(input_folder, output_folder)
site.serialize()
print(site)
if __name__ == "__main__": if __name__ == "__main__":

View File

@ -1,30 +1,33 @@
<!DOCTYPE html> <html>
<html lang="en">
<head> <head>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/foundation/6.3.1/css/foundation.min.css"> <title>Grnx</title>
<meta charset="utf-8"> <meta charset="UTF-8">
<title>Cryptocurrency Pricing Application</title> <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
<script src="https://unpkg.com/axios/dist/axios.min.js"></script>
</head> </head>
<body>
<body> <div id="app">
<div class="container" id="app"> <h1>Grnx</h1>
<h3 class="text-center">Cryptocurrency Pricing</h3> <ul>
<div class="columns medium-4" v-for="(result, index) in results"> <li v-for="(category, article_slugs) in categories.data">
<div class="card"> {{ category }}
<div class="card-section"> </li>
<p> {{ index }} </p> </ul>
</div> </div>
<div class="card-divider"> <script>
<p>$ {{ result.USD }}</p> const app = new Vue({
</div> el: '#app',
<div class="card-section"> data () {
<p> &#8364 {{ result.EUR }}</p> return {
</div> categories: [],
</div> }
</div> },
</div> mounted () {
axios
<script src="https://unpkg.com/vue"></script> .get('categories.json')
<script src="vueApp.js"></script> .then(response => (this.categories = response.data));
</body> }
</html> })
</script>
</body>
</html>