Skip to content
Permalink
Browse files

Merge pull request #160 from getnikola/ical

New ical plugin
  • Loading branch information
ralsina committed Aug 16, 2016
2 parents b6ac99f + 5e8a205 commit 626c734212ce586cd283086da3ace6c1321e40ac
@@ -0,0 +1,41 @@
**NOTE:** This plugin needs a Nikola release greater than 7.7.12. If you can't see such a release, then it still requires unreleased master from GitHub.


This plugin implements a shortcode to display calendar information.

The calendar information is provided in iCalendar format, either via an
external file, or embedded in your document.

Example with external file:

```
{{% calendar file=my_event.ical %}}
```

Example with embedded calendar:

```
{{% calendar %}}
BEGIN:VCALENDAR
VERSION:2.0
PRODID:-//hacksw/handcal//NONSGML v1.0//EN
BEGIN:VEVENT
UID:uid1@example.com
DTSTAMP:19970714T170000Z
ORGANIZER;CN=John Doe:MAILTO:john.doe@example.com
DTSTART:19970714T170000Z
DTEND:19970715T035959Z
SUMMARY:Bastille Day Party
END:VEVENT
END:VCALENDAR
{{% /calendar %}}
```

The plugin provides simple templates both for Mako and Jinja, but if you want to use
a different template, just use the ``template`` argument (it will be loaded from the theme
or from ``templates/``:

```
{{% calendar file=my_event.ical template=my_fancy_template.tmpl %}}
```
@@ -0,0 +1,12 @@
[Core]
Name = ical
Module = ical

[Documentation]
Author = Roberto Alsina
Version = 1.0
Website = https://getnikola.com
Description = Calendar shortcode

[Nikola]
plugincategory = ShortcodePlugin
@@ -0,0 +1,74 @@
# -*- coding: utf-8 -*-

# Copyright © 2016 Roberto Alsina and others

# This plugin is based on the Pelican ical plugin
# available at https://github.com/getpelican/pelican-plugins/tree/master/ical
#
# This plugin is licensed under the
# GNU AFFERO GENERAL PUBLIC LICENSE Version 3
# according to the license guidelines from the Pelican plugin repo
#
# Original code is written by:
#
# Julien Ortet: https://github.com/cozo
# Justin Mayer: https://github.com/justinmayer
# calfzhou: https://github.com/calfzhou

from __future__ import print_function

import icalendar as ical

from nikola.plugin_categories import ShortcodePlugin
from nikola.utils import LocaleBorg


class CalendarPlugin(ShortcodePlugin):
""" Calendar shortcode. """
name = "ical"

doc_purpose = "Display ical calendars"
doc_description = "Format and display ical calendars."
logger = None
cmd_options = []

def set_site(self, site):
super(CalendarPlugin, self).set_site(site)
self.site.register_shortcode('calendar', self.render_calendar)

def render_calendar(self, site=None, data=None, lang=None, file=None, template=None):
if template is not None:
template = 'calendar.tmpl'
deps = self.site.template_system.template_deps(template)

if file is not None:
with open(file, 'rb') as inf:
data = inf.read()
deps.append(file)
cal = ical.Calendar.from_ical(data)

events = []
for element in cal.walk():
eventdict = {}
if element.name == "VEVENT":
if element.get('summary') is not None:
eventdict['summary'] = element.get('summary')
if element.get('description') is not None:
eventdict['description'] = element.get('description')
if element.get('url') is not None:
eventdict['url'] = element.get('url')
if element.get('dtstart') is not None:
eventdict['dtstart'] = element.get('dtstart').dt
if element.get('dtend') is not None:
eventdict['dtend'] = element.get('dtend').dt
events.append(eventdict)

output = self.site.render_template(
template,
None,
{
'events': events,
'lang': LocaleBorg().current_lang,
})

return output, deps
@@ -0,0 +1 @@
icalendar
@@ -0,0 +1,11 @@
<dl>
{% for event in events %}
<dt>{{ event.summary }}</dt>
<dd>{{ event.description|replace('\n\n', '<br>') }}</dd>
<dd>{{ event.dtstart }}</dd>
<dd>{{ event.dtend }}</dd>
{% if event.url %}
<dd class="footer"><a href="{{ event.url }}">See more</a></dd>
{% endif %}
{% endfor %}
</dl>
@@ -0,0 +1,11 @@
<dl>
% for event in events:
<dt>${event.get('summary', '')}</dt>
<dd>${event.get('description', '')}</dd>
<dd>${event.get('dtstart', '')}</dd>
<dd>${event.get('dtend', '')}</dd>
% if event.get('url'):
<dd class="footer"><a href="${event['url']}">See more</a></dd>
% endif
% endfor
</dl>

0 comments on commit 626c734

Please sign in to comment.
You can’t perform that action at this time.