module SuttyMigration::Jekyll::DocumentCreator

Public Class Methods

create(site:, date:, title:, collection:, slug: nil) click to toggle source

Creates a new document in a collection or fails if it already exists.

@param :site [Jekyll::Site] Jekyll site @param :date [Time] Post date @param :title [String] Post title @param :slug [String] Post slug, slugified title if empty @param :collection [Jekyll::Collection,String] Collection label or collection @return [Jekyll::Document] A new document

   # File lib/sutty_migration/jekyll/document_creator.rb
23 def create(site:, date:, title:, collection:, slug: nil)
24   collection = site.collections[collection] if collection.is_a? String
25   slug = ::Jekyll::Utils.slugify(title, mode: 'latin') if slug.blank?
26   basename = "#{date.strftime('%F')}-#{slug}.markdown"
27   path = File.join(collection.directory, basename)
28 
29   raise DocumentExists, "#{path} already exists" if File.exist? path
30 
31   ::Jekyll::Document.new(path, site: site, collection: collection).tap do |document|
32     collection.docs << document
33     document.data['title'] = title
34   end
35 end
find(site:, relative_path:) click to toggle source

Finds a document by its relative path

@param :site [Jekyll::Site] @param :relative_path [String] @return [Jekyll::Document,Nil]

   # File lib/sutty_migration/jekyll/document_creator.rb
63 def find(site:, relative_path:)
64   indexed_documents_by_relative_path(site)[relative_path]
65 end
find_or_create(site:, date:, title:, collection:, slug: nil) click to toggle source

Finds a document by its relative path or creates it if it doesn't exist. Helpful for idempotent migrations (create or update actions)

@param :site [Jekyll::Site] Jekyll site @param :date [Time] Post date @param :title [String] Post title @param :slug [String] Post slug, slugified title if empty @param :collection [Jekyll::Collection,String] Collection label or collection @return [Jekyll::Document] The found document or a new one

   # File lib/sutty_migration/jekyll/document_creator.rb
47 def find_or_create(site:, date:, title:, collection:, slug: nil)
48   collection = site.collections[collection] if collection.is_a? String
49   slug = ::Jekyll::Utils.slugify(title, mode: 'latin') if slug.blank?
50   basename = "#{date.strftime('%F')}-#{slug}.markdown"
51   path = File.join(collection.relative_directory, basename)
52 
53   return find(site: site, relative_path: path) if File.exist?(path)
54 
55   create(site: site, date: date, title: title, slug: slug, collection: collection)
56 end
included(base) click to toggle source
   # File lib/sutty_migration/jekyll/document_creator.rb
11 def self.included(base)
12   base.class_eval do
13     class << self
14       # Creates a new document in a collection or fails if it already
15       # exists.
16       #
17       # @param :site [Jekyll::Site] Jekyll site
18       # @param :date [Time] Post date
19       # @param :title [String] Post title
20       # @param :slug [String] Post slug, slugified title if empty
21       # @param :collection [Jekyll::Collection,String] Collection label or collection
22       # @return [Jekyll::Document] A new document
23       def create(site:, date:, title:, collection:, slug: nil)
24         collection = site.collections[collection] if collection.is_a? String
25         slug = ::Jekyll::Utils.slugify(title, mode: 'latin') if slug.blank?
26         basename = "#{date.strftime('%F')}-#{slug}.markdown"
27         path = File.join(collection.directory, basename)
28 
29         raise DocumentExists, "#{path} already exists" if File.exist? path
30 
31         ::Jekyll::Document.new(path, site: site, collection: collection).tap do |document|
32           collection.docs << document
33           document.data['title'] = title
34         end
35       end
36 
37       # Finds a document by its relative path or creates it if it
38       # doesn't exist.  Helpful for idempotent migrations (create or
39       # update actions)
40       #
41       # @param :site [Jekyll::Site] Jekyll site
42       # @param :date [Time] Post date
43       # @param :title [String] Post title
44       # @param :slug [String] Post slug, slugified title if empty
45       # @param :collection [Jekyll::Collection,String] Collection label or collection
46       # @return [Jekyll::Document] The found document or a new one
47       def find_or_create(site:, date:, title:, collection:, slug: nil)
48         collection = site.collections[collection] if collection.is_a? String
49         slug = ::Jekyll::Utils.slugify(title, mode: 'latin') if slug.blank?
50         basename = "#{date.strftime('%F')}-#{slug}.markdown"
51         path = File.join(collection.relative_directory, basename)
52 
53         return find(site: site, relative_path: path) if File.exist?(path)
54 
55         create(site: site, date: date, title: title, slug: slug, collection: collection)
56       end
57 
58       # Finds a document by its relative path
59       #
60       # @param :site [Jekyll::Site]
61       # @param :relative_path [String]
62       # @return [Jekyll::Document,Nil]
63       def find(site:, relative_path:)
64         indexed_documents_by_relative_path(site)[relative_path]
65       end
66 
67       # Index documents by relative path for faster finding
68       #
69       # @param [Jekyll::Site]
70       # @return [Hash]
71       def indexed_documents_by_relative_path(site)
72         @indexed_documents_by_relative_path ||= site.documents.reduce({}) do |idx, doc|
73           idx.tap do |i|
74             i[doc.relative_path] = doc
75           end
76         end
77       end
78     end
79   end
80 end
indexed_documents_by_relative_path(site) click to toggle source

Index documents by relative path for faster finding

@param [Jekyll::Site] @return [Hash]

   # File lib/sutty_migration/jekyll/document_creator.rb
71 def indexed_documents_by_relative_path(site)
72   @indexed_documents_by_relative_path ||= site.documents.reduce({}) do |idx, doc|
73     idx.tap do |i|
74       i[doc.relative_path] = doc
75     end
76   end
77 end