[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)
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.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)
self.filename = split_path[-1] # the last element
@ -77,12 +95,8 @@ class Article(object):
if self.slug and self.slug.startswith('-'):
self.slug = self.slug[1:]
try:
self.category = split_path[1] #
self.keywords = split_path[2:-1]
except IndexError:
self.category = ''
self.keywords = []
self.category_name = category
self.keywords = keywords
try:
self.title = content.splitlines()[0]
@ -121,13 +135,15 @@ class Site(object):
Args:
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.pages = []
self.categories = {}
self.keywords = {}
self.root_path = root_path
self.output_path = output_path
for root, *_, files in os.walk(root_path):
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)
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
with open(article_file_path, encoding="utf8") as f:
content = f.read()
article = Article(article_file_path, content)
article = Article(article_file_path, content, category, keywords)
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)
category = categories.setdefault(article.category, [])
category.append(article.slug)
category = self.categories.setdefault(
article.category_name, Category(article.category_name)
)
category.append(article)
return article
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(to_json(self, indent))
with open(os.path.join(self.output_path, 'index.json'), 'w') as json_serialized_file:
json_serialized_file.write(to_json({"data": self}, indent))
with open("categories.json", "w") as json_serialized_file:
json_serialized_file.write(to_json(self.categories, indent))
with open(os.path.join(self.output_path, "categories.json"), "w") as json_serialized_file:
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):
Site(input_folder)
site = Site(input_folder, output_folder)
site.serialize()
print(site)
if __name__ == "__main__":

View File

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