Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
New ical plugin
  • Loading branch information
ralsina committed Aug 16, 2016
1 parent d812509 commit f5010ff
Show file tree
Hide file tree
Showing 9 changed files with 157 additions and 10 deletions.
41 changes: 41 additions & 0 deletions v7/ical/README.md
@@ -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 %}}
```
12 changes: 12 additions & 0 deletions v7/ical/ical.plugin
@@ -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
74 changes: 74 additions & 0 deletions v7/ical/ical.py
@@ -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
1 change: 1 addition & 0 deletions v7/ical/requirements.txt
@@ -0,0 +1 @@
icalendar
11 changes: 11 additions & 0 deletions v7/ical/templates/jinja/calendar.tmpl
@@ -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>
11 changes: 11 additions & 0 deletions v7/ical/templates/mako/calendar.tmpl
@@ -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>
2 changes: 1 addition & 1 deletion v7/wiki/requirements.txt
@@ -1 +1 @@
creole
nxcreole
2 changes: 1 addition & 1 deletion v7/wiki/wiki.plugin
Expand Up @@ -4,7 +4,7 @@ Module = wiki

[Documentation]
Author = Roberto Alsina
Version = 0.2
Version = 0.3
Website = http://plugins.getnikola.com/#wiki
Description = Compile WikiMarkup into HTML

13 changes: 5 additions & 8 deletions v7/wiki/wiki.py
Expand Up @@ -30,11 +30,9 @@
import os

try:
from creole import Parser
from creole.html_emitter import HtmlEmitter
creole = True
import nxcreole
except ImportError:
creole = None
nxcreole = None

from nikola.plugin_categories import PageCompiler
from nikola.utils import makedirs, req_missing
Expand All @@ -48,14 +46,13 @@ class CompileWiki(PageCompiler):
supports_onefile = False

def compile_html(self, source, dest, is_two_file=True):
if creole is None:
req_missing(['creole'], 'build this site (compile CreoleWiki)')
if nxcreole is None:
req_missing(['nxcreole'], 'build this site (compile CreoleWiki)')
makedirs(os.path.dirname(dest))
with codecs.open(dest, "w+", "utf8") as out_file:
with codecs.open(source, "r", "utf8") as in_file:
data = in_file.read()
document = Parser(data).parse()
output = HtmlEmitter(document).emit()
output = nxcreole.render_xhtml(data)
out_file.write(output)

def create_post(self, path, **kw):
Expand Down

0 comments on commit f5010ff

Please sign in to comment.