forked from vmarkovtsev/veles-blog
-
Notifications
You must be signed in to change notification settings - Fork 0
/
libpelican.py
395 lines (324 loc) · 12.2 KB
/
libpelican.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
#! /usr/bin/env python
# -*- coding: utf-8 -*-
"""A library that provides usefull functions to interact with Pelican
blog posts.
"""
__author__ = 'quack1'
__version__ = '0.9.1'
__date__ = '2015-01-15'
__copyright__ = 'Copyright © 2013-2014, Quack1'
__licence__ = 'BSD'
__credits__ = ['Quack1']
__maintainer__ = 'Quack1'
__email__ = '[email protected]'
__status__ = 'Development'
import datetime
import os.path
import re
class PelicanBlog:
"""Represents a Pelican blog.
This class gives a clean an simple API to interact with a Pelican blog.
After being created with the base directory of the blog, functions allows
to obtain information about an article.
The information that's possible to get for an article are :
- author
- category
- datetime
- lang
- slug
- status
- summary
- tags
- title
- url
It is also possible to get information from the whole blog, like :
- A list of all posts
- The base URL
- The base directory
- All the drafts.
"""
_blog_directory = None
_content_directory = None
_blog_base_url = None
_blog_post_url_mask = None
_default_blog_post_url_mask = '{slug}.html'
_REGEX_BASE_URL = re.compile(r'SITEURL = \'(.*)\'', re.IGNORECASE)
_REGEX_ARTICLE_URL = re.compile(r'ARTICLE_URL = \'(.*)\'', re.IGNORECASE)
# publishconf.py takes precedence over pelicanconf.py
_pelican_conf_files = ['publishconf.py', 'pelicanconf.py']
def __init__(self, base_directory=""):
"""Initiate a new Pelican blog instance.
Args:
base_directory:
The directory in which the blog stands.
This directory **has to** contain the `content` directory
and the `pelicanconf.py` and `Makefile` files.
"""
self._blog_directory = base_directory
self._content_directory = os.path.join(self._blog_directory, 'content')
def _get_pelican_config_value(self, c):
"""
Get a config value from the Pelican config files
Args:
c:
The config value to search for (compiled regex)
"""
val = None
for conf in self._pelican_conf_files:
if val is None:
with open(os.path.join(self._blog_directory, conf)) as f:
for l in f:
res = c.search(l)
if res:
val = res.group(1)
break
return val
def get_site_base_url(self):
"""
Get the blog base URL from the pelican config files - Attribute SITEURL.
"""
if self._blog_base_url is None:
self._blog_base_url = self._get_pelican_config_value(
self._REGEX_BASE_URL)
if self._blog_base_url:
if not self._blog_base_url.endswith('/'):
self._blog_base_url += '/'
return self._blog_base_url
def get_site_post_url_mask(self):
"""
Get the site URL mask from the pelican config files - Attribute ARTICLE_URL.
"""
if self._blog_post_url_mask is None:
self._blog_post_url_mask = self._get_pelican_config_value(
self._REGEX_ARTICLE_URL)
if self._blog_post_url_mask is None:
self._blog_post_url_mask = self._default_blog_post_url_mask
return self._blog_post_url_mask
def _get_post_info(self, post_filename, post_info):
"""Get the value of one option field in a post file.
The file must be located in a `content` directory, located in the base
directory given in the parameter of the constructor.
The file which will be read will be `{base_directory}/content/{post_filename}`.
Args:
post_filename:
The filename of the article.
post_info:
The name of the wanted information.
The value returned will be get using this regular expression:
`{post_info}\s*:\s*(.*)$`
If `post_info` is 'Title', the regular expression will match
on the following line : "Title: My Very Good Blog Post".
The returned value will be "My Very Good Blog Post".
"""
regex = re.compile("^%s\s*:\s*(.*)$" % post_info, re.IGNORECASE)
with open(os.path.join(self._content_directory, post_filename),
"r") as f:
for line in f:
res = regex.search(line)
if res:
return res.group(1)
return ""
def get_base_directory(self):
"""Get the base directory of a blog instance.
Returns:
The base directory containing a blog instance
"""
return self._blog_directory
def get_content_directory(self):
"""Get the content directory of a blog instance.
Returns:
The content directory containing a blog instance
"""
return self._content_directory
def get_post_title(self, post_filename):
"""Get the title of a blog post.
Args:
post_filename:
The filename of the article.
Returns:
The title of the article, or a blank string.
"""
return self._get_post_info(post_filename, "Title")
def get_post_date(self, post_filename):
"""Get the date of a blog post.
Args:
post_filename:
The filename of the article.
Returns:
The date of the article in a **datetime** object.
If the date is not defined in the article, None is returned.
"""
date = self._get_post_info(post_filename, "Date").strip()
if date is None or date == "":
return None
else:
return datetime.datetime.strptime(date, "%Y-%m-%d %H:%M")
def get_post_authors(self, post_filename):
"""Get the author of a blog post.
Args:
post_filename:
The filename of the article.
Returns:
The author of the article, or a blank string.
"""
return self._get_post_info(post_filename, "Authors")
def get_post_category(self, post_filename):
"""Get the category of a blog post.
Args:
post_filename:
The filename of the article.
Returns:
The category of the article, or a blank string.
"""
return self._get_post_info(post_filename, "Category")
def get_post_tags(self, post_filename):
"""Get the tags of a blog post.
Args:
post_filename:
The filename of the article.
Returns:
A list containing all the tags of the article. If no tags is defined, the
list can contain only an empty string.
TODO: If the string returned by get_post_info is blank, return an empty
list.
"""
tags = self._get_post_info(post_filename, "Tags").split(',')
return [x.strip() for x in tags]
def get_post_triggers(self, post_filename):
"""Get the triggers of a blog post.
Args:
post_filename:
The filename of the article.
Returns:
A list containing all the tags of the article. If no triggers are defined, the
list can contain only an empty string.
TODO: If the string returned by get_post_info is blank, return an empty
list.
"""
tags = self._get_post_info(post_filename, "Triggers").split(',')
return [x.strip() for x in tags]
def get_post_slug(self, post_filename):
"""Get the slug of a blog post.
Args:
post_filename:
The filename of the article.
Returns:
The slug of the article, or a blank string.
TODO: If the slug option is not defined, generate the URL with
the Pelican core method.
"""
return self._get_post_info(post_filename, "Slug")
def get_post_summary(self, post_filename):
"""Get the summary of a blog post.
Args:
post_filename:
The filename of the article.
Returns:
The summary of the article, or a blank string.
"""
return self._get_post_info(post_filename, "Summary")
def get_post_lang(self, post_filename):
"""Get the lang of a blog post.
Args:
post_filename:
The filename of the article.
Returns:
The lang of the article, or a blank string.
"""
return self._get_post_info(post_filename, "Lang")
def get_post_status(self, post_filename):
"""Get the status of a blog post.
Args:
post_filename:
The filename of the article.
Returns:
The status of the article, or a blank string.
"""
return self._get_post_info(post_filename, "Status")
def is_draft(self, post_filename):
"""Returns the 'draft' status of a blog post.
If the uppercase status of the blog post is 'DRAFT', it is considered
as a draft.
Args:
post_filename:
The filename of the article.
Returns:
True if the blog post is a draft.
"""
return self.get_post_status(post_filename).upper() == 'DRAFT'
def posts_have_drafts(self, posts_filenames):
"""Look if a list of articles contains drafts.
Args:
posts_filenames:
A list containing the filename of the articles.
Returns:
True if at least one article in the list is a draft.
"""
for f in posts_filenames:
if self.get_post_status(f).upper() == 'DRAFT':
return True
return False
def get_post_url(self, post_filename):
"""Get the complete URL of a blog post.
The URL is generated by appending the post slug after the site
base URL.
Args:
post_filename:
The filename of the article.
Returns:
The complete URL to the article.
"""
return self.get_site_base_url() + self._translate_url_mask(
post_filename)
def _substitute_date_tags(self, path, date):
"""Substitute {date:%dateformat} tags in a string
Args:
path:
The path containing the tags.
date:
The date value to use for substitution
Returns:
The substituted path.
"""
regex = re.compile(r'\{date\:([^\}]+)\}')
match = regex.search(path)
while (match):
dpat = match.group(1)
res = date.strftime(dpat)
(s, e) = match.span()
path = path[:s] + res + path[e:]
match = regex.search(path)
return path
def _translate_url_mask(self, post_filename):
mask = self.get_site_post_url_mask()
post_date = self.get_post_date(post_filename)
url = mask.replace('{slug}', self.get_post_slug(post_filename))
url = url.replace('{lang}', self.get_post_lang(post_filename))
url = url.replace('{authors}', self.get_post_authors(post_filename))
url = url.replace('{category}', self.get_post_category(post_filename))
url = self._substitute_date_tags(url, post_date)
return url
def get_posts(self):
"""Get the list of all articles of the blog.
Returns:
A list containing all the articles of the blog.
"""
for post_filename in os.listdir(self._content_directory):
base, ext = os.path.splitext(post_filename)
if ext in ('.rst', '.md'):
yield post_filename
def get_drafts(self):
"""Get the list of all drafts of the blog.
To be considered as a draft, the status of the article (get with the
`get_post_status()` method), uppercase, has to be 'DRAFT'.
Returns:
A list containing all the drafts of the blog.
"""
l = os.listdir(self._content_directory)
posts = []
for post_filename in l:
base, ext = os.path.splitext(post_filename)
if ext in ('.rst', '.md'):
if self.get_post_status(post_filename).upper() == "DRAFT":
posts.append(post_filename)
return posts