1
0
Fork 0

inizio a fare il plugin per i talk

This commit is contained in:
boyska 2017-05-03 01:43:18 +02:00
parent 2be9f8b9a6
commit f1b402769f
No known key found for this signature in database
GPG key ID: 7395DCAE58289CA9
4 changed files with 181 additions and 1 deletions

View file

@ -60,7 +60,7 @@ BOOTSTRAP_THEME = 'darkly'
HIDE_SIDEBAR = True HIDE_SIDEBAR = True
PLUGIN_PATHS = ['plugins'] PLUGIN_PATHS = ['plugins']
PLUGINS = ['langmenu'] PLUGINS = ['langmenu', 'talks']
MD_EXTENSIONS = ['toc'] MD_EXTENSIONS = ['toc']

144
plugins/talks.py Normal file
View file

@ -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'''<div id="talk-{t[id]}">
<h3 class="talk-title">{t[title]}</h3>
<div class="talk-description">{t[text]}</div>
</div>'''
def run(self):
tmpl = jinja_env.get_template('talk.html')
return [
nodes.raw('', tmpl.render(**get_talk_data(n)),
format='html')
for n in get_talk_names()
]
class TalksGenerator(generators.Generator):
def __init__(self, *args, **kwargs):
self.talks = []
super(TalksGenerator, self).__init__(*args, **kwargs)
def generate_context(self):
self.talks.append('sblindola')
self.talks.append('tapioco')
if os.path.isdir(self.settings['TALKS_PATH']):
self.talks += os.listdir(self.settings['TALKS_PATH'])
self._update_context(('talks', ))
def add_talks_option_defaults(pelican):
pelican.settings.setdefault('TALKS_PATH', TALKS_PATH)
def get_generators(gen):
return TalksGenerator
try:
import yaml
except ImportError:
print('ERROR: yaml not found. Talks plugins will be disabled')
def register():
pass
else:
def register():
#signals.get_generators.connect(get_generators)
signals.initialized.connect(add_talks_option_defaults)
directives.register_directive('talklist', TalkListDirective)

View file

@ -12,3 +12,4 @@ six==1.9.0
smartypants==1.8.6 smartypants==1.8.6
typogrify==2.0.7 typogrify==2.0.7
Unidecode==0.4.17 Unidecode==0.4.17
PyYAML==3.12

View file

@ -0,0 +1,35 @@
<div id="talk-{{id}}">
<h3 class="talk-title">{{title}}</h3>
<div class="talk-info">
{% if time is defined and day is defined %}
{# Dai un nome meglio ai giorni #}
<p>Giorno {{day}} alle {{time}}</p>
{% else %}
<p><i>L'orario non è ancora stato fissato</i></p>
{% endif %}
{% if room is defined %}
<p>Stanza {{ room }}</p>
{% endif %}
</div>
<div class="talk-description">{{text}}</div>
{% if links is defined or resources is defined or mail is defined %}
<div class="talk-resources">
<h4>Link utili:</h4>
<ul>
{% if links is defined: %}
{% for link in links %}
<li>{{link}}</li>
{% endfor %}
{% endif %}
{% if resources is defined: %}
<li><a href="{{resources}}">Materiali</a></li>
{% endif %}
{% if mail is defined: %}
<li><a href="{{mail}}">Mail</a></li>
{% endif %}
</ul>
{% endif %}
</div>
</div>
{# vim: set ft=jinja: #}