Skip to content

free-creations/sk_calendar

Repository files navigation

sk_calendar

This application demonstrates the use of the icalendar-rrule gem.

Here is the main program:

#! /usr/bin/ruby
# frozen_string_literal: true

require 'icalendar-rrule'
require 'sk_calendar/config'
require 'sk_calendar/event'

using Icalendar::Scannable

SkCalendar::Config.setup

# specify the time time span we want to show on our home page.
BEGIN_TIME =   Time.now              # from today
CLOSING_TIME = Date.new(2019, 1, 1)  # until end of the year

##
# Scans the the icalendar-file with the given name.
#
# @return[Array<Icalendar::Rrule::Occurrence>] a list of all events within the given time span.
def scan_calendar(filename)
  cal_file = File.open(filename)
  calendar = Icalendar::Calendar.parse(cal_file).first

  calendar.scan(BEGIN_TIME, CLOSING_TIME)
end

# scan calendars
scan_holidays = scan_calendar(SkCalendar::Config.holidays_calendar_file)
scan_sk       = scan_calendar(SkCalendar::Config.sk_calendar_file)
scan_month    = scan_calendar(SkCalendar::Config.month_file)

# merge the calendars into one.
full_scan = (scan_sk + scan_holidays + scan_month).sort

# prepare an empty file for output
html_file = File.new(SkCalendar::Config.outfile, 'w')

html_file.puts "<p>Stand #{I18n.l BEGIN_TIME, format: '%d. %B %Y'} </p>"

full_scan.each_with_index do |occurrence, index|
  # put a read-more ruler before the third event
  html_file.puts '<hr id="system-readmore"/>' if index == 3
  # format the event
  sk_event = SkCalendar::Event.new(occurrence)
  html_file.puts sk_event.to_html
end

html_file.close

The result will look as follows:

screenshot of web-formated calendar

About

This application demonstrates the use of the "icalendar-rrule" gem.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published