Class: CloudCannonJekyll::Config

Inherits:
Object
  • Object
show all
Defined in:
lib/cloudcannon-jekyll/config.rb

Overview

Processes Jekyll configuration to enable the plugin and fix common issues

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(site) ⇒ Config

Returns a new instance of Config.



28
29
30
# File 'lib/cloudcannon-jekyll/config.rb', line 28

def initialize(site)
  @site_config = site.config
end

Class Method Details

.rename_legacy_collection_config_keys(collection_config) ⇒ Object



22
23
24
25
26
# File 'lib/cloudcannon-jekyll/config.rb', line 22

def self.rename_legacy_collection_config_keys(collection_config)
  collection_config&.keys&.each do |k|
    collection_config[MAPPINGS[k]] = collection_config.delete(k) if MAPPINGS[k]
  end
end

Instance Method Details

#configObject



45
46
47
48
49
50
51
52
53
54
55
# File 'lib/cloudcannon-jekyll/config.rb', line 45

def config
  loaded = config_file('cloudcannon.config.json') ||
           config_file('cloudcannon.config.yaml') ||
           config_file('cloudcannon.config.yml')

  unless loaded
    Logger.info("⚙️ No config file found at #{'cloudcannon.config.(json|yaml|yml)'.bold}")
  end

  loaded
end

#config_defaults(config) ⇒ Object



65
66
67
68
69
70
71
72
73
# File 'lib/cloudcannon-jekyll/config.rb', line 65

def config_defaults(config)
  defaults = {
    'source' => @site_config['source'].gsub(Dir.pwd, ''),
    'timezone' => @site_config['timezone'],
    'base_url' => @site_config['baseurl']
  }

  defaults.merge(config)
end

#config_file(path) ⇒ Object



57
58
59
60
61
62
63
# File 'lib/cloudcannon-jekyll/config.rb', line 57

def config_file(path)
  loaded = YAML.safe_load(File.read(path), aliases: true) # Also loads JSON
  Logger.info "⚙️ Using config file at #{path.bold}"
  loaded
rescue Errno::ENOENT
  nil
end

#custom_configObject



36
37
38
39
40
41
42
43
# File 'lib/cloudcannon-jekyll/config.rb', line 36

def custom_config
  custom_path = ENV['CLOUDCANNON_CONFIG_PATH']
  return unless custom_path

  loaded = config_file(custom_path)
  Logger.info("⚙️ No config file found at #{custom_path.bold}") unless loaded
  loaded
end

#legacy_collection_groupsObject



113
114
115
# File 'lib/cloudcannon-jekyll/config.rb', line 113

def legacy_collection_groups
  @site_config.dig('_explore', 'groups')
end

#legacy_collections_configObject



101
102
103
104
105
106
107
108
109
110
111
# File 'lib/cloudcannon-jekyll/config.rb', line 101

def legacy_collections_config
  collections_config = @site_config.dig('cloudcannon', 'collections')

  return unless collections_config

  collections_config.each do |_, collection_config|
    Config.rename_legacy_collection_config_keys(collection_config)
  end

  collections_config
end

#legacy_configObject



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
# File 'lib/cloudcannon-jekyll/config.rb', line 75

def legacy_config
  Logger.info('⚙️ Falling back to site config'.yellow)

  {
    'data_config' => @site_config.dig('cloudcannon', 'data'),
    'collections_config' => legacy_collections_config,
    'collection_groups' => @site_config['_collection_groups'] || legacy_collection_groups,
    'editor' => @site_config['_editor'],
    'source_editor' => @site_config['_source_editor'],
    'paths' => {
      'uploads' => @site_config['uploads_dir']
    },
    '_select_data' => @site_config['_select_data'] || legacy_select_data,
    '_inputs' => @site_config['_inputs'],
    '_editables' => @site_config['_editables'],
    '_structures' => @site_config['_structures'],
    '_enabled_editors' => @site_config['_enabled_editors'],

    # Deprecated keys
    '_array_structures' => @site_config['_array_structures'],
    '_comments' => @site_config['_comments'],
    '_instance_values' => @site_config['_instance_values'],
    '_options' => @site_config['_options']
  }
end

#legacy_select_dataObject



117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# File 'lib/cloudcannon-jekyll/config.rb', line 117

def legacy_select_data
  cloudcannon_keys = %w[_comments _options _editor _explore cloudcannon _collection_groups
                        _enabled_editors _instance_values _source_editor _array_structures
                        uploads_dir _editables _inputs _structures editor source_editor
                        collection_groups]

  jekyll_keys = %w[source destination collections_dir cache_dir plugins_dir layouts_dir
                   data_dir includes_dir collections safe include exclude keep_files encoding
                   markdown_ext strict_front_matter show_drafts limit_posts future unpublished
                   whitelist plugins markdown highlighter lsi excerpt_separator incremental
                   detach port host baseurl show_dir_listing permalink paginate_path timezone
                   quiet verbose defaults liquid kramdown title url description maruku
                   redcloth rdiscount redcarpet gems plugins]

  keys = cloudcannon_keys + jekyll_keys

  select_data = @site_config.keys.each_with_object({}) do |key, memo|
    value = @site_config[key]

    next unless value.is_a?(Array) || value.is_a?(Hash)
    next if key.nil? || keys.include?(key)

    memo[key] = value
  end

  compacted = select_data.compact
  compacted.empty? ? nil : compacted
end

#readObject



32
33
34
# File 'lib/cloudcannon-jekyll/config.rb', line 32

def read
  config_defaults(custom_config || config || legacy_config)
end