Class: CalendariumRomanum::SanctoraleLoader

Inherits:
Object
  • Object
show all
Defined in:
lib/calendarium-romanum/sanctoraleloader.rb

Overview

understands a plaintext calendar format and knows how to transform it to Celebrations and fill them in a Sanctorale

Format of the file: 1/31 m : S. Ioannis Bosco, presbyteri

<month>/<day> <rank shortcut> : <title> rank shortcut is optional, default value is optional memorial

Constant Summary collapse

RANK_CODES =
{
 nil => Ranks::MEMORIAL_OPTIONAL,
 'm' => Ranks::MEMORIAL_GENERAL,
 'f' => Ranks::FEAST_GENERAL,
 's' => Ranks::SOLEMNITY_GENERAL
}
COLOUR_CODES =
{
 nil => Colours::WHITE,
 'w' => Colours::WHITE,
 'v' => Colours::VIOLET,
 'g' => Colours::GREEN,
 'r' => Colours::RED
}

Instance Method Summary collapse

Instance Method Details

#load(src, dest = nil) ⇒ Object Also known as: load_from_string

dest should be a Sanctorale, src anything with #each_line


30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/calendarium-romanum/sanctoraleloader.rb', line 30

def load(src, dest=nil)
  dest ||= Sanctorale.new

  in_front_matter = false
  month_section = nil
  src.each_line.with_index(1) do |l, line_num|
    # skip YAML front matter
    if line_num == 1 && l.start_with?('---')
      in_front_matter = true
      next
    elsif in_front_matter
      if l.start_with?('---')
        in_front_matter = false
      end

      next
    end

    # strip whitespace and comments
    l.sub!(/#.*/, '')
    l.strip!
    next if l.empty?

    # month section heading
    n = l.match /^=\s*(\d+)\s*$/
    unless n.nil?
      month_section = n[1].to_i
      unless month_section >= 1 && month_section <= 12
        raise error("Invalid month #{month_section}", line_num)
      end
      next
    end

    # celebration record
    m = l.match /^((\d+)\/)?(\d+)\s*(([mfs])?(\d\.\d{1,2})?)?\s*([WVRG])?\s*:(.*)$/i
    if m.nil?
      raise error("Syntax error, line skipped '#{l}'", line_num)
      next
    end

    month, day, rank_char, rank_num, colour, title = m.values_at(2, 3, 5, 6, 7, 8)
    month ||= month_section
    day = day.to_i
    month = month.to_i

    rank = RANK_CODES[rank_char && rank_char.downcase]
    if rank.nil?
      raise error("Invalid celebration rank code #{rank_char}", line_num)
    end

    if rank_num
      rank_num = rank_num.to_f
      rank_by_num = Ranks[rank_num]

      if rank_by_num.nil?
        raise error("Invalid celebration rank code #{rank_num}", line_num)
      elsif rank_char && (rank.priority.to_i != rank_by_num.priority.to_i)
        raise error("Invalid combination of rank letter #{rank_char.inspect} and number #{rank_num}.", line_num)
      end

      rank = rank_by_num
    end

    dest.add month, day, Celebration.new(
                                         title.strip,
                                         rank,
                                         COLOUR_CODES[colour && colour.downcase]
                                        )
  end

  dest
end

#load_from_file(filename, dest = nil, encoding = 'utf-8') ⇒ Object


105
106
107
# File 'lib/calendarium-romanum/sanctoraleloader.rb', line 105

def load_from_file(filename, dest=nil, encoding='utf-8')
  self.load File.open(filename, 'r', encoding: encoding), dest
end