Class: CloudCannonJekyll::Generator

Inherits:
Jekyll::Generator
  • Object
show all
Defined in:
lib/cloudcannon-jekyll/generator.rb

Overview

Generates JSON files containing build config and build output details

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.<=>Object



12
13
14
# File 'lib/cloudcannon-jekyll/generator.rb', line 12

def self.<=>(*)
  1
end

Instance Method Details

#add_blogging_config(collections_config) ⇒ Object

Add posts/drafts to collections config



111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/cloudcannon-jekyll/generator.rb', line 111

def add_blogging_config(collections_config)
  collections_config["posts"] = { "output" => true } if Jekyll::VERSION.start_with? "2."
  drafts = @reader.read_drafts(collections_dir)

  if drafts.any? || (collections_config.key?("posts") && !collections_config.key?("drafts"))
    collections_config["drafts"] = {}
  end

  folders = add_category_folder_config(collections_config, collections_config["posts"])
  folders.compact.each do |folder|
    drafts += @reader.read_drafts(folder)
  end

  drafts
end

#add_category_folder_config(collections_config, posts_config = {}) ⇒ Object

rubocop:disable Metrics/AbcSize



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

def add_category_folder_config(collections_config, posts_config = {})
  posts = @site.posts || @site.collections["posts"]
  docs = posts.class.method_defined?(:docs) ? posts.docs : posts
  seen = {}

  docs.map do |post|
    parts = post.relative_path.split("/_posts/")
    path = parts.first

    # Ignore unless it's an unseen category folder post
    next if parts.length < 2 || path.empty? || seen[path]

    # Could check this to ensure raw files exist since posts can be generated without files
    # next if @reader.read_posts(parts[0]).empty?

    seen[path] = true
    folder = path.sub(%r!^\/+!, "")
    collections_path = "#{collections_dir}/#{folder}".gsub(%r!\/+!, "/").sub(%r!^\/+!, "")

    collections_config["#{folder}/posts"] = posts_config.merge({
      "path" => "#{collections_path}/_posts",
    })

    # Adding the category draft config like this isn't ideal, since you could have drafts
    #  without posts, but it's a decent trade off vs looking for _drafts folders
    collections_config["#{folder}/drafts"] = posts_config.merge({
      "path" => "#{collections_path}/_drafts",
    })

    path
  end
end

#add_collection_paths(collections_config) ⇒ Object

Add path to each collection config



128
129
130
131
132
# File 'lib/cloudcannon-jekyll/generator.rb', line 128

def add_collection_paths(collections_config)
  collections_config.each do |key, collection|
    collection["path"] ||= File.join(collections_dir, "_#{key}").sub(%r!^\/+!, "")
  end
end

#add_data_config(collections_config) ⇒ Object

Add data to collections config if raw data files exist



105
106
107
108
# File 'lib/cloudcannon-jekyll/generator.rb', line 105

def add_data_config(collections_config)
  data_files = @reader.read_data(data_dir)
  collections_config["data"] = { "path" => data_dir } if data_files&.keys&.any?
end

#add_legacy_explore_groupsObject

Support for the deprecated _explore configuration



98
99
100
101
102
# File 'lib/cloudcannon-jekyll/generator.rb', line 98

def add_legacy_explore_groups
  unless @site.config.key?("_collection_groups")
    @site.config["_collection_groups"] = @site.config.dig("_explore", "groups")&.dup
  end
end

#collections_dirObject



52
53
54
55
56
# File 'lib/cloudcannon-jekyll/generator.rb', line 52

def collections_dir
  return "" if Jekyll::VERSION.start_with? "2."

  @site.config["collections_dir"] || ""
end

#data_dirObject



58
59
60
# File 'lib/cloudcannon-jekyll/generator.rb', line 58

def data_dir
  @site.config["data_dir"] || "_data"
end

#destination_path(filename) ⇒ Object



157
158
159
# File 'lib/cloudcannon-jekyll/generator.rb', line 157

def destination_path(filename)
  Jekyll.sanitized_path(@site.dest, path(filename))
end

#file_content(filename, data) ⇒ Object



161
162
163
164
165
166
167
168
169
# File 'lib/cloudcannon-jekyll/generator.rb', line 161

def file_content(filename, data)
  page = PageWithoutAFile.new(@site, File.dirname(__FILE__), "", path(filename))
  page.content = File.read(source_path(filename))
  page.data["layout"] = nil
  page.data["sitemap"] = false
  page.data["permalink"] = "/#{path(filename)}"
  page.render({}, data)
  page.output
end

#generate(site) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/cloudcannon-jekyll/generator.rb', line 16

def generate(site)
  @site = site
  @reader = Reader.new(@site)

  collections_config = process_collections_config

  payload = @site.site_payload.merge({
    "gem_version" => CloudCannonJekyll::VERSION,
  })

  drafts = add_blogging_config(collections_config)
  add_collection_paths(collections_config)
  add_data_config(collections_config)
  add_legacy_explore_groups

  generate_file("info", payload.merge({
    "pwd"                => Dir.pwd,
    "config"             => @site.config,
    "collections_config" => collections_config,
    "drafts"             => drafts,
  }))
end

#generate_file(filename, data) ⇒ Object



134
135
136
137
138
139
140
# File 'lib/cloudcannon-jekyll/generator.rb', line 134

def generate_file(filename, data)
  dest = destination_path(filename)
  FileUtils.mkdir_p(File.dirname(dest))
  File.open(dest, "w") { |file| file.write(file_content(filename, data)) }
  @site.keep_files ||= []
  @site.keep_files << path(filename)
end

#path(filename, suffix = "") ⇒ Object



149
150
151
# File 'lib/cloudcannon-jekyll/generator.rb', line 149

def path(filename, suffix = "")
  "_cloudcannon/#{filename}#{suffix}.json"
end

#process_collections_configObject



39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/cloudcannon-jekyll/generator.rb', line 39

def process_collections_config
  collections = @site.config["collections"]&.dup || {}
  cc_collections = @site.config.dig("cloudcannon", "collections")&.dup || {}

  collections.each_key do |key|
    # Workaround for empty collection configurations
    defaults = collections[key] || { "output" => false }
    cc_collections[key] = (cc_collections[key] || {}).merge(defaults)
  end

  cc_collections
end

#source_path(filename) ⇒ Object



153
154
155
# File 'lib/cloudcannon-jekyll/generator.rb', line 153

def source_path(filename)
  File.expand_path(path(filename, version_path_suffix), File.dirname(__FILE__))
end

#version_path_suffixObject



142
143
144
145
146
147
# File 'lib/cloudcannon-jekyll/generator.rb', line 142

def version_path_suffix
  return "-2.x" if Jekyll::VERSION.start_with? "2."
  return "-3.0-4.x" if Jekyll::VERSION.match? %r!3\.[0-4]\.!

  ""
end