jekyll-admin 0.8.1 → 0.11.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (58) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +50 -7
  3. data/lib/jekyll-admin.rb +14 -13
  4. data/lib/jekyll-admin/apiable.rb +76 -14
  5. data/lib/jekyll-admin/data_file.rb +19 -16
  6. data/lib/jekyll-admin/directory.rb +25 -26
  7. data/lib/jekyll-admin/file_helper.rb +14 -1
  8. data/lib/jekyll-admin/path_helper.rb +10 -1
  9. data/lib/jekyll-admin/public/asset-manifest.json +27 -0
  10. data/lib/jekyll-admin/public/index.html +1 -12
  11. data/lib/jekyll-admin/public/precache-manifest.64226b0470ae7a98d28d6c8a80943541.js +78 -0
  12. data/lib/jekyll-admin/public/service-worker.js +39 -0
  13. data/lib/jekyll-admin/public/static/css/2.9311179f.chunk.css +2 -0
  14. data/lib/jekyll-admin/public/static/css/2.9311179f.chunk.css.map +1 -0
  15. data/lib/jekyll-admin/public/static/css/main.51bccafc.chunk.css +5 -0
  16. data/lib/jekyll-admin/public/static/css/main.51bccafc.chunk.css.map +1 -0
  17. data/lib/jekyll-admin/public/static/js/2.c4e847cc.chunk.js +3 -0
  18. data/lib/jekyll-admin/public/static/js/2.c4e847cc.chunk.js.LICENSE.txt +69 -0
  19. data/lib/jekyll-admin/public/static/js/2.c4e847cc.chunk.js.map +1 -0
  20. data/lib/jekyll-admin/public/static/js/main.2ce220f0.chunk.js +2 -0
  21. data/lib/jekyll-admin/public/static/js/main.2ce220f0.chunk.js.map +1 -0
  22. data/lib/jekyll-admin/public/static/js/runtime-main.e35fac22.js +2 -0
  23. data/lib/jekyll-admin/public/static/js/runtime-main.e35fac22.js.map +1 -0
  24. data/lib/jekyll-admin/public/{1dc35d25e61d819a9c357074014867ab.ttf → static/media/fontawesome-webfont.1dc35d25.ttf} +0 -0
  25. data/lib/jekyll-admin/public/{25a32416abee198dd821b0b17a198a8f.eot → static/media/fontawesome-webfont.25a32416.eot} +0 -0
  26. data/lib/jekyll-admin/public/{c8ddf1e5e5bf3682bc7bebf30f394148.woff → static/media/fontawesome-webfont.c8ddf1e5.woff} +0 -0
  27. data/lib/jekyll-admin/public/{d7c639084f684d66a1bc66855d193ed8.svg → static/media/fontawesome-webfont.d7c63908.svg} +0 -0
  28. data/lib/jekyll-admin/public/{e6cf7c6ec7c2d6f670ae9d762604cb0b.woff2 → static/media/fontawesome-webfont.e6cf7c6e.woff2} +0 -0
  29. data/lib/jekyll-admin/public/{55131026930a0cd4539d1e2fdb92722d.ttf → static/media/lato-bold.55131026.ttf} +0 -0
  30. data/lib/jekyll-admin/public/{059514c92565e4045da1a69525dd9ec0.ttf → static/media/lato-regular.059514c9.ttf} +0 -0
  31. data/lib/jekyll-admin/public/{a770b6797b68e3f8920e473eb824bac0.gif → static/media/loader-big.a770b679.gif} +0 -0
  32. data/lib/jekyll-admin/public/static/media/rw-widgets.12f0820c.woff +0 -0
  33. data/lib/jekyll-admin/public/{03945ac4fc7fdefc44bc110bf1ba2393.svg → static/media/rw-widgets.792dcd18.svg} +14 -8
  34. data/lib/jekyll-admin/public/{e44520ab9079ea7633bfa874bed5d21d.eot → static/media/rw-widgets.bc7c4a59.eot} +0 -0
  35. data/lib/jekyll-admin/public/{bfc14ac982326f7d0b1340e20d3e0c37.ttf → static/media/rw-widgets.eceddf47.ttf} +0 -0
  36. data/lib/jekyll-admin/server.rb +23 -16
  37. data/lib/jekyll-admin/server/{collection.rb → collections.rb} +12 -5
  38. data/lib/jekyll-admin/server/configuration.rb +9 -6
  39. data/lib/jekyll-admin/server/data.rb +4 -2
  40. data/lib/jekyll-admin/server/{draft.rb → drafts.rb} +11 -7
  41. data/lib/jekyll-admin/server/{page.rb → pages.rb} +24 -5
  42. data/lib/jekyll-admin/server/site_meta.rb +25 -0
  43. data/lib/jekyll-admin/server/static_files.rb +83 -0
  44. data/lib/jekyll-admin/static_server.rb +3 -1
  45. data/lib/jekyll-admin/urlable.rb +9 -1
  46. data/lib/jekyll-admin/version.rb +3 -1
  47. data/lib/jekyll/commands/serve.rb +14 -12
  48. metadata +60 -62
  49. data/lib/jekyll-admin/page_without_a_file.rb +0 -7
  50. data/lib/jekyll-admin/public/33a752211d05af6684e26ec63c2ed965.gif +0 -0
  51. data/lib/jekyll-admin/public/8b4968b70019a0551a72940c5a2020d3.png +0 -0
  52. data/lib/jekyll-admin/public/8ea28ca3bfdf27145068e81dd07a34c6.png +0 -0
  53. data/lib/jekyll-admin/public/99adb54b0f30c0758bb4cb9ed5b80aa8.woff +0 -0
  54. data/lib/jekyll-admin/public/bundle.js +0 -117
  55. data/lib/jekyll-admin/public/bundle.js.map +0 -1
  56. data/lib/jekyll-admin/public/styles.css +0 -5
  57. data/lib/jekyll-admin/public/styles.css.map +0 -1
  58. data/lib/jekyll-admin/server/static_file.rb +0 -61
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 1e3a603092f343692bb2fcd0c1dab8a04da0603daba3eb86ed4a10f44b29c28b
4
- data.tar.gz: 173733135b068e422b498a52a5ba70a6fe7070a07584ead7d9985d01f9aca73a
3
+ metadata.gz: dd575c1a5e2646679878544936041b41705f451b3a9cb38d71272bc3518c5be4
4
+ data.tar.gz: 122cd2307dfd1e3cdd2587da707bae510d9744cdc4453f5eaa24f4e9dd72d290
5
5
  SHA512:
