diff --git a/Gemfile b/Gemfile index 3be9c3c..a941335 100644 --- a/Gemfile +++ b/Gemfile @@ -1,2 +1,3 @@ source "https://rubygems.org" gemspec +gem 'icalendar' diff --git a/_plugins/events_to_ics.rb b/_plugins/events_to_ics.rb new file mode 100644 index 0000000..c168a34 --- /dev/null +++ b/_plugins/events_to_ics.rb @@ -0,0 +1,42 @@ +require 'icalendar' + +module Jekyll + class IcsGenerator < Generator + safe true + priority :low + + def generate(site) + events = site.collections['events'].docs + + cal = Icalendar::Calendar.new + + events.each do |event| + title = event.data['title'] + date = event.data['event_date'] + event = Icalendar::Event.new + event.dtstart = date + event.dtend = date + event.summary = title + cal.add_event(event) + end + site.pages << IcalPage.new(site, site.source, 'feed', "events.ics", cal) + + puts "Generated .ics page from #{events.length} events" + end + end + + class IcalPage < Page + def initialize(site, base, dir, name, calendar) + @site = site + @base = base + @dir = dir + @name = name + + self.process(name) + self.content = calendar.to_ical + self.data = { + 'layout' => nil + } + end + end +end