/
plugin.py
75 lines (63 loc) · 2.27 KB
/
plugin.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
import os
import glob
from ...plugin import Plugin
from ...document import Document
from ... import helpers
class BlogPlugin(Plugin):
identity = "blog"
validity = {
"type": "object",
"properties": {
"path": {"type": "string"},
},
}
# Context
@property
def path(self):
path = self.document.project.config.get("blog", {}).get("path", "blog")
if os.path.isdir(path):
return path
@property
def relpath(self):
path = f"{self.path}/index.html"
return helpers.get_url_relpath(path, self.document.path)
@property
def items(self):
items = []
if not self.path:
return items
index_path = "/".join([self.path, "index"])
for document in self.document.project.documents:
if document.path.startswith(self.path) and document.path != index_path:
relpath = helpers.get_url_relpath(document.path, self.document.path)
items.append({"document": document, "relpath": relpath})
return items
@property
def author(self):
return self.config.get("author")
@property
def image(self):
return self.config.get("image")
@property
def date(self):
if self.path:
date = self.document.path.replace(f"{self.path}/", "")
return "-".join(date.split("-")[:3])
# Process
@staticmethod
def process_project(project):
path = project.config.get("blog", {}).get("path", "blog")
if os.path.isdir(path):
index_default = os.path.join(os.path.dirname(__file__), "index.md")
index_source = os.path.join(path, "index.md")
if not os.path.isfile(index_source):
helpers.copy_file(index_default, index_source)
for source in glob.glob(f"{path}/**/*.md", recursive=True):
if source != index_source:
item = Document(source, project=project)
project.documents.append(item)
def process_markup(self, markup):
markup.add_style("style.css")
if self.author:
markup.add_markup("markup.html", target="h1 + p", action="prepend")
markup.query('a[href="/blog/index.html"]').parent().add_class("active")