Class: Jekyll::Premonition::Resources
- Inherits:
-
Object
- Object
- Jekyll::Premonition::Resources
- Defined in:
- lib/premonition/resources.rb
Overview
Class for loading the Premonition configuration and preparing it for use.
Instance Attribute Summary collapse
-
#config ⇒ Object
readonly
Returns the value of attribute config.
-
#markdown ⇒ Object
readonly
Returns the value of attribute markdown.
Instance Method Summary collapse
- #citation_template ⇒ Object
-
#default_config ⇒ Object
Setup the default configuration and types.
- #default_template ⇒ Object
- #fail(msg) ⇒ Object
-
#initialize(site_config) ⇒ Resources
constructor
A new instance of Resources.
-
#load(site_config) ⇒ Object
Load the configuration of Premonition from Jekyll site configuration object.
-
#load_extensions(p, cfg) ⇒ Object
Load extra extensions from config.
-
#load_types(p, cfg) ⇒ Object
Load extra Premonition types configured/overriden in _config.yml.
-
#type_config(id, t) ⇒ Object
Validate a configured type and return as type hash.
-
#validate_defaults(df, prem) ⇒ Object
Basic configuration validation.
-
#validate_type(id, t) ⇒ Object
Type validation.
Constructor Details
#initialize(site_config) ⇒ Resources
Returns a new instance of Resources.
10 11 12 13 14 |
# File 'lib/premonition/resources.rb', line 10 def initialize(site_config) @config = load site_config # Setup a new Markdown renderer. @markdown = Converters::Markdown.new site_config end |
Instance Attribute Details
#config ⇒ Object (readonly)
Returns the value of attribute config.
7 8 9 |
# File 'lib/premonition/resources.rb', line 7 def config @config end |
#markdown ⇒ Object (readonly)
Returns the value of attribute markdown.
8 9 10 |
# File 'lib/premonition/resources.rb', line 8 def markdown @markdown end |
Instance Method Details
#citation_template ⇒ Object
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/premonition/resources.rb', line 41 def citation_template <<~TEMPLATE <div class="premonition {% if meta.style %}{{meta.style}} {% endif %}{{type}}"> <i class="{% if meta.fa-icon %}fas {{meta.fa-icon}}{% else %}premonition {{meta.pn-icon}}{% endif %}"></i> <blockquote class="content blockquote"{% if attrs.cite %} cite="{{attrs.cite}}"{% endif %}> {{content}} {% if header %} <footer class="blockquote-footer"> <cite title="{{title}}">{{title}}</cite> </footer> {% endif %} </blockquote> </div> TEMPLATE end |
#default_config ⇒ Object
Setup the default configuration and types
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/premonition/resources.rb', line 58 def default_config { 'default' => { 'template' => default_template, 'meta' => { 'pn-icon' => 'pn-square', 'fa-icon' => nil }, 'title' => nil }, 'types' => { 'note' => { 'meta' => { 'pn-icon' => 'pn-note' } }, 'info' => { 'meta' => { 'pn-icon' => 'pn-info' } }, 'warning' => { 'meta' => { 'pn-icon' => 'pn-warn' } }, 'error' => { 'meta' => { 'pn-icon' => 'pn-error' } }, 'citation' => { 'meta' => { 'pn-icon' => 'pn-quote' }, 'template' => citation_template } }, 'extensions' => %w[ md markdown ] } end |
#default_template ⇒ Object
30 31 32 33 34 35 36 37 38 39 |
# File 'lib/premonition/resources.rb', line 30 def default_template <<~TEMPLATE <div class="premonition {% if meta.style %}{{meta.style}} {% endif %}{{type}}"> <i class="{% if meta.fa-icon %}fas {{meta.fa-icon}}{% else %}premonition {{meta.pn-icon}}{% endif %}"></i> <div class="content"> {% if header %}<p class="header">{{title}}</p>{% endif %}{{content}} </div> </div> TEMPLATE end |
#fail(msg) ⇒ Object
127 128 129 130 |
# File 'lib/premonition/resources.rb', line 127 def fail(msg) Jekyll.logger.error 'Fatal (Premonition):', msg raise LoadError, msg end |
#load(site_config) ⇒ Object
Load the configuration of Premonition from Jekyll site configuration object.
17 18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/premonition/resources.rb', line 17 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 load_extensions p, cfg cfg end |
#load_extensions(p, cfg) ⇒ Object
Load extra extensions from config.
We need this when looking for excerpts
99 100 101 102 103 104 105 106 107 108 |
# File 'lib/premonition/resources.rb', line 99 def load_extensions(p, cfg) return if p['extensions'].nil? return unless p['extensions'].is_a?(Array) return if p['extensions'].empty? cfg['extensions'] = [] p['extensions'].each do |v| cfg['extensions'] << v unless cfg['extensions'].include?(v) end end |
#load_types(p, cfg) ⇒ Object
Load extra Premonition types configured/overriden in _config.yml
86 87 88 89 90 91 92 93 94 |
# File 'lib/premonition/resources.rb', line 86 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
Validate a configured type and return as type hash
111 112 113 114 115 116 117 118 |
# File 'lib/premonition/resources.rb', line 111 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
Basic configuration validation
80 81 82 83 |
# File 'lib/premonition/resources.rb', line 80 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
Type validation
121 122 123 124 125 |
# File 'lib/premonition/resources.rb', line 121 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 |