ruby 解析ICS文件中的数据并将其暴露给模板中的liquid变量

46qrfjad  于 2023-02-18  发布在  Ruby
关注(0)|答案(1)|浏览(98)

我已经不知疲倦地在谷歌上搜索这个,但不幸的是,每一个搜索碰撞的事实杰基尔是一个网站生成器和结果没有帮助。
我正在寻找一个如何从插件/生成器读取ICS文件的小例子,然后从模板与液体访问。
我试过创建集合并在插件中添加到它们,我试过创建site.data数组。似乎什么都不起作用。有没有一个jekyll插件的小例子,它读取文件或url并创建数据,然后存储在一个站点变量中,并可以通过liquid访问?具体来说,我将阅读ICS提要并创建一个日历。

ej83mcc0

ej83mcc01#

数据文件可能无法满足您的要求。您需要哲基尔插件。
下面是我将实现的一个临时解决方案,用于读取ics文件并将日历事件暴露给Jekyll中的流动变量:
icalendar添加到您的Gemfile并进行安装:

bundle add icalendar
bundle install

将此文件放入_plugins/calendar_reader.rb

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)

icalendarREADME.md文档将帮助您理解如何从文件中读取数据,基本上,我们解析文件中的事件,并将其Map到字典中并返回。
现在取一个ics文件并将其放入_data文件夹。
_data/my_calendar.ics

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

此示例ics文件取自icalendar repository
现在您可以使用markdown/html中的插件过滤器:

{% assign events = "_data/my_calendar.ics" | read_calendar %}

这里read_calendar是在_plugins/calendar_reader.rb中定义的函数,_data/my_calendar.ics是你想要从中获取数据的文件,插件获取input作为_data/my_calendar.ics,读取它并返回一个hash,它存储在events变量本身中。
现在可以使用{{ events }}访问从插件文件中的函数返回的数据的散列。

// {{ 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}

这是Jekyll Filter工作原理的基本框架,你可以更深入地了解其他类型的Jekyll插件,如docs中所解释的。

相关问题