-
Notifications
You must be signed in to change notification settings - Fork 5
/
event.py
279 lines (206 loc) · 8.12 KB
/
event.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
import datetime
import fnmatch
import logging
import os
from operator import attrgetter
from os.path import join, exists, relpath, dirname
import frontmatter
import markdown
from dateutil import parser as dateparser
CONTENT_DIR = './content/events'
DEFAULT_DATE_FORMAT = '%Y-%m-%d'
def _recursive_glob(rootdir='.', pattern='*'):
matches = []
for root, dirnames, filenames in os.walk(rootdir):
for filename in fnmatch.filter(filenames, pattern):
matches.append(os.path.join(root, filename))
return matches
class Tags:
NOPAGE = 'nopage'
NOLIST = 'nolist'
LONGLIVE = 'longlive'
FEATURED = 'featured'
DRAFT = 'draft'
class Metadata:
NAME = "name"
START = "start"
END = "end"
LOCATION = "location"
SUBTITLE = "subtitle"
SUMMARY = "summary"
URL = "url"
TAGS = "tags"
class Event:
@staticmethod
def REQUIRED_FIELDS():
return Metadata.NAME, Metadata.START, Metadata.END
@staticmethod
def OPTIONAL_FIELDS():
return Metadata.LOCATION, Metadata.SUBTITLE, Metadata.SUMMARY, Metadata.URL, Metadata.TAGS
@staticmethod
def ALL_FIELDS():
return Event.REQUIRED_FIELDS() + Event.OPTIONAL_FIELDS()
@staticmethod
def VALID_TAGS():
return Tags.NOPAGE, Tags.NOLIST, Tags.LONGLIVE, Tags.FEATURED, Tags.DRAFT
def __init__(self):
# front matter fields
for f in Event.ALL_FIELDS():
setattr(self, f, None)
# generated
self.date_str = None
self.output_path = None
# XXX: hardcoded :/
self.type = 'event'
def __str__(self):
return self.__unicode__()
def __unicode__(self):
s = "- Event: [%s] (%s): " % (self.name, self._format_date(False))
return s.encode('utf-8')
def _parse_metadata(self, front_matter):
# mandatory fields
for mf in Event.REQUIRED_FIELDS():
setattr(self, mf, front_matter[mf])
# optional fields
for of in Event.OPTIONAL_FIELDS():
setattr(self, of, front_matter.get(of, ""))
if 'external' in front_matter:
logging.warning('Warning: deprecated tags used')
self.url = None
self.tags = None
if "url" in front_matter and front_matter[Metadata.URL] != '':
self.url = front_matter[Metadata.URL]
if "tags" in front_matter and len(front_matter[Metadata.TAGS]) > 0:
self.tags = front_matter[Metadata.TAGS]
# validate date format
def parse_if_not_date_obj(obj):
if not isinstance(obj, datetime.date):
return dateparser.parse(obj).date()
else:
return obj
self.start = parse_if_not_date_obj(self.start)
self.end = parse_if_not_date_obj(self.end)
self.date_str = self._format_date()
def _format_date(self, attach_icon=True):
s = self.start
t = self.end
if attach_icon:
if self.end >= datetime.date.today():
cal_icon = '<i class="calendar plus icon"></i>'
else:
cal_icon = '<i class="calendar check icon"></i>'
else:
cal_icon = ''
full_format = '%A %B %e, %Y '
short_full_format = '%B %e, %Y '
if s == t:
date = '%s%s' % (cal_icon, s.strftime(full_format))
return date.strip()
if s.month == t.month and s.year == t.year:
date = '%s%s %d-%d, %d' % (cal_icon, s.strftime("%B"), s.day, t.day, t.year)
return date.strip()
r = '%s%s-%s' % (cal_icon, s.strftime(short_full_format), t.strftime(short_full_format))
return r.strip()
def _start_date_str(self):
return self.start.strftime(DEFAULT_DATE_FORMAT)
def _end_date_str(self):
return self.end.strftime(DEFAULT_DATE_FORMAT)
def parse_md(self, ssg_engine, md_src):
logging.debug(f'processing {md_src}')
if not exists(md_src):
raise Exception('%s does not exist' % md_src)
post = frontmatter.load(md_src)
front = post.metadata
content = markdown.markdown(post.content, extensions=['markdown.extensions.tables'])
self._parse_metadata(front)
output_fn = self._start_date_str() + '-' + ssg_engine.sanitize_filename(self.name) + '.html'
relative_path = join('events', output_fn)
if self.url or self.has_tag(Tags.NOPAGE):
self.output_path = None
else:
self.url = relative_path
self.output_path = join(ssg_engine.output_dir, relative_path)
self.render_cntx = ssg_engine.default_context()
# compute the relative path to the root of website
self.render_cntx['SITE_ROOT'] = relpath(ssg_engine.output_dir, dirname(self.output_path))
self.render_cntx['title'] = self.name
self.render_cntx['metadata'] = self.to_dict()
self.render_cntx['content'] = content
def write_file(self, ssg_engine):
if not self.output_path:
return
output_dir = join(ssg_engine.output_dir, 'events')
if not exists(output_dir):
os.mkdir(output_dir)
if not self.render_cntx:
raise Exception("render context not ready")
temp = ssg_engine.env.get_template('event_details.html')
html = temp.render(self.render_cntx)
logging.debug(f'writing to {self.output_path}')
ssg_engine.write(html.encode('utf-8'), self.output_path)
def has_tag(self, tag):
if tag not in Event.VALID_TAGS():
raise Exception("invalid tag: %s" % tag)
return self.tags and tag in self.tags
def has_expired(self, expire_in_days):
if self.has_tag(Tags.LONGLIVE):
# extend the life time by a week
expire_in_days += 7
today = datetime.date.today()
tdiff = today - self.end
return tdiff.days >= expire_in_days
def to_dict(self):
r = {}
for f in Event.ALL_FIELDS():
r[f] = getattr(self, f)
r['date_str'] = getattr(self, 'date_str')
return r
def get_event_list(env, expire_in_days=None):
"""
get a list of events
:param env: context
:param expire_in_days: define the life time of an event. If None, events never expire.
:return: a list of event, reversely sorted by the starting date.
"""
events = []
markdown_files = _recursive_glob(CONTENT_DIR, '*.md')
for f in markdown_files:
if f.endswith('template.md'):
continue
e = Event()
e.parse_md(env, f)
events.append(e)
if expire_in_days:
events = [ev for ev in events if not ev.has_expired(expire_in_days=expire_in_days)]
event_metadata = sorted(events, key=attrgetter('start'), reverse=True)
return event_metadata
def get_upcoming_events(e, expire_in_days=None):
logging.info('Getting upcoming events')
event_list = get_event_list(e, expire_in_days=expire_in_days)
ongoing = [x for x in event_list if x.end >= datetime.date.today()]
return ongoing
def get_featured_events(e, expire_in_days):
"""
:param e: template engine needed here [why? I forgot]
:return: a list of upcoming or featured events
"""
logging.info('Getting featured events')
event_list = get_event_list(e, expire_in_days)
today = datetime.date.today()
featured = [ev for ev in event_list if ev.has_tag('featured') or ev.end >= today]
return featured
def write_event_list_page(e):
event_list = get_event_list(e, expire_in_days=None)
for ev in event_list:
ev.write_file(e)
ongoing = [x for x in event_list if x.end >= datetime.date.today() and (not x.has_tag(Tags.NOLIST))]
past = [x for x in event_list if x.end < datetime.date.today()]
event_index_temp = e.env.get_template('event_list.html')
output = e.calc_output_fullpath('events.html')
breadcrumb = [{'name': 'Events', 'url': 'events.html'}]
e.render_and_write(event_index_temp,
dict(title='Events',
ongoing=ongoing,
past=past,
breadcrumb=breadcrumb),
output)