Class: Jekyll::Premonition::Resources

Inherits:
Object
  • Object
show all
Defined in:
lib/premonition/resources.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(site_config) ⇒ Resources

Returns a new instance of Resources.



7
8
9
10
# File 'lib/premonition/resources.rb', line 7

def initialize(site_config)
  @config = load site_config
  @markdown = Converters::Markdown.new site_config
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



4
5
6
# File 'lib/premonition/resources.rb', line 4

def config
  @config
end

#markdownObject (readonly)

Returns the value of attribute markdown.



5
6
7
# File 'lib/premonition/resources.rb', line 5

def markdown
  @markdown
end

Instance Method Details

#default_configObject



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/premonition/resources.rb', line 24

def default_config
  {
    'default' => {
      'template' => '<div class="premonition {{type}}"><div class="fa {{meta.fa-icon}}"></div>'\
        '<div class="content">{% if header %}<p class="header">{{title}}</p>{% endif %}{{content}}</div></div>',
      'meta' => { 'fa-icon' => 'fa-check-square' },
      'title' => nil
    },
    'types' => {
      'note' => { 'meta' => { 'fa-icon' => 'fa-check-square' } },
      'info' => { 'meta' => { 'fa-icon' => 'fa-info-circle' } },
      'warning' => { 'meta' => { 'fa-icon' => 'fa-exclamation-circle' } },
      'error' => { 'meta' => { 'fa-icon' => 'fa-exclamation-triangle' } }
    }
  }
end

#fail(msg) ⇒ Object

Raises:

  • (LoadError)


70
71
72
73
# File 'lib/premonition/resources.rb', line 70

def fail(msg)
  Jekyll.logger.error 'Fatal (Premonition):', msg
  raise LoadError, msg
end

#load(site_config) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
# File 'lib/premonition/resources.rb', line 12

def load(site_config)
  cfg = default_config
  p = site_config['premonition'] || {}
  df = p['default'] || {}
  validate_defaults df, p
  cfg['default']['template'] = df['template'].strip unless df['template'].nil?
  cfg['default']['title'] = df['title'].strip unless df['title'].nil?
  cfg['default']['meta'] = cfg['default']['meta'].merge(df['meta']) unless df['meta'].nil?
  load_types p, cfg
  cfg
end

#load_types(p, cfg) ⇒ Object



46
47
48
49
50
51
52
53
# File 'lib/premonition/resources.rb', line 46

def load_types(p, cfg)
  return if p['types'].nil?
  p['types'].each do |id, obj|
    t = type_config id, obj
    cfg['types'][id] = cfg['types'][id].merge(t) unless cfg['types'][id].nil?
    cfg['types'][id] = t if cfg['types'][id].nil?
  end
end

#type_config(id, t) ⇒ Object



55
56
57
58
59
60
61
62
# File 'lib/premonition/resources.rb', line 55

def type_config(id, t)
  validate_type(id, t)
  {
    'template' => t['template'].nil? ? nil : t['template'].strip,
    'default_title' => t['default_title'].nil? || t['default_title'].empty? ? nil : t['default_title'].strip,
    'meta' => t['meta'].nil? ? {} : t['meta']
  }
end

#validate_defaults(df, prem) ⇒ Object



41
42
43
44
# File 'lib/premonition/resources.rb', line 41

def validate_defaults(df, prem)
  fail 'meta must be a hash' if !df['meta'].nil? && !df['meta'].is_a?(Hash)
  fail 'types must be a hash' if !prem['types'].nil? && !prem['types'].is_a?(Hash)
end

#validate_type(id, t) ⇒ Object



64
65
66
67
68
# File 'lib/premonition/resources.rb', line 64

def validate_type(id, t)
  fail 'id missing from type' if id.nil?
  fail "id can only be lowercase letters: #{id}" unless id[/[a-z]+/] == id
  fail 'meta must be an hash' if !t['meta'].nil? && !t['meta'].is_a?(Hash)
end