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

Instance Method Summary collapse

Instance Method Details

#add_blogging_config(collections_config) ⇒ Object

Add posts/drafts to collections config



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/cloudcannon-jekyll/generator.rb', line 91

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

  if collections_config.key?("posts")
    collections_config["drafts"] = collections_config["posts"].dup
  elsif drafts.any?
    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



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/cloudcannon-jekyll/generator.rb', line 52

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

  posts.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



110
111
112
113
114
115
116
# File 'lib/cloudcannon-jekyll/generator.rb', line 110

def add_collection_paths(collections_config)
  collections_config.each do |key, collection|
    next if collection.key?("_path")

    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



85
86
87
88
# File 'lib/cloudcannon-jekyll/generator.rb', line 85

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

#collections_dirObject



42
43
44
45
46
# File 'lib/cloudcannon-jekyll/generator.rb', line 42

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

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

#data_dirObject



48
49
50
# File 'lib/cloudcannon-jekyll/generator.rb', line 48

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

#destination_path(filename) ⇒ Object



141
142
143
# File 'lib/cloudcannon-jekyll/generator.rb', line 141

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

#file_content(filename, data) ⇒ Object



145
146
147
148
149
150
151
152
153
# File 'lib/cloudcannon-jekyll/generator.rb', line 145

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



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

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

  collections_config = @site.config["collections"].dup || {}

  # Workaround for empty collection configurations
  collections_config.each_key do |key|
    collections_config[key] ||= { "output" => false }
  end

  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)

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

  generate_file("details", payload.merge({
    "drafts" => drafts,
  }))
end

#generate_file(filename, data) ⇒ Object



118
119
120
121
122
123
124
# File 'lib/cloudcannon-jekyll/generator.rb', line 118

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



133
134
135
# File 'lib/cloudcannon-jekyll/generator.rb', line 133

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

#source_path(filename) ⇒ Object



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

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

#version_path_suffixObject



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

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

  ""
end