jekyll-admin 0.8.1 → 0.9.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,103 +1,103 @@
1
- module JekyllAdmin
2
- class DataFile
3
- METHODS_FOR_LIQUID = %w(path relative_path slug ext title).freeze
4
- EXTENSIONS = %w(yaml yml json csv).freeze
5
-
6
- include APIable
7
- include URLable
8
- include PathHelper
9
- extend PathHelper
10
-
11
- attr_reader :id
12
-
13
- # Initialize a new DataFile object
14
- #
15
- # id - the file ID as passed from the API. This may or may not have an extension
16
- def initialize(id)
17
- @id ||= File.extname(id).empty? ? "#{id}.yml" : id
18
- end
19
- alias_method :relative_path, :id
20
-
21
- def exists?
22
- @exists ||= File.exist?(absolute_path)
23
- end
24
-
25
- # Returns unparsed content as it exists on disk
26
- def raw_content
27
- @raw_content ||= File.open(absolute_path, "r:UTF-8", &:read)
28
- end
29
-
30
- # Returnes (re)parsed content using Jekyll's native parsing mechanism
31
- def content
32
- @content ||= data_reader.read_data_file(absolute_path)
33
- end
34
-
35
- # Returns the file's extension with preceeding `.`
36
- def ext
37
- @ext ||= if File.extname(id).to_s.empty?
38
- ".yml"
39
- else
40
- File.extname(id)
41
- end
42
- end
43
- alias_method :extension, :ext
44
-
45
- # Returns the file's sanitized slug (as used in `site.data[slug]`)
46
- def slug
47
- @slug ||= data_reader.sanitize_filename(basename)
48
- end
49
-
50
- # Returns the human-readable title of the data file
51
- def title
52
- @title ||= Jekyll::Utils.titleize_slug(slug.tr("_", "-"))
53
- end
54
-
55
- # Returns path relative to site source
56
- def path
57
- ensure_leading_slash(File.join(DataFile.data_dir, relative_path))
58
- end
59
-
60
- def absolute_path
61
- sanitized_path(path)
62
- end
63
- alias_method :file_path, :absolute_path
64
-
65
- # Mimics Jekyll's native to_liquid functionality by returning a hash
66
- # of data file metadata
67
- def to_liquid
68
- @to_liquid ||= METHODS_FOR_LIQUID.map { |key| [key, public_send(key)] }.to_h
69
- end
70
-
71
- def self.all
72
- data_dir = Jekyll.sanitized_path(JekyllAdmin.site.source, DataFile.data_dir)
73
- data_dir = Pathname.new(data_dir)
74
- Dir["#{data_dir}/**/*.{#{EXTENSIONS.join(",")}}"].map do |path|
75
- new(Pathname.new(path).relative_path_from(data_dir).to_s)
76
- end
77
- end
78
-
79
- # Relative path to data directory within site source
80
- def self.data_dir
81
- JekyllAdmin.site.config["data_dir"]
82
- end
83
-
84
- private
85
-
86
- def data_reader
87
- @data_reader = Jekyll::DataReader.new(JekyllAdmin.site)
88
- end
89
-
90
- def basename
91
- @basename ||= File.basename(id, ".*")
92
- end
93
-
94
- def basename_with_extension
95
- [basename, extension].join
96
- end
97
- alias_method :filename, :basename_with_extension
98
-
99
- def namespace
100
- "data"
101
- end
102
- end
103
- end
1
+ module JekyllAdmin
2
+ class DataFile
3
+ METHODS_FOR_LIQUID = %w(path relative_path slug ext title).freeze
4
+ EXTENSIONS = %w(yaml yml json csv).freeze
5
+
6
+ include APIable
7
+ include URLable
8
+ include PathHelper
9
+ extend PathHelper
10
+
11
+ attr_reader :id
12
+
13
+ # Initialize a new DataFile object
14
+ #
15
+ # id - the file ID as passed from the API. This may or may not have an extension
16
+ def initialize(id)
17
+ @id = File.extname(id).empty? ? "#{id}.yml" : id
18
+ end
19
+ alias_method :relative_path, :id
20
+
21
+ def exists?
22
+ @exists ||= File.exist?(absolute_path)
23
+ end
24
+
25
+ # Returns unparsed content as it exists on disk
26
+ def raw_content
27
+ @raw_content ||= File.open(absolute_path, "r:UTF-8", &:read)
28
+ end
29
+
30
+ # Returnes (re)parsed content using Jekyll's native parsing mechanism
31
+ def content
32
+ @content ||= data_reader.read_data_file(absolute_path)
33
+ end
34
+
35
+ # Returns the file's extension with preceeding `.`
36
+ def ext
37
+ @ext ||= if File.extname(id).to_s.empty?
38
+ ".yml"
39
+ else
40
+ File.extname(id)
41
+ end
42
+ end
43
+ alias_method :extension, :ext
44
+
45
+ # Returns the file's sanitized slug (as used in `site.data[slug]`)
46
+ def slug
47
+ @slug ||= data_reader.sanitize_filename(basename)
48
+ end
49
+
50
+ # Returns the human-readable title of the data file
51
+ def title
52
+ @title ||= Jekyll::Utils.titleize_slug(slug.tr("_", "-"))
53
+ end
54
+
55
+ # Returns path relative to site source
56
+ def path
57
+ ensure_leading_slash(File.join(DataFile.data_dir, relative_path))
58
+ end
59
+
60
+ def absolute_path
61
+ sanitized_path(path)
62
+ end
63
+ alias_method :file_path, :absolute_path
64
+
65
+ # Mimics Jekyll's native to_liquid functionality by returning a hash
66
+ # of data file metadata
67
+ def to_liquid
68
+ @to_liquid ||= METHODS_FOR_LIQUID.map { |key| [key, public_send(key)] }.to_h
69
+ end
70
+
71
+ def self.all
72
+ data_dir = Jekyll.sanitized_path(JekyllAdmin.site.source, DataFile.data_dir)
73
+ data_dir = Pathname.new(data_dir)
74
+ Dir["#{data_dir}/**/*.{#{EXTENSIONS.join(",")}}"].map do |path|
75
+ new(Pathname.new(path).relative_path_from(data_dir).to_s)
76
+ end
77
+ end
78
+
79
+ # Relative path to data directory within site source
80
+ def self.data_dir
81
+ JekyllAdmin.site.config["data_dir"]
82
+ end
83
+
84
+ private
85
+
86
+ def data_reader
87
+ @data_reader = Jekyll::DataReader.new(JekyllAdmin.site)
88
+ end
89
+
90
+ def basename
91
+ @basename ||= File.basename(id, ".*")
92
+ end
93
+
94
+ def basename_with_extension
95
+ [basename, extension].join
96
+ end
97
+ alias_method :filename, :basename_with_extension
98
+
99
+ def namespace
100
+ "data"
101
+ end
102
+ end
103
+ end
@@ -1,73 +1,74 @@
1
- module JekyllAdmin
2
- class Directory
3
- extend Forwardable
4
- def_delegator :@path, :basename, :name
5
- def_delegator :@path, :mtime, :modified_time
6
- attr_reader :path, :splat, :content_type, :base
7
-
8
- include Enumerable
9
- include JekyllAdmin::URLable
10
- include JekyllAdmin::APIable
11
-
12
- TYPE = :directory
13
-
14
- # Arguments:
15
- #
16
- # path - full path of the directory which its entries will be listed
17
- #
18
- # base - passes site.source to generate `relative_path` needed for `to_api`
19
- #
20
- # content_type - type of the requested directory entries, this is used to generate
21
- # API endpoint of the directory along with `splat`
22
- #
23
- # splat - the requested directory path relative to content namespace
24
- def initialize(path, base: nil, content_type: nil, splat: nil)
25
- @base = Pathname.new base
26
- @content_type = content_type
27
- @splat = Pathname.new splat
28
- @path = Pathname.new path
29
- end
30
-
31
- def to_liquid
32
- {
33
- :name => name,
34
- :modified_time => modified_time,
35
- :path => relative_path,
36
- :type => TYPE,
37
- }
38
- end
39
-
40
- def relative_path
41
- if content_type == "drafts"
42
- path.relative_path_from(base).to_s.sub("_drafts/", "")
43
- else
44
- path.relative_path_from(base).to_s
45
- end
46
- end
47
-
48
- def resource_path
49
- types = %w(pages data drafts)
50
- if types.include?(content_type)
51
- "/#{content_type}/#{splat}/#{name}"
52
- else
53
- "/collections/#{content_type}/entries/#{splat}/#{name}"
54
- end
55
- end
56
- alias_method :url, :resource_path
57
-
58
- def http_url
59
- nil
60
- end
61
-
62
- def directories
63
- path.entries.map do |entry|
64
- next if [".", ".."].include? entry.to_s
65
- next unless path.join(entry).directory?
66
- self.class.new(
67
- path.join(entry),
68
- :base => base, :content_type => content_type, :splat => splat
69
- )
70
- end.compact!
71
- end
72
- end
73
- end
1
+ module JekyllAdmin
2
+ class Directory
3
+ extend Forwardable
4
+ def_delegator :@path, :basename, :name
5
+ def_delegator :@path, :mtime, :modified_time
6
+ attr_reader :path, :splat, :content_type, :base
7
+
8
+ include Enumerable
9
+ include JekyllAdmin::URLable
10
+ include JekyllAdmin::APIable
11
+
12
+ TYPE = :directory
13
+
14
+ # Arguments:
15
+ #
16
+ # path - full path of the directory which its entries will be listed
17
+ #
18
+ # base - passes site.source to generate `relative_path` needed for `to_api`
19
+ #
20
+ # content_type - type of the requested directory entries, this is used to generate
21
+ # API endpoint of the directory along with `splat`
22
+ #
23
+ # splat - the requested directory path relative to content namespace
24
+ def initialize(path, base: nil, content_type: nil, splat: nil)
25
+ @base = Pathname.new base
26
+ @content_type = content_type
27
+ @splat = Pathname.new splat
28
+ @path = Pathname.new path
29
+ end
30
+
31
+ def to_liquid
32
+ {
33
+ :name => name,
34
+ :modified_time => modified_time,
35
+ :path => relative_path,
36
+ :type => TYPE,
37
+ }
38
+ end
39
+
40
+ def relative_path
41
+ if content_type == "drafts"
42
+ path.relative_path_from(base).to_s.sub("_drafts/", "")
43
+ else
44
+ path.relative_path_from(base).to_s
45
+ end
46
+ end
47
+
48
+ def resource_path
49
+ types = %w(pages data drafts)
50
+ if types.include?(content_type)
51
+ "/#{content_type}/#{splat}/#{name}"
52
+ else
53
+ "/collections/#{content_type}/entries/#{splat}/#{name}"
54
+ end
55
+ end
56
+ alias_method :url, :resource_path
57
+
58
+ def http_url
59
+ nil
60
+ end
61
+
62
+ def directories
63
+ path.entries.map do |entry|
64
+ next if [".", ".."].include? entry.to_s
65
+ next unless path.join(entry).directory?
66
+
67
+ self.class.new(
68
+ path.join(entry),
69
+ :base => base, :content_type => content_type, :splat => splat
70
+ )
71
+ end.compact!
72
+ end
73
+ end
74
+ end
@@ -1,78 +1,77 @@
1
- module JekyllAdmin
2
- module FileHelper
3
-
4
- # The file the user requested in the URL
5
- def requested_file
6
- find_by_path(path)
7
- end
8
-
9
- # The file ultimately written to disk
10
- # This may be the requested file, or in the case of a rename will be read
11
- # from the new path that was actually written to disk
12
- def written_file
13
- find_by_path(write_path)
14
- end
15
-
16
- # Write a file to disk with the given content
17
- def write_file(path, content)
18
- Jekyll.logger.debug "WRITING:", path
19
- path = sanitized_path(path)
20
- FileUtils.mkdir_p File.dirname(path)
21
- File.open(path, "wb") do |file|
22
- file.write(content)
23
- end
24
- # we should fully process in dev mode for tests to pass
25
- if ENV["RACK_ENV"] == "production"
26
- site.read
27
- else
28
- site.process
29
- end
30
- end
31
-
32
- # Delete the file at the given path
33
- def delete_file(path)
34
- Jekyll.logger.debug "DELETING:", path
35
- FileUtils.rm_f sanitized_path(path)
36
- site.process
37
- end
38
-
39
- def delete_file_without_process(path)
40
- Jekyll.logger.debug "DELETING:", path
41
- FileUtils.rm_f sanitized_path(path)
42
- end
43
-
44
- private
45
-
46
- def ensure_requested_file
47
- ensure_file(requested_file)
48
- end
49
-
50
- def ensure_written_file
51
- ensure_file(written_file)
52
- end
53
-
54
- def find_by_path(path)
55
- files = case namespace
56
- when "collections"
57
- collection.docs
58
- when "data"
59
- DataFile.all
60
- when "drafts"
61
- drafts
62
- when "pages", "static_files"
63
- site.public_send(namespace.to_sym)
64
- else
65
- []
66
- end
67
- files.find { |f| sanitized_path(f.path) == path }
68
- end
69
-
70
- def ensure_file(file)
71
- render_404 if file.nil?
72
- end
73
-
74
- def ensure_directory
75
- render_404 unless Dir.exist?(directory_path)
76
- end
77
- end
78
- end
1
+ module JekyllAdmin
2
+ module FileHelper
3
+ # The file the user requested in the URL
4
+ def requested_file
5
+ find_by_path(path)
6
+ end
7
+
8
+ # The file ultimately written to disk
9
+ # This may be the requested file, or in the case of a rename will be read
10
+ # from the new path that was actually written to disk
11
+ def written_file
12
+ find_by_path(write_path)
13
+ end
14
+
15
+ # Write a file to disk with the given content
16
+ def write_file(path, content)
17
+ Jekyll.logger.debug "WRITING:", path
18
+ path = sanitized_path(path)
19
+ FileUtils.mkdir_p File.dirname(path)
20
+ File.open(path, "wb") do |file|
21
+ file.write(content)
22
+ end
23
+ # we should fully process in dev mode for tests to pass
24
+ if ENV["RACK_ENV"] == "production"
25
+ site.read
26
+ else
27
+ site.process
28
+ end
29
+ end
30
+
31
+ # Delete the file at the given path
32
+ def delete_file(path)
33
+ Jekyll.logger.debug "DELETING:", path
34
+ FileUtils.rm_f sanitized_path(path)
35
+ site.process
36
+ end
37
+
38
+ def delete_file_without_process(path)
39
+ Jekyll.logger.debug "DELETING:", path
40
+ FileUtils.rm_f sanitized_path(path)
41
+ end
42
+
43
+ private
44
+
45
+ def ensure_requested_file
46
+ ensure_file(requested_file)
47
+ end
48
+
49
+ def ensure_written_file
50
+ ensure_file(written_file)
51
+ end
52
+
53
+ def find_by_path(path)
54
+ files = case namespace
55
+ when "collections"
56
+ collection.docs
57
+ when "data"
58
+ DataFile.all
59
+ when "drafts"
60
+ drafts
61
+ when "pages", "static_files"
62
+ site.public_send(namespace.to_sym)
63
+ else
64
+ []
65
+ end
66
+ files.find { |f| sanitized_path(f.path) == path }
67
+ end
68
+
69
+ def ensure_file(file)
70
+ render_404 if file.nil?
71
+ end
72
+
73
+ def ensure_directory
74
+ render_404 unless Dir.exist?(directory_path)
75
+ end
76
+ end
77
+ end