diff --git a/pelicanconf.py b/pelicanconf.py index 3d7b2e0..32b68b7 100644 --- a/pelicanconf.py +++ b/pelicanconf.py @@ -60,7 +60,7 @@ BOOTSTRAP_THEME = 'darkly' HIDE_SIDEBAR = True PLUGIN_PATHS = ['plugins'] -PLUGINS = ['langmenu'] +PLUGINS = ['langmenu', 'talks'] MD_EXTENSIONS = ['toc'] diff --git a/plugins/talks.py b/plugins/talks.py new file mode 100644 index 0000000..17e6b9e --- /dev/null +++ b/plugins/talks.py @@ -0,0 +1,144 @@ +''' +Manage talks scheduling in a semantic way +''' + +from __future__ import print_function +import os +import io +from functools import wraps +import logging +import re +import datetime + +from docutils import nodes +from docutils.parsers.rst import directives, Directive + +from pelican import signals, generators +import jinja2 + + +TALKS_PATH = 'talks' + + +def memoize(function): + '''decorators to cache''' + memo = {} + + @wraps(function) + def wrapper(*args): + if args in memo: + return memo[args] + else: + rv = function(*args) + memo[args] = rv + return rv + return wrapper + + +@memoize +def get_talk_names(): + return [name for name in os.listdir(TALKS_PATH) + if not name.startswith('_') and + get_talk_data(name) is not None + ] + + +def warn(s): + print('WARN:', s) + + +@memoize +def get_talk_data(talkname): + fname = os.path.join(TALKS_PATH, talkname, 'meta.yaml') + if not os.path.isfile(fname): + return None + with io.open(fname, encoding='utf8') as buf: + data = yaml.load(buf) + if data is None: + return None + if 'title' not in data: + logging.warn("Talk <{}> has no `title` field".format(talkname)) + data['title'] = talkname + if 'text' not in data: + logging.warn("Talk <{}> has no `text` field".format(talkname)) + data['text'] = '' + if 'duration' not in data: + logging.warn("Talk <{}> has no `duration` field".format(talkname)) + data['duration'] = 50 + data['duration'] = int(data['duration']) + if 'room' not in data: + logging.warn("Talk <{}> has no `room` field".format(talkname)) + if 'time' not in data or 'day' not in data: + logging.error("Talk <{}> has no `time` or `day`".format(talkname)) + if 'time' in data: + timeparts = re.findall(r'\d+', str(data['time'])) + if 4 > len(timeparts) > 0: + timeparts = [int(p) for p in timeparts] + data['time'] = datetime.time(*timeparts) + else: + logging.error("Talk <{}> has malformed `time`".format(talkname)) + data['id'] = talkname + return data + + +jinja_env = jinja2.Environment( + loader=jinja2.FileSystemLoader(os.path.join(TALKS_PATH, '_templates')), + autoescape=True, +) + + +class TalkListDirective(Directive): + required_arguments = 0 + optional_arguments = 0 + final_argument_whitespace = True + has_content = True + + # TODO: use a jinja template + tmpl = u'''
Giorno {{day}} alle {{time}}
+ {% else %} +L'orario non รจ ancora stato fissato
+ {% endif %} + {% if room is defined %} +Stanza {{ room }}
+ {% endif %} +