www.netz39.de/_plugins/events_to_ics.rb

74 lines
2.4 KiB
Ruby
Raw Normal View History

2024-03-13 22:19:48 +01:00
require 'icalendar'
require 'redcarpet'
require 'redcarpet/render_strip'
2024-03-13 22:19:48 +01:00
module Jekyll
class IcsGenerator < Generator
safe true
priority :low
def generate(site)
events = site.collections['events'].docs
default_organizer = "Netz39 Team <kontakt@netz39.de>"
default_location = "Netz39 e.V., Leibnizstraße 32, 39104 Magdeburg"
2024-03-14 22:40:20 +01:00
default_duration = Rational(4, 24)
2024-03-13 22:19:48 +01:00
cal = Icalendar::Calendar.new
events.each do |event|
title = event.data['title']
2024-03-14 22:22:31 +01:00
start_date = event.data.dig('event', 'start') || event.data['event_date']
2024-03-14 22:40:20 +01:00
end_date = event.data.dig('event', 'end') || event.data['event_date'] || start_date + default_duration
organizer = event.data.dig('event', 'organizer') || default_organizer
location = event.data.dig('event', 'location') || default_location
2024-04-01 22:28:35 +02:00
# Skip events older than 365 days
next if start_date.to_date < (Date.today - 365)
# Remove image URLs from description
content = event.content
content.gsub!(/\!\[.*?\]\((.*?)\)/, '')
# Render Markdown content to plain text
markdown_parser = Redcarpet::Markdown.new(Redcarpet::Render::StripDown)
2024-04-11 01:27:38 +02:00
description = markdown_parser.render(content) + "\nMehr Infos unter #{site.config['url']}#{event.url}"
# Create new event and set its properties
ical_event = Icalendar::Event.new
2024-03-14 22:22:31 +01:00
if start_date > end_date
raise StandardError.new "#{File.basename(event.path)}: Start date must not be greater than end date"
end
if start_date < end_date
ical_event.dtstart = start_date
ical_event.dtend = end_date
else
2024-03-14 22:22:31 +01:00
ical_event.dtstart = Icalendar::Values::Date.new(start_date)
ical_event.dtend = Icalendar::Values::Date.new(end_date)
end
ical_event.summary = title
ical_event.description = description
ical_event.organizer = organizer
ical_event.location = location
cal.add_event(ical_event)
2024-03-13 22:19:48 +01:00
end
2024-03-13 23:10:20 +01:00
site.pages << IcalPage.new(site, site.source, 'feed/eo-events', "events.ics", cal)
2024-03-13 22:19:48 +01:00
2024-03-13 23:10:20 +01:00
puts "Generated events.ics page from #{events.length} events"
2024-03-13 22:19:48 +01:00
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