class CloudCannonJekyll::Generator

Generates JSON files containing build config and build output details

Public Class Methods

<=>(*) click to toggle source
# File lib/cloudcannon-jekyll/generator.rb, line 12
def self.<=>(*)
  1
end

Public Instance Methods

add_blogging_config(collections_config) click to toggle source

Add posts/drafts to collections config

# File lib/cloudcannon-jekyll/generator.rb, line 127
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 = {}) click to toggle source

rubocop:disable Metrics/AbcSize

# File lib/cloudcannon-jekyll/generator.rb, line 79
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) click to toggle source

Add path to each collection config

# File lib/cloudcannon-jekyll/generator.rb, line 144
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) click to toggle source

Add data to collections config if raw data files exist

# File lib/cloudcannon-jekyll/generator.rb, line 121
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_groups() click to toggle source

Support for the deprecated _explore configuration

# File lib/cloudcannon-jekyll/generator.rb, line 114
def add_legacy_explore_groups
  unless @site.config.key?("_collection_groups")
    @site.config["_collection_groups"] = @site.config.dig("_explore", "groups")&.dup
  end
end
collections_dir() click to toggle source
# File lib/cloudcannon-jekyll/generator.rb, line 54
def collections_dir
  return "" if Jekyll::VERSION.start_with? "2."

  @site.config["collections_dir"] || ""
end
data_dir() click to toggle source
# File lib/cloudcannon-jekyll/generator.rb, line 74
def data_dir
  @site.config["data_dir"] || "_data"
end
destination_path(filename) click to toggle source
# File lib/cloudcannon-jekyll/generator.rb, line 173
def destination_path(filename)
  Jekyll.sanitized_path(@site.dest, path(filename))
end
file_content(filename, data) click to toggle source
# File lib/cloudcannon-jekyll/generator.rb, line 177
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) click to toggle source
# File lib/cloudcannon-jekyll/generator.rb, line 16
def generate(site)
  @site = site
  @reader = Reader.new(@site)

  collections_config = process_collections_config
  data = process_data

  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,
    "data"               => data,
  }))
end
generate_file(filename, data) click to toggle source
# File lib/cloudcannon-jekyll/generator.rb, line 150
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 = "") click to toggle source
# File lib/cloudcannon-jekyll/generator.rb, line 165
def path(filename, suffix = "")
  "_cloudcannon/#{filename}#{suffix}.json"
end
process_collections_config() click to toggle source
# File lib/cloudcannon-jekyll/generator.rb, line 41
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
process_data() click to toggle source
# File lib/cloudcannon-jekyll/generator.rb, line 60
def process_data
  cc_data = @site.config.dig("cloudcannon", "data")
  data = if cc_data == true
           @site.data&.dup
         elsif cc_data&.is_a?(Hash)
           @site.data&.select { |key, _| cc_data.key?(key) }
         end

  data ||= {}
  data["categories"] ||= @site.categories.keys
  data["tags"] ||= @site.tags.keys
  data
end
source_path(filename) click to toggle source
# File lib/cloudcannon-jekyll/generator.rb, line 169
def source_path(filename)
  File.expand_path(path(filename, version_path_suffix), File.dirname(__FILE__))
end
version_path_suffix() click to toggle source
# File lib/cloudcannon-jekyll/generator.rb, line 158
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