From 8448436a46b10ba54a2ce27c8eadfe03b0bd62a6 Mon Sep 17 00:00:00 2001 From: Lorin Werthen Date: Wed, 27 Jul 2016 00:13:36 +0200 Subject: [PATCH] add ical support' --- Gemfile | 3 +++ Gemfile.lock | 2 ++ Rules | 18 ++++++++++++++++++ content/{feed.erb => feed.xml.erb} | 0 content/ical.ics.erb | 1 + lib/filters/ical_filter.rb | 9 +++++++++ lib/helpers/ical.rb | 18 ++++++++++++++++++ lib/{helpers/helpers.rb => helpers_.rb} | 1 + 8 files changed, 52 insertions(+) rename content/{feed.erb => feed.xml.erb} (100%) create mode 100644 content/ical.ics.erb create mode 100644 lib/filters/ical_filter.rb create mode 100644 lib/helpers/ical.rb rename lib/{helpers/helpers.rb => helpers_.rb} (90%) diff --git a/Gemfile b/Gemfile index af0876d..0a3fe3d 100644 --- a/Gemfile +++ b/Gemfile @@ -13,6 +13,9 @@ gem 'builder' # Checks gem 'w3c_validators' +# ical files +gem 'icalendar' + group :development do gem 'adsf' gem 'guard-nanoc' diff --git a/Gemfile.lock b/Gemfile.lock index 6c70245..e780af9 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -32,6 +32,7 @@ GEM nanoc (~> 4.0) hamster (3.0.0) concurrent-ruby (~> 1.0) + icalendar (2.4.0) json (2.0.1) kramdown (1.11.1) listen (3.1.5) @@ -79,6 +80,7 @@ DEPENDENCIES builder coffee-script guard-nanoc + icalendar kramdown nanoc (= 4.2.4) nokogiri diff --git a/Rules b/Rules index ba42946..111b328 100644 --- a/Rules +++ b/Rules @@ -2,6 +2,7 @@ # frozen_string_literal: true require 'json' +require 'icalendar' # # @@ -83,6 +84,10 @@ compile '/posts/**/*', rep: :text do filter :strip_html end +compile '/posts/**/*', rep: :ical do + filter :ical +end + # # GENERIC ERB PAGES # @@ -90,6 +95,10 @@ compile '/tipuesearch_content.js.erb' do filter :erb end +compile '/**/*.ics.erb' do + filter :erb +end + compile '/*.erb' do layout '/generic.*' layout '/default.*' @@ -142,6 +151,15 @@ route '/assets/scripts/**/*' do end # TEXT +route '/*.ics.erb' do + item.identifier.without_ext +end + +# POSTS +route '/posts/**/*', rep: :ical do + "#{item.identifier.without_ext}.ics" +end + route '/**/*.{erb,html,md}' do "#{item.identifier.without_ext}/index.html" end diff --git a/content/feed.erb b/content/feed.xml.erb similarity index 100% rename from content/feed.erb rename to content/feed.xml.erb diff --git a/content/ical.ics.erb b/content/ical.ics.erb new file mode 100644 index 0000000..2b4d96e --- /dev/null +++ b/content/ical.ics.erb @@ -0,0 +1 @@ +<%= event_calendar %> diff --git a/lib/filters/ical_filter.rb b/lib/filters/ical_filter.rb new file mode 100644 index 0000000..a5c8acc --- /dev/null +++ b/lib/filters/ical_filter.rb @@ -0,0 +1,9 @@ +class IcalFilter < Nanoc::Filter + identifier :ical + + def run(_content, _params = {}) + cal = Icalendar::Calendar.new + cal.add_event(event_for(item)) + cal.to_ical + end +end diff --git a/lib/helpers/ical.rb b/lib/helpers/ical.rb new file mode 100644 index 0000000..22a3b55 --- /dev/null +++ b/lib/helpers/ical.rb @@ -0,0 +1,18 @@ +module IcalHelper + def event_calendar + cal = Icalendar::Calendar.new + + events = items.find_all('/posts/**/*').map { |i| event_for(i) } + events.each { |e| cal.add_event(e) } + + cal.to_ical + end + + def event_for(i) + event = Icalendar::Event.new + event.dtstart = Date.parse(i[:time]) + event.summary = 'A great event!' + + event + end +end diff --git a/lib/helpers/helpers.rb b/lib/helpers_.rb similarity index 90% rename from lib/helpers/helpers.rb rename to lib/helpers_.rb index be8a7a4..86f3a76 100644 --- a/lib/helpers/helpers.rb +++ b/lib/helpers_.rb @@ -5,3 +5,4 @@ include Nanoc::Helpers::Rendering include Nanoc::Helpers::Text include ArchiveHelper +include IcalHelper