class Jekyll::IcalTag

Constants

VERSION

Public Class Methods

new(tag_name, markup, parse_context) click to toggle source
Calls superclass method
# File lib/jekyll-ical-tag.rb, line 17
def initialize(tag_name, markup, parse_context)
  super
  @markup = markup
  @attributes = {}

  scan_attributes!
  set_limit!
  set_reverse!
  set_url!
  set_only!
  set_before_date!
  set_after_date!
end

Public Instance Methods

render(context) click to toggle source
# File lib/jekyll-ical-tag.rb, line 31
def render(context)
  context.registers[:ical] ||= Hash.new(0)

  result = []

  context.stack do
    url = get_dereferenced_url(context) || @url

    calendar_feed_coordinator = CalendarFeedCoordinator.new(
      url: url, only: @only, reverse: @reverse,
      before_date: @before_date, after_date: @after_date,
      limit: @limit
    )
    events = calendar_feed_coordinator.events
    event_count = events.length

    events.each_with_index do |event, index|
      # Init
      context["event"] = {}

      # Jekyll helper variables
      context["event"]["index"] = index

      # RFC 5545 conformant and custom properties.
      context["event"].merge!(event.all_properties)

      # Supported but non-standard attributes.
      context["event"]["attendees"] = event.attendees
      context["event"]["simple_html_description"] = event.simple_html_description

      # Overridden values
      context["event"]["url"] ||= event.description_urls.first

      # Deprecated attribute names.
      context["event"]["end_time"] = context["event"]["dtend"]
      context["event"]["start_time"] = context["event"]["dtstart"]

      context["forloop"] = {
        "name" => "ical",
        "length" => event_count,
        "index" => index + 1,
        "index0" => index,
        "rindex" => event_count - index,
        "rindex0" => event_count - index - 1,
        "first" => (index == 0),
        "last" => (index == event_count - 1),
      }

      result << nodelist.map do |n|
        if n.respond_to? :render
          n.render(context)
        else
          n
        end
      end.join
    end
  end

  result
end

Private Instance Methods

get_dereferenced_url(context) click to toggle source
# File lib/jekyll-ical-tag.rb, line 94
def get_dereferenced_url(context)
  return unless context.key?(@url)

  context[@url]
end
scan_attributes!() click to toggle source
# File lib/jekyll-ical-tag.rb, line 100
def scan_attributes!
  @markup.scan(Liquid::TagAttributes) do |key, value|
    @attributes[key] = value
  end
end
set_after_date!() click to toggle source
# File lib/jekyll-ical-tag.rb, line 144
def set_after_date!
  @after_date =
    begin
      Time.parse(@attributes["after_date"])
    rescue
      nil
    end
end
set_before_date!() click to toggle source
# File lib/jekyll-ical-tag.rb, line 135
def set_before_date!
  @before_date =
    begin
      Time.parse(@attributes["before_date"])
    rescue
      nil
    end
end
set_limit!() click to toggle source
# File lib/jekyll-ical-tag.rb, line 106
def set_limit!
  @limit = nil
  @limit = @attributes["limit"].to_i if @attributes["limit"]
end
set_only!() click to toggle source
# File lib/jekyll-ical-tag.rb, line 119
def set_only!
  only_future = @attributes["only_future"] == "true"
  only_past = @attributes["only_past"] == "true"

  raise "Set only_future OR only_past, not both" if only_future && only_past

  @only =
    if only_future
      :future
    elsif only_past
      :past
    else
      :all
    end
end
set_reverse!() click to toggle source
# File lib/jekyll-ical-tag.rb, line 111
def set_reverse!
  @reverse = @attributes["reverse"] == "true"
end
set_url!() click to toggle source
# File lib/jekyll-ical-tag.rb, line 115
def set_url!
  @url = @attributes["url"]
end