6
- metadata.gz: 75d237355890639a3003b2f7397ed3c4c3386e1a02592eedd0ea89e6066d226bc92c91a27a18c6ebebb491e6a588546f82e3de72860f528c3a57a7771c7c741d
7
- data.tar.gz: 4ed0303b0055911ae268634a3559a38cbe988959dc9405f1be91150065e519e1ea61d6e175b07dd550e496544247e618662a3f263920b2df5533514042ea617a
6
+ metadata.gz: b022a7157b17c8e42f6ecdaa3edcdb162242eb7a000f54f5e1d595ec30d4ea75d41df3eb6e9d3765b370cdc33ab97538ac95f6b31af89fc84fa34bf63337e8b8
7
+ data.tar.gz: 8bbadf4964bb2ab703863d04c5a0c9cf7b9c650bf009a9368435da84750a74aafe1b85ca2634dffb1ce5dc9905a68d610a90b72df4ad589b5c82240861806f2d
data/README.md CHANGED
@@ -1,8 +1,9 @@
1
1
  [![Gem Version](https://img.shields.io/gem/v/jekyll-admin.svg)](https://rubygems.org/gems/jekyll-admin)
2
2
  [![Build Status](https://travis-ci.org/jekyll/jekyll-admin.svg?branch=master)](https://travis-ci.org/jekyll/jekyll-admin)
3
- [![Build status](https://ci.appveyor.com/api/projects/status/biop1r6ae524xlm2/branch/master?svg=true)](https://ci.appveyor.com/project/benbalter/jekyll-admin/branch/master)
3
+ [![Build status](https://ci.appveyor.com/api/projects/status/u6u9tn7rk5tln33s/branch/master?svg=true)](https://ci.appveyor.com/project/jekyll/jekyll-admin)
4
4
  [![Coverage Status](https://coveralls.io/repos/github/jekyll/jekyll-admin/badge.svg?branch=master)](https://coveralls.io/github/jekyll/jekyll-admin?branch=master)
5
5
  [![NPM Dependencies](https://david-dm.org/jekyll/jekyll-admin.svg)](https://david-dm.org/jekyll/jekyll-admin)
6
+ [![Financial Contributors on Open Collective](https://opencollective.com/jekyll-admin/all/badge.svg?label=financial+contributors)](https://opencollective.com/jekyll-admin)
6
7
 
7
8
  A Jekyll plugin that provides users with a traditional CMS-style graphical interface to author content and administer Jekyll sites. The project is divided into two parts. A Ruby-based HTTP API that handles Jekyll and filesystem operations, and a JavaScript-based front end, built on that API.
8
9
 
@@ -10,7 +11,7 @@ A Jekyll plugin that provides users with a traditional CMS-style graphical inter
10
11
 
11
12
  ## Installation
12
13
 
13
- Refer to the [installing plugins](https://jekyllrb.com/docs/plugins/#installing-a-plugin) section of Jekyll's documentation and install the `jekyll-admin` plugin as you would any other plugin. Here's the short version:
14
+ Refer to the [installing plugins](https://jekyllrb.com/docs/plugins/installation/) section of Jekyll's documentation and install the `jekyll-admin` plugin as you would any other plugin. Here's the short version:
14
15
 
15
16
  1. Add the following to your site's Gemfile:
16
17
 
@@ -27,9 +28,7 @@ Refer to the [installing plugins](https://jekyllrb.com/docs/plugins/#installing-
27
28
 
28
29
  ## Options
29
30
 
30
- Jekyll Admin related options can be specified in `_config.yml`
31
- under a key called `jekyll_admin`. Currently it has only one option `hidden_links`
32
- which is for hiding unwanted links on the sidebar. The following keys under `hidden_links` can be used in order to hide default links;
31
+ Jekyll Admin related options can be specified in `_config.yml` under a key called `jekyll_admin`.
33
32
 
34
33
  ```yaml
35
34
  jekyll_admin:
@@ -39,12 +38,56 @@ jekyll_admin:
39
38
  - staticfiles
40
39
  - datafiles
41
40
  - configuration
41
+ homepage: "pages"
42
+ ```
43
+
44
+ ### Customizing collection label in Sidebar
45
+
46
+ The plugin allows you to customize the name of a collection that is displayed in the sidebar by defining it in the collection's
47
+ metadata in the config file. For example, if your source's *posts* are actually *news-items* on the deployed site, then it can
48
+ be distracting to see the label `Posts` in the admin's sidebar. This situation can be resolved with the following configuration:
49
+
50
+ ```yaml
51
+ collections:
52
+ posts:
53
+ output: true
54
+ sidebar_label: News
42
55
  ```
43
56
 
44
57
  ## Contributing
45
58
 
46
- Interested in contributing to Jekyll Admin? We’d love your help. Jekyll Admin is an open source project, built one contribution at a time by users like you. See [the contributing instructions](.github/CONTRIBUTING.md), and [the development docs](http://jekyll.github.io/jekyll-admin/development/) for more information.
59
+ Interested in contributing to Jekyll Admin? We’d love your help. Jekyll Admin is an open source project, built one contribution at a time by users like you. See [the contributing instructions](.github/CONTRIBUTING.md), and [the development docs](https://jekyll.github.io/jekyll-admin/development/) for more information.
60
+
61
+ ## Contributors
62
+
63
+ ### Code Contributors
64
+
65
+ This project exists thanks to all the people who contribute. [[Contribute](.github/CONTRIBUTING.md)].
66
+ <a href="https://github.com/jekyll/jekyll-admin/graphs/contributors"><img src="https://opencollective.com/jekyll-admin/contributors.svg?width=890&button=false" /></a>
67
+
68
+ ### Financial Contributors
69
+
70
+ Become a financial contributor and help us sustain our community. [[Contribute](https://opencollective.com/jekyll-admin/contribute)]
71
+
72
+ #### Individuals
73
+
74
+ <a href="https://opencollective.com/jekyll-admin"><img src="https://opencollective.com/jekyll-admin/individuals.svg?width=890"></a>
75
+
76
+ #### Organizations
77
+
78
+ Support this project with your organization. Your logo will show up here with a link to your website. [[Contribute](https://opencollective.com/jekyll-admin/contribute)]
79
+
80
+ <a href="https://opencollective.com/jekyll-admin/organization/0/website"><img src="https://opencollective.com/jekyll-admin/organization/0/avatar.svg"></a>
81
+ <a href="https://opencollective.com/jekyll-admin/organization/1/website"><img src="https://opencollective.com/jekyll-admin/organization/1/avatar.svg"></a>
82
+ <a href="https://opencollective.com/jekyll-admin/organization/2/website"><img src="https://opencollective.com/jekyll-admin/organization/2/avatar.svg"></a>
83
+ <a href="https://opencollective.com/jekyll-admin/organization/3/website"><img src="https://opencollective.com/jekyll-admin/organization/3/avatar.svg"></a>
84
+ <a href="https://opencollective.com/jekyll-admin/organization/4/website"><img src="https://opencollective.com/jekyll-admin/organization/4/avatar.svg"></a>
85
+ <a href="https://opencollective.com/jekyll-admin/organization/5/website"><img src="https://opencollective.com/jekyll-admin/organization/5/avatar.svg"></a>
86
+ <a href="https://opencollective.com/jekyll-admin/organization/6/website"><img src="https://opencollective.com/jekyll-admin/organization/6/avatar.svg"></a>
87
+ <a href="https://opencollective.com/jekyll-admin/organization/7/website"><img src="https://opencollective.com/jekyll-admin/organization/7/avatar.svg"></a>
88
+ <a href="https://opencollective.com/jekyll-admin/organization/8/website"><img src="https://opencollective.com/jekyll-admin/organization/8/avatar.svg"></a>
89
+ <a href="https://opencollective.com/jekyll-admin/organization/9/website"><img src="https://opencollective.com/jekyll-admin/organization/9/avatar.svg"></a>
47
90
 
48
91
  ## License
49
92
 
50
- The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
93
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  # Default Sinatra to "production" mode (surpress errors) unless
2
4
  # otherwise specified by the `RACK_ENV` environmental variable.
3
5
  # Must be done prior to requiring Sinatra, or we'll get a LoadError
@@ -17,16 +19,15 @@ require "sinatra/reloader"
17
19
  require "sinatra/namespace"
18
20
 
19
21
  module JekyllAdmin
20
- autoload :APIable, "jekyll-admin/apiable"
21
- autoload :DataFile, "jekyll-admin/data_file"
22
- autoload :Directory, "jekyll-admin/directory"
23
- autoload :FileHelper, "jekyll-admin/file_helper"
24
- autoload :PageWithoutAFile, "jekyll-admin/page_without_a_file"
25
- autoload :PathHelper, "jekyll-admin/path_helper"
26
- autoload :Server, "jekyll-admin/server"
27
- autoload :StaticServer, "jekyll-admin/static_server"
28
- autoload :URLable, "jekyll-admin/urlable"
29
- autoload :Version, "jekyll-admin/version"
22
+ autoload :APIable, "jekyll-admin/apiable"
23
+ autoload :DataFile, "jekyll-admin/data_file"
24
+ autoload :Directory, "jekyll-admin/directory"
25
+ autoload :FileHelper, "jekyll-admin/file_helper"
26
+ autoload :PathHelper, "jekyll-admin/path_helper"
27
+ autoload :Server, "jekyll-admin/server"
28
+ autoload :StaticServer, "jekyll-admin/static_server"
29
+ autoload :URLable, "jekyll-admin/urlable"
30
+ autoload :Version, "jekyll-admin/version"
30
31
 
31
32
  def self.site
32
33
  @site ||= begin
@@ -37,10 +38,10 @@ module JekyllAdmin
37
38
  end
38
39
  end
39
40
 
40
- # Monkey Patches
41
- require_relative "./jekyll/commands/serve"
42
-
43
41
  [Jekyll::Page, Jekyll::Document, Jekyll::StaticFile, Jekyll::Collection].each do |klass|
44
42
  klass.include JekyllAdmin::APIable
45
43
  klass.include JekyllAdmin::URLable
46
44
  end
45
+
46
+ # Monkey Patches
47
+ require_relative "jekyll/commands/serve"
@@ -1,9 +1,11 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module JekyllAdmin
2
4
  # Abstract module to be included in Convertible and Document to provide
3
5
  # additional, API-specific functionality without duplicating logic
4
6
  module APIable
5
-
6
7
  CONTENT_FIELDS = %w(next previous content excerpt).freeze
8
+ API_SCAFFOLD = %w(name path relative_path).map { |i| [i, nil] }.to_h.freeze
7
9
 
8
10
  # Returns a hash suitable for use as an API response.
9
11
  #
@@ -19,15 +21,13 @@ module JekyllAdmin
19
21
  # include_content - if true, includes the content in the respond, false by default
20
22
  # to support mapping on indexes where we only want metadata
21
23
  #
22
- #
23
24
  # Returns a hash (which can then be to_json'd)
24
25
  def to_api(include_content: false)
25
- output = hash_for_api
26
- output = output.merge(url_fields)
26
+ output = API_SCAFFOLD.merge hash_for_api
27
27
 
28
28
  # Include content, if requested, otherwise remove it
29
29
  if include_content
30
- output = output.merge(content_fields)
30
+ output.merge!(content_fields)
31
31
  else
32
32
  CONTENT_FIELDS.each { |field| output.delete(field) }
33
33
  end
@@ -36,26 +36,73 @@ module JekyllAdmin
36
36
  # Since it's an API, use `content` in both for consistency
37
37
  output.delete("output")
38
38
 
39
+ output["name"] = basename if is_a?(Jekyll::Document)
40
+ output["from_theme"] = from_theme_gem? if is_a?(Jekyll::StaticFile)
41
+ output["relative_path"] = relative_path_for_api
42
+
39
43
  # By default, calling to_liquid on a collection will return a docs
40
44
  # array with each rendered document, which we don't want.
41
45
  if is_a?(Jekyll::Collection)
42
46
  output.delete("docs")
47
+ output["name"] = label
48
+ output["path"] = relative_directory
43
49
  output["entries_url"] = entries_url
44
50
  end
45
51
 
46
- if is_a?(Jekyll::Document)
47
- output["relative_path"] = relative_path.sub("_drafts/", "") if draft?
48
- output["name"] = basename
49
- end
52
+ output.merge!(url_fields)
53
+ output
54
+ end
50
55
 
51
- if is_a?(Jekyll::StaticFile)
52
- output["from_theme"] = from_theme_gem?
56
+ private
57
+
58
+ # Relative path of files and directories with their *special directories*
59
+ # and any leading slashes stripped away.
60
+ #
61
+ # Examples:
62
+ # `_drafts/foo/draft-post.md` => `foo/draft-post.md`
63
+ # `_posts/foo/2019-10-18-hello.md` => `foo/2019-10-18-hello.md`
64
+ # `/assets/img/logo.png` => `assets/img/logo.png`
65
+ def relative_path_for_api
66
+ return unless respond_to?(:relative_path) && relative_path
67
+
68
+ @relative_path_for_api ||= begin
69
+ rel_path = relative_path.dup
70
+ strip_leading_slash!(rel_path)
71
+ strip_leading_special_directory!(rel_path)
72
+ strip_leading_slash!(rel_path)
73
+
74
+ rel_path
53
75
  end
76
+ end
54
77
 
55
- output
78
+ # Prefer substituting substrings instead of using a regex in order to avoid multiple
79
+ # regex allocations. String literals are frozen and reused.
80
+
81
+ def strip_leading_slash!(path)
82
+ return unless path.start_with?("/")
83
+
84
+ path.sub!("/", "")
56
85
  end
57
86
 
58
- private
87
+ def strip_leading_special_directory!(path)
88
+ return unless special_directory && path.start_with?(special_directory)
89
+
90
+ path.sub!(special_directory, "")
91
+ end
92
+
93
+ def special_directory
94
+ return @special_directory if defined?(@special_directory)
95
+
96
+ @special_directory = begin
97
+ if is_a?(Jekyll::Document) && draft?
98
+ "_drafts"
99
+ elsif is_a?(Jekyll::Document)
100
+ collection.relative_directory
101
+ elsif is_a?(Jekyll::Collection)
102
+ relative_directory
103
+ end
104
+ end
105
+ end
59
106
 
60
107
  # Pages don't have a hash method, but Documents do
61
108
  def file_path
@@ -80,15 +127,26 @@ module JekyllAdmin
80
127
  end
81
128
 
82
129
  def file_contents
83
- @file_contents ||= File.read(file_path, file_read_options) if file_exists?
130
+ @file_contents ||= File.read(file_path, **file_read_options) if file_exists?
84
131
  end
85
132
 
86
133
  def file_read_options
87
134
  Jekyll::Utils.merged_file_read_opts(site, {})
88
135
  end
89
136
 
137
+ def front_matter_defaults
138
+ return unless file_exists?
139
+
140
+ @front_matter_defaults ||= begin
141
+ return {} unless respond_to?(:relative_path) && respond_to?(:type)
142
+
143
+ site.frontmatter_defaults.all(relative_path, type)
144
+ end
145
+ end
146
+
90
147
  def front_matter
91
148
  return unless file_exists?
149
+
92
150
  @front_matter ||= if file_contents =~ Jekyll::Document::YAML_FRONT_MATTER_REGEXP
93
151
  SafeYAML.load(Regexp.last_match(1))
94
152
  else
@@ -98,6 +156,7 @@ module JekyllAdmin
98
156
 
99
157
  def raw_content
100
158
  return unless file_exists?
159
+
101
160
  @raw_content ||= if file_contents =~ Jekyll::Document::YAML_FRONT_MATTER_REGEXP
102
161
  $POSTMATCH
103
162
  else
@@ -111,6 +170,7 @@ module JekyllAdmin
111
170
 
112
171
  def file_exists?
113
172
  return @file_exists if defined? @file_exists
173
+
114
174
  @file_exists = File.exist?(file_path)
115
175
  end
116
176
 
@@ -137,6 +197,7 @@ module JekyllAdmin
137
197
  else
138
198
  output["raw_content"] = raw_content
139
199
  output["front_matter"] = front_matter
200
+ output["front_matter_defaults"] = front_matter_defaults
140
201
  end
141
202
 
142
203
  # Include next and previous documents non-recursively
@@ -153,6 +214,7 @@ module JekyllAdmin
153
214
 
154
215
  def url_fields
155
216
  return {} unless respond_to?(:http_url) && respond_to?(:api_url)
217
+
156
218
  {
157
219
  "http_url" => http_url,
158
220
  "api_url" => api_url,
@@ -1,6 +1,8 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module JekyllAdmin
2
4
  class DataFile
3
- METHODS_FOR_LIQUID = %w(path relative_path slug ext title).freeze
5
+ METHODS_FOR_LIQUID = %w(name path relative_path slug ext title).freeze
4
6
  EXTENSIONS = %w(yaml yml json csv).freeze
5
7
 
6
8
  include APIable
@@ -8,16 +10,26 @@ module JekyllAdmin
8
10
  include PathHelper
9
11
  extend PathHelper
10
12
 
11
- attr_reader :id
13
+ attr_reader :id, :ext
12
14
 
13
15
  # Initialize a new DataFile object
14
16
  #
15
17
  # id - the file ID as passed from the API. This may or may not have an extension
16
18
  def initialize(id)
17
- @id ||= File.extname(id).empty? ? "#{id}.yml" : id
19
+ extname = File.extname(id)
20
+ if extname.empty?
21
+ @id = "#{id}.yml"
22
+ @ext = ".yml"
23
+ else
24
+ @id = id
25
+ @ext = extname
26
+ end
18
27
  end
19
28
  alias_method :relative_path, :id
20
29
 
30
+ # Returns the file's extension with preceeding `.`
31
+ alias_method :extension, :ext
32
+
21
33
  def exists?
22
34
  @exists ||= File.exist?(absolute_path)
23
35
  end
@@ -32,16 +44,6 @@ module JekyllAdmin
32
44
  @content ||= data_reader.read_data_file(absolute_path)
33
45
  end
34
46
 
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
47
  # Returns the file's sanitized slug (as used in `site.data[slug]`)
46
48
  def slug
47
49
  @slug ||= data_reader.sanitize_filename(basename)
@@ -54,7 +56,7 @@ module JekyllAdmin
54
56
 
55
57
  # Returns path relative to site source
56
58
  def path
57
- ensure_leading_slash(File.join(DataFile.data_dir, relative_path))
59
+ @path ||= File.join(DataFile.data_dir, relative_path)
58
60
  end
59
61
 
60
62
  def absolute_path
@@ -92,9 +94,10 @@ module JekyllAdmin
92
94
  end
93
95
 
94
96
  def basename_with_extension
95
- [basename, extension].join
97
+ "#{basename}#{extension}"
96
98
  end
97
- alias_method :filename, :basename_with_extension
99
+ alias_method :name, :basename_with_extension
100
+ public :name
98
101
 
99
102
  def namespace
100
103
  "data"
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module JekyllAdmin
2
4
  class Directory
3
5
  extend Forwardable
@@ -9,45 +11,41 @@ module JekyllAdmin
9
11
  include JekyllAdmin::URLable
10
12
  include JekyllAdmin::APIable
11
13
 
12
- TYPE = :directory
14
+ RESOURCE_TYPES = %w(pages data drafts static_files).freeze
15
+ DOT_DIRECTORIES = [".", ".."].freeze
13
16
 
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`
17
+ private_constant :RESOURCE_TYPES, :DOT_DIRECTORIES
18
+
19
+ # Parameters:
20
+ # path - The full path of the directory at which its entries will be listed.
22
21
  #
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
22
+ # Named parameters:
23
+ # base: - The full path to the directory from source.
24
+ # splat: - The requested directory path relative to content namespace.
25
+ # content_type: - The type of the requested directory entries. Corresponds to
26
+ # the resources' API namespace.
27
+ def initialize(path, base:, splat:, content_type:)
28
+ @path = Pathname.new path
29
+ @base = Pathname.new base
27
30
  @splat = Pathname.new splat
28
- @path = Pathname.new path
31
+ @content_type = content_type
29
32
  end
30
33
 
31
34
  def to_liquid
32
35
  {
33
- :name => name,
34
- :modified_time => modified_time,
35
- :path => relative_path,
36
- :type => TYPE,
36
+ "name" => name,
37
+ "modified_time" => modified_time,
38
+ "path" => relative_path,
39
+ "type" => "directory",
37
40
  }
38
41
  end
39
42
 
40
43
  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
44
+ @relative_path ||= path.relative_path_from(base).to_s
46
45
  end
47
46
 
48
47
  def resource_path
49
- types = %w(pages data drafts)
50
- if types.include?(content_type)
48
+ if RESOURCE_TYPES.include?(content_type)
51
49
  "/#{content_type}/#{splat}/#{name}"
52
50
  else
53
51
  "/collections/#{content_type}/entries/#{splat}/#{name}"
@@ -61,8 +59,9 @@ module JekyllAdmin
61
59
 
62
60
  def directories
63
61
  path.entries.map do |entry|
64
- next if [".", ".."].include? entry.to_s
62
+ next if DOT_DIRECTORIES.include? entry.to_s
65
63
  next unless path.join(entry).directory?
64
+
66
65
  self.class.new(
67
66
  path.join(entry),
68
67
  :base => base, :content_type => content_type, :splat => splat