-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmyplugins.py
99 lines (74 loc) · 2.23 KB
/
myplugins.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
import re
import markdown
from pelican import signals
REGEX_YEAR = r"\b(\d{4})\b"
###########################################################
# Filters
###########################################################
def filter_person2html(person, template):
'''
Convert a person information to HTML by using a template
Person is defined in the `people.json` file
'''
return template.render(p=person)
def filter_sort_mycates(categories, desc=True):
'''
Sort categories
'''
return sorted(categories, key=lambda v: v[0], reverse=True)
def filter_md2html(s):
'''
Convert a Markdown string to HTML
'''
return markdown.markdown(s)
def filter_get_year(date_str):
'''
Get the year in a string
'''
ms = re.findall(REGEX_YEAR, date_str)
if len(ms) == 0:
return ''
else:
return ms[0]
def filter_pg2path(page_name):
'''
Get the path according to page_name
'''
if page_name == 'index':
# ok it's the homepage
return '.'
else:
return '..'
def filter_pg2act_nav(output_file, target):
'''
Get the active nav item
'''
if output_file == target:
return 'active'
return ''
def filter_astitle(s):
'''
Convert a string to title
'''
return s.title()
def filter_fmtdate(date_str, fmt='%Y-%m-%d'):
'''
Format a date string
'''
return date_str.strftime(fmt)
def add_all_filters(pelican):
"""Add (register) all filters to Pelican."""
pelican.env.filters.update({"pg2path": filter_pg2path})
pelican.env.filters.update({"person2html": filter_person2html})
pelican.env.filters.update({"pg2act_nav": filter_pg2act_nav})
pelican.env.filters.update({"md2html": filter_md2html})
pelican.env.filters.update({"get_year": filter_get_year})
pelican.env.filters.update({"sort_mycates": filter_sort_mycates})
pelican.env.filters.update({"astitle": filter_astitle})
pelican.env.filters.update({"fmtdate": filter_fmtdate})
###########################################################
# Plugin for make special pages
###########################################################
def register():
"""Plugin registration."""
signals.generator_init.connect(add_all_filters)