Class: CloudCannonJekyll::Collections
- Inherits:
-
Object
- Object
- CloudCannonJekyll::Collections
- Defined in:
- lib/cloudcannon-jekyll/generators/collections.rb
Overview
Helper functions for generating collection configuration and summaries
Instance Method Summary collapse
- #all_drafts ⇒ Object
- #all_pages ⇒ Object
- #all_posts ⇒ Object
- #allowed_document?(doc) ⇒ Boolean
- #allowed_page?(page) ⇒ Boolean
- #allowed_static_file?(static_file) ⇒ Boolean
- #collections_config_path_map(collections_config) ⇒ Object
- #data_files? ⇒ Boolean
- #document_collection_key(doc, path_map) ⇒ Object
- #document_data(doc) ⇒ Object
- #document_path(doc) ⇒ Object
- #document_to_json(doc, collection) ⇒ Object
- #document_type(doc) ⇒ Object
- #document_url(doc) ⇒ Object
- #drafts_paths ⇒ Object
- #each_document(&block) ⇒ Object
- #generate_collections(collections_config) ⇒ Object
- #generate_collections_config ⇒ Object
- #generate_collections_config_path(key) ⇒ Object
- #group_by_category_folder(collection, key) ⇒ Object
-
#initialize(site, config) ⇒ Collections
constructor
A new instance of Collections.
- #legacy_doc?(doc) ⇒ Boolean
- #legacy_document_data(doc) ⇒ Object
- #remove_empty_collection_config(collections_config, collections) ⇒ Object
Constructor Details
#initialize(site, config) ⇒ Collections
Returns a new instance of Collections.
15 16 17 18 19 20 21 22 23 |
# File 'lib/cloudcannon-jekyll/generators/collections.rb', line 15 def initialize(site, config) @site = site @config = config @reader = Reader.new(site) @collections_dir = Paths.collections_dir(site) @data_dir = Paths.data_dir(site) @split_posts = group_by_category_folder(all_posts, 'posts') @split_drafts = group_by_category_folder(all_drafts, 'drafts') end |
Instance Method Details
#all_drafts ⇒ Object
286 287 288 289 290 291 |
# File 'lib/cloudcannon-jekyll/generators/collections.rb', line 286 def all_drafts drafts_paths.reduce([]) do |drafts, drafts_path| base_path = drafts_path.gsub(%r{(^|/)_drafts}, '') drafts + @reader.read_drafts(base_path) end end |
#all_pages ⇒ Object
293 294 295 296 297 |
# File 'lib/cloudcannon-jekyll/generators/collections.rb', line 293 def all_pages pages = @site.pages.select { |doc| allowed_page?(doc) } static_pages = @site.static_files.select { |doc| allowed_static_file?(doc) } pages + static_pages end |
#all_posts ⇒ Object
281 282 283 284 |
# File 'lib/cloudcannon-jekyll/generators/collections.rb', line 281 def all_posts posts = @site.posts || @site.collections['posts'] posts.class.method_defined?(:docs) ? posts.docs : posts end |
#allowed_document?(doc) ⇒ Boolean
299 300 301 302 303 304 305 306 307 308 309 |
# File 'lib/cloudcannon-jekyll/generators/collections.rb', line 299 def allowed_document?(doc) if Jekyll.const_defined?(:PageWithoutAFile) && doc.instance_of?(Jekyll::PageWithoutAFile) false elsif doc.instance_of?(Jekyll::Page) allowed_page?(doc) elsif doc.instance_of?(Jekyll::StaticFile) allowed_static_file?(doc) else true end end |
#allowed_page?(page) ⇒ Boolean
311 312 313 |
# File 'lib/cloudcannon-jekyll/generators/collections.rb', line 311 def allowed_page?(page) page.html? || page.url.end_with?('/') end |
#allowed_static_file?(static_file) ⇒ Boolean
315 316 317 |
# File 'lib/cloudcannon-jekyll/generators/collections.rb', line 315 def allowed_static_file?(static_file) STATIC_EXTENSIONS.include?(static_file.extname) end |
#collections_config_path_map(collections_config) ⇒ Object
203 204 205 206 207 208 209 210 211 212 |
# File 'lib/cloudcannon-jekyll/generators/collections.rb', line 203 def collections_config_path_map(collections_config) unsorted = collections_config.map do |key, collection_config| { key: key, path: "/#{collection_config['path']}/".sub(%r{/+}, '/') } end unsorted.sort_by { |pair| pair[:path].length }.reverse end |
#data_files? ⇒ Boolean
319 320 321 |
# File 'lib/cloudcannon-jekyll/generators/collections.rb', line 319 def data_files? @reader.read_data(@data_dir)&.keys&.any? end |
#document_collection_key(doc, path_map) ⇒ Object
214 215 216 217 218 219 220 221 222 |
# File 'lib/cloudcannon-jekyll/generators/collections.rb', line 214 def document_collection_key(doc, path_map) path = "/#{File.join(@collections_dir, doc.relative_path)}/".sub(%r{/+}, '/') collection_path_pair = path_map.find do |pair| path.start_with? pair[:path] end collection_path_pair[:key] if collection_path_pair end |
#document_data(doc) ⇒ Object
240 241 242 243 244 245 246 247 248 249 250 251 252 |
# File 'lib/cloudcannon-jekyll/generators/collections.rb', line 240 def document_data(doc) data = if legacy_doc?(doc) legacy_document_data(doc) elsif doc.respond_to?(:data) doc.data.dup else {} end data.delete('excerpt') defaults = @site.frontmatter_defaults.all(doc.relative_path, document_type(doc)) defaults.merge(data) end |
#document_path(doc) ⇒ Object
258 259 260 261 262 263 264 265 266 |
# File 'lib/cloudcannon-jekyll/generators/collections.rb', line 258 def document_path(doc) path = if doc.respond_to?(:collection) && doc.collection File.join(@collections_dir, doc.relative_path) else doc.relative_path end path.sub(%r{^/+}, '') end |
#document_to_json(doc, collection) ⇒ Object
268 269 270 271 272 273 274 275 276 277 278 279 |
# File 'lib/cloudcannon-jekyll/generators/collections.rb', line 268 def document_to_json(doc, collection) base = document_data(doc).merge( { 'path' => document_path(doc), 'url' => document_url(doc), 'collection' => collection } ) base['id'] = doc.id if doc.respond_to? :id base end |
#document_type(doc) ⇒ Object
191 192 193 194 195 196 197 198 199 200 201 |
# File 'lib/cloudcannon-jekyll/generators/collections.rb', line 191 def document_type(doc) if IS_JEKYLL_2_X_X && (doc.instance_of?(Jekyll::Post) || doc.instance_of?(Jekyll::Draft)) :posts elsif doc.respond_to?(:type) doc.type elsif doc.respond_to?(:collection) doc.collection.label.to_sym elsif doc.instance_of?(Jekyll::Page) :pages end end |
#document_url(doc) ⇒ Object
254 255 256 |
# File 'lib/cloudcannon-jekyll/generators/collections.rb', line 254 def document_url(doc) doc.respond_to?(:url) ? doc.url : doc.relative_path end |
#drafts_paths ⇒ Object
112 113 114 115 116 117 118 |
# File 'lib/cloudcannon-jekyll/generators/collections.rb', line 112 def drafts_paths paths = @split_posts.keys.map do |key| File.join('/', @collections_dir, key.sub(/posts$/, '_drafts')) end paths.empty? ? [File.join('/', @collections_dir, '_drafts')] : paths end |
#each_document(&block) ⇒ Object
120 121 122 123 124 125 126 127 128 |
# File 'lib/cloudcannon-jekyll/generators/collections.rb', line 120 def each_document(&block) @site.pages.each(&block) @site.static_files.each(&block) @site.collections.each_value { |coll| coll.docs.each(&block) } all_drafts.each(&block) # Jekyll 2.x.x doesn't have posts in site.collections all_posts.each(&block) if IS_JEKYLL_2_X_X end |
#generate_collections(collections_config) ⇒ Object
130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 |
# File 'lib/cloudcannon-jekyll/generators/collections.rb', line 130 def generate_collections(collections_config) assigned_pages = {} collections = {} path_map = collections_config_path_map(collections_config) each_document do |doc| next unless allowed_document?(doc) key = document_collection_key(doc, path_map) unless key Logger.warn "⚠️ No collection for #{doc.relative_path.bold}" next end if collections_config.dig(key, 'parser') == false Logger.warn "⚠️ Ignoring #{doc.relative_path.bold} in #{key.bold} collection" next end collections[key] ||= [] collections[key].push(document_to_json(doc, key)) assigned_pages[doc.relative_path] = true if doc.instance_of?(Jekyll::Page) end collections_config.each_key do |key| next if key == 'data' collections[key] ||= [] end if collections.key?('pages') && collections['pages'].empty? all_pages.each do |page| assigned = assigned_pages[page.relative_path] collections['pages'].push(document_to_json(page, 'pages')) unless assigned end end collections end |
#generate_collections_config ⇒ Object
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 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 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 |
# File 'lib/cloudcannon-jekyll/generators/collections.rb', line 35 def generate_collections_config collections = @site.config['collections'] || {} collections_config = @config['collections_config'] || {} return collections_config if @config['collections_config_override'] if collections.is_a?(Array) collections = collections.each_with_object({}) { |key, memo| memo[key] = {} } end defaults = { 'data' => { 'path' => @data_dir, 'output' => false }, 'posts' => { 'output' => true }, 'drafts' => { 'output' => !!@site.show_drafts } } unless collections.key?('pages') && !collections['pages'].empty? defaults['pages'] = { 'path' => '', 'output' => true, 'filter' => 'strict' } end collection_keys = (collections_config.keys + defaults.keys + collections.keys).uniq collection_keys.each do |key| processed = (defaults[key] || {}) .merge(collections[key] || {}) .merge(collections_config[key] || {}) processed['output'] ||= false processed['auto_discovered'] = !collections_config.key?(key) processed['path'] ||= generate_collections_config_path(key) processed['path'] = processed['path'].sub(%r{^/+}, '') Config.rename_legacy_collection_config_keys(processed) collections_config[key] = processed end @split_posts.each_key do |key| posts_path = @split_posts[key]&.first&.relative_path&.sub(%r{(^|/)_posts.*}, '\1_posts') next unless posts_path defaults = { 'auto_discovered' => !collections_config.key?(key), 'path' => File.join(@collections_dir, posts_path).sub(%r{^/+}, ''), 'output' => true } collections_config[key] = defaults.merge(collections_config[key] || {}) end @split_drafts.each_key do |key| drafts_path = @split_drafts[key]&.first&.relative_path&.sub(%r{(^|/)_drafts.*}, '\1_drafts') next unless drafts_path defaults = { 'auto_discovered' => !collections_config.key?(key), 'path' => File.join(@collections_dir, drafts_path).sub(%r{^/+}, ''), 'output' => !!@site.show_drafts } collections_config[key] = defaults.merge(collections_config[key] || {}) end collections_config end |
#generate_collections_config_path(key) ⇒ Object
25 26 27 28 29 30 31 32 33 |
# File 'lib/cloudcannon-jekyll/generators/collections.rb', line 25 def generate_collections_config_path(key) if key.end_with?("/posts") File.join(@collections_dir, key.sub(/\/posts$/, "/_posts")) elsif key.end_with?("/drafts") File.join(@collections_dir, key.sub(/\/drafts$/, "/_drafts")) else File.join(@collections_dir, "_#{key}") end end |
#group_by_category_folder(collection, key) ⇒ Object
323 324 325 326 327 328 329 330 331 332 333 |
# File 'lib/cloudcannon-jekyll/generators/collections.rb', line 323 def group_by_category_folder(collection, key) split_path = "/_#{key}/" collection.group_by do |doc| parts = doc.relative_path.split(split_path) if parts.length > 1 File.join(parts.first, key).sub(%r{^/+}, '') else key end end end |
#legacy_doc?(doc) ⇒ Boolean
235 236 237 238 |
# File 'lib/cloudcannon-jekyll/generators/collections.rb', line 235 def legacy_doc?(doc) (IS_JEKYLL_3_04_X && doc.instance_of?(Jekyll::Document) && doc.collection.label == 'posts') || (IS_JEKYLL_2_X_X && (doc.instance_of?(Jekyll::Draft) || doc.instance_of?(Jekyll::Post))) end |
#legacy_document_data(doc) ⇒ Object
224 225 226 227 228 229 230 231 232 233 |
# File 'lib/cloudcannon-jekyll/generators/collections.rb', line 224 def legacy_document_data(doc) legacy_data = {} legacy_data['categories'] = doc.categories if doc.respond_to?(:categories) legacy_data['tags'] = doc. if doc.respond_to?(:tags) legacy_data['date'] = doc.date if doc.respond_to?(:date) data = doc.data.merge(legacy_data) data['slug'] = doc.slug if doc.respond_to?(:slug) data end |
#remove_empty_collection_config(collections_config, collections) ⇒ Object
173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 |
# File 'lib/cloudcannon-jekyll/generators/collections.rb', line 173 def remove_empty_collection_config(collections_config, collections) collections_config.each do |key, collection_config| should_delete = if key == 'data' !data_files? else collections[key]&.empty? && collection_config['auto_discovered'] end if should_delete Logger.info "📂 #{'Ignored'.yellow} #{key.bold} collection" collections_config.delete(key) else count = collections[key]&.length || 0 Logger.info "📁 Processed #{key.bold} collection with #{count} files" end end end |