Simplify Article and Site models
This commit is contained in:
parent
dc0baa971c
commit
91c9533b08
|
@ -1,8 +1,28 @@
|
|||
from datetime import datetime
|
||||
import logging
|
||||
import os
|
||||
import re
|
||||
from datetime import datetime
|
||||
from typing import List
|
||||
|
||||
import markdown
|
||||
import yaml
|
||||
|
||||
LOGGER = logging.getLogger(__name__)
|
||||
|
||||
|
||||
def extract_metadata_from_content(content):
|
||||
BLOCK_RE = re.compile(r'^-{3}[ \t]*\n(.*?\n)(?:\.{3}|-{3})[ \t]*\n', re.UNICODE|re.DOTALL)
|
||||
|
||||
metadata = {}
|
||||
|
||||
if (match := BLOCK_RE.match(content)):
|
||||
try:
|
||||
metadata = yaml.safe_load(match.group(1))
|
||||
content = content[match.end():].lstrip('\n')
|
||||
except Exception as exc:
|
||||
LOGGER.warning("Unable to extract metadata: {}".format(exc))
|
||||
|
||||
return metadata, content
|
||||
|
||||
|
||||
class Article:
|
||||
|
@ -10,17 +30,22 @@ class Article:
|
|||
|
||||
"""
|
||||
def __init__(self, file_content: str, file_name: str, categories: list):
|
||||
self.content = file_content
|
||||
self.file_name = file_name
|
||||
self.file_path = file_path
|
||||
self.published_date = datetime.strptime(filename[0:10], "%Y-%m-%d")
|
||||
self.categories = categories
|
||||
self.meta, self.content = extract_metadata_from_content(file_content)
|
||||
self.title = self.meta.get("title")
|
||||
self.description = self.meta.get("description")
|
||||
self.tags = self.meta.get("tags")
|
||||
self.file_name = file_name
|
||||
try:
|
||||
self.published_date = datetime.strptime(file_name[0:10], "%Y-%m-%d")
|
||||
except TypeError:
|
||||
self.published_date = None
|
||||
self.categories = categories
|
||||
|
||||
md = markdown.Markdown(extensions=["meta", "fenced_code"])
|
||||
self.html = md.convert(self.content)
|
||||
self.meta = md.Meta
|
||||
self.title = self.meta.get("title")
|
||||
self.description = self.meta.get("description")
|
||||
md = markdown.Markdown(extensions=["fenced_code"])
|
||||
self.html = md.convert(self.content)
|
||||
|
||||
def to_prose(self):
|
||||
return self.html
|
||||
|
||||
|
||||
class Site:
|
||||
|
@ -32,25 +57,19 @@ class Site:
|
|||
self.categories = {}
|
||||
self.tags = {}
|
||||
|
||||
def add(self, folder: str, article_filepath: str):
|
||||
def add(self, file_content: str):
|
||||
"""Add a new article to the current site
|
||||
|
||||
Args:
|
||||
article (Article)
|
||||
Returns:
|
||||
The newly created article
|
||||
"""
|
||||
try:
|
||||
article = Article(self.root_directory, folder, article_filepath)
|
||||
logging.info("Append article {}".format(article))
|
||||
|
||||
self.articles.append(article)
|
||||
article = Article(file_content, None, None)
|
||||
|
||||
category = self.categories.setdefault(article.category, [])
|
||||
category.append(article)
|
||||
self.articles.append(article)
|
||||
|
||||
for tag in article.tags:
|
||||
tag_key = self.tags.setdefault(tag, [])
|
||||
tag_key.append(article)
|
||||
|
||||
except ValueError as value_error:
|
||||
logging.warn("Article does not support {}".format(value_error))
|
||||
for tag in article.tags:
|
||||
tag_key = self.tags.setdefault(tag, [])
|
||||
tag_key.append(article)
|
||||
|
||||
return article
|
||||
|
|
|
@ -1,3 +1,51 @@
|
|||
def test_one_plus_one():
|
||||
assert 1 + 1 == 0
|
||||
"""Tests associated to the models"""
|
||||
|
||||
from jack.models import Article, Site
|
||||
|
||||
content = """---
|
||||
title: This is a test article
|
||||
description: Some fancy description
|
||||
tags:
|
||||
- python
|
||||
- dev
|
||||
- code
|
||||
---
|
||||
|
||||
This is the content
|
||||
|
||||
```python
|
||||
def this_is_a_python_function():
|
||||
...
|
||||
```
|
||||
|
||||
"""
|
||||
|
||||
def test_article_metadata():
|
||||
article = Article(content, None, None)
|
||||
assert article.title == "This is a test article"
|
||||
assert article.description == "Some fancy description"
|
||||
assert article.tags == ["python", "dev", "code"]
|
||||
|
||||
|
||||
def test_article_content():
|
||||
article = Article(content, None, None)
|
||||
assert "title" not in article.content
|
||||
|
||||
|
||||
def test_article_published_date():
|
||||
article = Article(content, None, None)
|
||||
assert article.published_date is None
|
||||
|
||||
|
||||
def test_article_fenced_code():
|
||||
article = Article(content, None, None)
|
||||
assert """<pre><code class="language-python">""" in article.to_prose()
|
||||
|
||||
|
||||
def test_site_append_article():
|
||||
site = Site(".")
|
||||
article = site.add(content)
|
||||
|
||||
assert article in site.articles
|
||||
assert "python" in site.tags
|
||||
assert article in site.tags.get("python")
|
||||
|
|
Loading…
Reference in New Issue