require "jekyll"
require "icalendar"
module Jekyll
module ICSReader
def read_calendar(input)
begin
calendar_file = File.open(input)
events = Icalendar::Event.parse(calendar_file)
hash = {}
counter = 0
# loop through the events in the calendars
# and map the values you want into a variable and then return it:
events.each do |event|
hash[counter] = {
"summary" => event.summary,
"dtstart" => event.dtstart,
"dtend" => event.dtend,
"description" => event.description
}
counter += 1
end
return hash
rescue
# Handle errors
Jekyll.logger.error "Calendar Reader:", "An error occurred!"
return {}
end
end
end
end
Liquid::Template.register_filter(Jekyll::ICSReader)
BEGIN:VEVENT
DTSTAMP:20050118T211523Z
UID:bsuidfortestabc123
DTSTART;TZID=US-Mountain:20050120T170000
DTEND;TZID=US-Mountain:20050120T184500
CLASS:PRIVATE
GEO:37.386013;-122.0829322
ORGANIZER:mailto:joebob@random.net
PRIORITY:2
SUMMARY:This is a really long summary to test the method of unfolding lines
\, so I'm just going to make it a whole bunch of lines.
ATTACH:http://bush.sucks.org/impeach/him.rhtml
ATTACH:http://corporations-dominate.existence.net/why.rhtml
RDATE;TZID=US-Mountain:20050121T170000,20050122T170000
X-TEST-COMPONENT;QTEST="Hello, World":Shouldn't double double quotes
END:VEVENT
BEGIN:VEVENT
DTSTAMP:20110118T211523Z
UID:uid-1234-uid-4321
DTSTART;TZID=US-Mountain:20110120T170000
DTEND;TZID=US-Mountain:20110120T184500
CLASS:PRIVATE
GEO:37.386013;-122.0829322
ORGANIZER:mailto:jmera@jmera.human
PRIORITY:2
SUMMARY:This is a very short summary.
RDATE;TZID=US-Mountain:20110121T170000,20110122T170000
END:VEVENT
// {{ events }}
{0=>{“summary”=>”This is a really long summary to test the method of unfolding lines, so I’m just going to make it a whole bunch of lines.”, “dtstart”=>#<DateTime: 2005-01-20T17:00:00+00:00 ((2453391j,61200s,0n),+0s,2299161j)>, “dtend”=>#<DateTime: 2005-01-20T18:45:00+00:00 ((2453391j,67500s,0n),+0s,2299161j)>, “description”=>nil}, 1=>{“summary”=>”This is a very short summary.”, “dtstart”=>#<DateTime: 2011-01-20T17:00:00+00:00 ((2455582j,61200s,0n),+0s,2299161j)>, “dtend”=>#<DateTime: 2011-01-20T18:45:00+00:00 ((2455582j,67500s,0n),+0s,2299161j)>, “description”=>nil}}
// {{ events[0] }}
{“summary”=>”This is a really long summary to test the method of unfolding lines, so I’m just going to make it a whole bunch of lines.”, “dtstart”=>#<DateTime: 2005-01-20T17:00:00+00:00 ((2453391j,61200s,0n),+0s,2299161j)>, “dtend”=>#<DateTime: 2005-01-20T18:45:00+00:00 ((2453391j,67500s,0n),+0s,2299161j)>, “description”=>nil}
// {{ events[1] }}
{“summary”=>”This is a very short summary.”, “dtstart”=>#<DateTime: 2011-01-20T17:00:00+00:00 ((2455582j,61200s,0n),+0s,2299161j)>, “dtend”=>#<DateTime: 2011-01-20T18:45:00+00:00 ((2455582j,67500s,0n),+0s,2299161j)>, “description”=>nil}
1条答案
按热度按时间ej83mcc01#
数据文件可能无法满足您的要求。您需要哲基尔插件。
下面是我将实现的一个临时解决方案,用于读取
ics
文件并将日历事件暴露给Jekyll中的流动变量:将
icalendar
添加到您的Gemfile
并进行安装:将此文件放入
_plugins/calendar_reader.rb
icalendar
的README.md
文档将帮助您理解如何从文件中读取数据,基本上,我们解析文件中的事件,并将其Map到字典中并返回。现在取一个ics文件并将其放入
_data
文件夹。_data/my_calendar.ics
此示例ics文件取自icalendar repository。
现在您可以使用markdown/html中的插件过滤器:
这里
read_calendar
是在_plugins/calendar_reader.rb
中定义的函数,_data/my_calendar.ics
是你想要从中获取数据的文件,插件获取input
作为_data/my_calendar.ics
,读取它并返回一个hash
,它存储在events
变量本身中。现在可以使用
{{ events }}
访问从插件文件中的函数返回的数据的散列。这是Jekyll
Filter
工作原理的基本框架,你可以更深入地了解其他类型的Jekyll插件,如docs中所解释的。