adding ical plugin
This commit is contained in:
28
ical/Readme.rst
Normal file
28
ical/Readme.rst
Normal file
@@ -0,0 +1,28 @@
|
||||
ical
|
||||
--------
|
||||
|
||||
This plugin read the calendar defined in the page metadata : calendar :
|
||||
|
||||
with::
|
||||
|
||||
:calendar: /path/to/your/ics/file
|
||||
|
||||
Example of code that can be added in page template ::
|
||||
|
||||
|
||||
{% if page.calendar %}
|
||||
<dl>
|
||||
{% for vevent in events[page.slug] %}
|
||||
<dt>{{ vevent.summary }}</dt>
|
||||
<dd>{{ vevent.description|replace('\n\n', '<br>') }}</dd>
|
||||
<dd>{{ vevent.dtstart }}</dd>
|
||||
<dd>{{ vevent.dtend }}</dd>
|
||||
<dd class="footer"><a href="{{ vevent.url }}" target="_blank">See more</a></dd>
|
||||
{% endfor %}
|
||||
</dl>
|
||||
{% endif %}
|
||||
|
||||
this plugins needs icalendar module installed::
|
||||
|
||||
pip install icalendar
|
||||
|
||||
1
ical/__init__.py
Normal file
1
ical/__init__.py
Normal file
@@ -0,0 +1 @@
|
||||
from .ical import *
|
||||
52
ical/ical.py
Normal file
52
ical/ical.py
Normal file
@@ -0,0 +1,52 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
"""
|
||||
ical plugin for Pelican
|
||||
===========================
|
||||
|
||||
This plugin parse the calendars defined in pages metadata :calendar:
|
||||
One calendar per page
|
||||
|
||||
"""
|
||||
|
||||
from icalendar import Calendar, Event
|
||||
from pelican import signals , utils
|
||||
import pytz
|
||||
import datetime
|
||||
import os.path
|
||||
|
||||
def init_cal(generator):
|
||||
# initialisation of the dictionnary of calendar
|
||||
# you can add one calendar per page
|
||||
calDict = {}
|
||||
generator.context['events'] = calDict
|
||||
|
||||
def add_ical(generator, metadata):
|
||||
# check if a calendar is here
|
||||
if 'calendar' in metadata.keys():
|
||||
summ = []
|
||||
path = metadata['calendar']
|
||||
if not os.path.isabs(path):
|
||||
path = os.path.abspath(metadata['calendar'])
|
||||
cal = Calendar.from_ical(open(path,'rb').read())
|
||||
for element in cal.walk():
|
||||
eventdict = {}
|
||||
if element.name == "VEVENT":
|
||||
if element.get('summary') != None:
|
||||
eventdict['summary'] = element.get('summary')
|
||||
if element.get('description') != None:
|
||||
eventdict['description'] = element.get('description')
|
||||
if element.get('url') != None:
|
||||
eventdict['url'] = element.get('url')
|
||||
if element.get('dtstart') != None:
|
||||
eventdict['dtstart'] = element.get('dtstart').dt
|
||||
if element.get('dtend') != None:
|
||||
eventdict['dtend'] = element.get('dtend').dt
|
||||
summ.append(eventdict)
|
||||
# the id of the calendar is the slugify name of the page
|
||||
calId = utils.slugify(metadata['title'])
|
||||
generator.context['events'][calId] = summ
|
||||
|
||||
|
||||
def register():
|
||||
signals.pages_generator_init.connect(init_cal)
|
||||
signals.pages_generate_context.connect(add_ical)
|
||||
Reference in New Issue
Block a user