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,57 +1,57 @@
1
- module JekyllAdmin
2
- class Server < Sinatra::Base
3
- namespace "/configuration" do
4
- get do
5
- json({
6
- :content => parsed_configuration,
7
- :raw_content => raw_configuration,
8
- })
9
- end
10
-
11
- put do
12
- write_file(configuration_path, configuration_body)
13
- json request_payload
14
- end
15
-
16
- private
17
-
18
- def overrides
19
- {
20
- "source" => sanitized_path("/"),
21
- }
22
- end
23
-
24
- # Computed configuration, with updates and defaults
25
- def configuration
26
- @configuration ||= Jekyll.configuration(overrides)
27
- end
28
-
29
- # Configuration data, as read by Jekyll
30
- def parsed_configuration
31
- configuration.read_config_file(configuration_path)
32
- end
33
-
34
- # Raw configuration content, as it sits on disk
35
- def raw_configuration
36
- File.read(
37
- configuration_path,
38
- Jekyll::Utils.merged_file_read_opts(site, {})
39
- )
40
- end
41
-
42
- # Returns the path to the *first* config file discovered
43
- def configuration_path
44
- sanitized_path configuration.config_files(overrides).first
45
- end
46
-
47
- # The user's uploaded configuration for updates
48
- # Instead of extracting `raw_content` directly from the `request_payload`,
49
- # assign the data to a new variable and then extract the `raw_content`
50
- # from it to circumvent CORS violation in `development` mode.
51
- def configuration_body
52
- payload = request_payload
53
- payload["raw_content"]
54
- end
55
- end
56
- end
57
- end
1
+ module JekyllAdmin
2
+ class Server < Sinatra::Base
3
+ namespace "/configuration" do
4
+ get do
5
+ json(
6
+ :content => parsed_configuration,
7
+ :raw_content => raw_configuration
8
+ )
9
+ end
10
+
11
+ put do
12
+ write_file(configuration_path, configuration_body)
13
+ json request_payload
14
+ end
15
+
16
+ private
17
+
18
+ def overrides
19
+ {
20
+ "source" => sanitized_path("/"),
21
+ }
22
+ end
23
+
24
+ # Computed configuration, with updates and defaults
25
+ def configuration
26
+ @configuration ||= Jekyll.configuration(overrides)
27
+ end
28
+
29
+ # Configuration data, as read by Jekyll
30
+ def parsed_configuration
31
+ configuration.read_config_file(configuration_path)
32
+ end
33
+
34
+ # Raw configuration content, as it sits on disk
35
+ def raw_configuration
36
+ File.read(
37
+ configuration_path,
38
+ Jekyll::Utils.merged_file_read_opts(site, {})
39
+ )
40
+ end
41
+
42
+ # Returns the path to the *first* config file discovered
43
+ def configuration_path
44
+ sanitized_path configuration.config_files(overrides).first
45
+ end
46
+
47
+ # The user's uploaded configuration for updates
48
+ # Instead of extracting `raw_content` directly from the `request_payload`,
49
+ # assign the data to a new variable and then extract the `raw_content`
50
+ # from it to circumvent CORS violation in `development` mode.
51
+ def configuration_body
52
+ payload = request_payload
53
+ payload["raw_content"]
54
+ end
55
+ end
56
+ end
57
+ end
@@ -1,82 +1,82 @@
1
- module JekyllAdmin
2
- class Server < Sinatra::Base
3
- # supported extensions, in order of preference, for now, no .csv
4
- EXTENSIONS = %w(yml json).freeze
5
-
6
- namespace "/data" do
7
- get "/*?/?:path.:ext" do
8
- ensure_requested_file
9
- json requested_file.to_api(:include_content => true)
10
- end
11
-
12
- get "/?*" do
13
- ensure_directory
14
- json entries.map(&:to_api)
15
- end
16
-
17
- put "/*?/?:path.:ext" do
18
- if renamed?
19
- ensure_requested_file
20
- delete_file_without_process path
21
- end
22
-
23
- write_file write_path, data_file_body
24
- json written_file.to_api(:include_content => true)
25
- end
26
-
27
- delete "/*?/?:path.:ext" do
28
- ensure_requested_file
29
- delete_file path
30
- content_type :json
31
- status 200
32
- halt
33
- end
34
-
35
- private
36
-
37
- # returns relative path of root level directories that contain data files
38
- def directory_paths
39
- DataFile.all.map { |p| File.dirname(p.relative_path).split("/")[0] }.uniq
40
- end
41
-
42
- def directory_pages
43
- DataFile.all.find_all do |p|
44
- sanitized_path(File.dirname(p.path)) == directory_path
45
- end
46
- end
47
-
48
- def entries
49
- args = {
50
- :base => sanitized_path(DataFile.data_dir),
51
- :content_type => "data",
52
- :splat => splats.first,
53
- }
54
- # get all directories inside the requested directory
55
- directory = JekyllAdmin::Directory.new(directory_path, args)
56
- directories = directory.directories
57
-
58
- # exclude root level directories which do not have data files
59
- if splats.first.empty?
60
- directories = directories.select do |d|
61
- directory_paths.include? d.name.to_s
62
- end
63
- end
64
-
65
- # merge directories with the pages at the same level
66
- directories.concat(directory_pages)
67
- end
68
-
69
- def data_file_body
70
- if !request_payload["raw_content"].to_s.empty?
71
- request_payload["raw_content"]
72
- elsif !request_payload["content"].to_s.empty?
73
- YAML.dump(request_payload["content"]).sub(%r!\A---\n!, "")
74
- end
75
- end
76
-
77
- def splats
78
- params["splat"] || ["/"]
79
- end
80
- end
81
- end
82
- end
1
+ module JekyllAdmin
2
+ class Server < Sinatra::Base
3
+ # supported extensions, in order of preference, for now, no .csv
4
+ EXTENSIONS = %w(yml json).freeze
5
+
6
+ namespace "/data" do
7
+ get "/*?/?:path.:ext" do
8
+ ensure_requested_file
9
+ json requested_file.to_api(:include_content => true)
10
+ end
11
+
12
+ get "/?*" do
13
+ ensure_directory
14
+ json entries.map(&:to_api)
15
+ end
16
+
17
+ put "/*?/?:path.:ext" do
18
+ if renamed?
19
+ ensure_requested_file
20
+ delete_file_without_process path
21
+ end
22
+
23
+ write_file write_path, data_file_body
24
+ json written_file.to_api(:include_content => true)
25
+ end
26
+
27
+ delete "/*?/?:path.:ext" do
28
+ ensure_requested_file
29
+ delete_file path
30
+ content_type :json
31
+ status 200
32
+ halt
33
+ end
34
+
35
+ private
36
+
37
+ # returns relative path of root level directories that contain data files
38
+ def directory_paths
39
+ DataFile.all.map { |p| File.dirname(p.relative_path).split("/")[0] }.uniq
40
+ end
41
+
42
+ def directory_pages
43
+ DataFile.all.find_all do |p|
44
+ sanitized_path(File.dirname(p.path)) == directory_path
45
+ end
46
+ end
47
+
48
+ def entries
49
+ args = {
50
+ :base => sanitized_path(DataFile.data_dir),
51
+ :content_type => "data",
52
+ :splat => splats.first,
53
+ }
54
+ # get all directories inside the requested directory
55
+ directory = JekyllAdmin::Directory.new(directory_path, args)
56
+ directories = directory.directories
57
+
58
+ # exclude root level directories which do not have data files
59
+ if splats.first.empty?
60
+ directories = directories.select do |d|
61
+ directory_paths.include? d.name.to_s
62
+ end
63
+ end
64
+
65
+ # merge directories with the pages at the same level
66
+ directories.concat(directory_pages)
67
+ end
68
+
69
+ def data_file_body
70
+ if !request_payload["raw_content"].to_s.empty?
71
+ request_payload["raw_content"]
72
+ elsif !request_payload["content"].to_s.empty?
73
+ YAML.dump(request_payload["content"]).sub(%r!\A---\n!, "")
74
+ end
75
+ end
76
+
77
+ def splats
78
+ params["splat"] || ["/"]
79
+ end
80
+ end
81
+ end
82
+ end
@@ -1,110 +1,109 @@
1
- module JekyllAdmin
2
- class Server < Sinatra::Base
3
- namespace "/drafts" do
4
- get "/*?/?:path.:ext" do
5
- ensure_requested_file
6
- json requested_file.to_api(:include_content => true)
7
- end
8
-
9
- get "/?*" do
10
- ensure_directory
11
- json entries.map(&:to_api)
12
- end
13
-
14
- put "/*?/?:path.:ext" do
15
- ensure_html_content
16
-
17
- if renamed?
18
- ensure_requested_file
19
- delete_file_without_process path
20
- end
21
-
22
- write_file write_path, document_body
23
- json written_file.to_api(:include_content => true)
24
- end
25
-
26
- delete "/*?/?:path.:ext" do
27
- ensure_requested_file
28
- delete_file path
29
- content_type :json
30
- status 200
31
- halt
32
- end
33
-
34
- private
35
-
36
- # return all documents in the 'posts' collection that output to an html
37
- # file but reside in a separate directory, `<source_dir>/_drafts/`
38
- def drafts
39
- posts = site.collections.find { |l, _c| l == "posts" }
40
- if posts
41
- posts[1].docs.find_all { |d| d.output_ext == ".html" && d.draft? }
42
- end
43
- end
44
-
45
- # return drafts at the same level as directory
46
- def directory_drafts
47
- drafts.find_all { |d| File.dirname(d.path) == directory_path }
48
- end
49
-
50
- def reverse_sorted_drafts
51
- directory_drafts.sort_by(&:date).reverse
52
- end
53
-
54
- # returns directories at the root of `/_drafts/` that contain drafts
55
- def relevant_directory_paths
56
- drafts.map { |doc| relative_draft_path(doc).split("/")[0] }.uniq
57
- end
58
-
59
- def relative_draft_path(document)
60
- File.dirname(document.relative_path.sub("_drafts/", ""))
61
- end
62
-
63
- def ensure_directory
64
- ensure_drafts
65
- render_404 unless Dir.exist?(directory_path)
66
- end
67
-
68
- def ensure_drafts
69
- render_404 if drafts.nil?
70
- end
71
-
72
- def ensure_html_content
73
- return if html_content?
74
- content_type :json
75
- halt 422, json("error_message" => "Invalid file extension for drafts")
76
- end
77
-
78
- def entries
79
- args = {
80
- :base => site.source,
81
- :content_type => "drafts",
82
- :splat => params["splat"].first,
83
- }
84
- # get the directories inside the requested directory
85
- directory = JekyllAdmin::Directory.new(directory_path, args)
86
- directories = directory.directories
87
-
88
- # exclude root level directories which do not have drafts
89
- if params["splat"].first.empty?
90
- directories = directories.select do |d|
91
- relevant_directory_paths.include? d.name.to_s
92
- end
93
- end
94
- # merge directories with the drafts at the same level
95
- directories.concat(reverse_sorted_drafts)
96
- end
97
-
98
- def html_content?
99
- draft = JekyllAdmin::PageWithoutAFile.new(
100
- site,
101
- site.source,
102
- "_drafts",
103
- request_payload["path"] || filename
104
- )
105
- draft.data = request_payload["front_matter"]
106
- draft.html?
107
- end
108
- end
109
- end
110
- end
1
+ module JekyllAdmin
2
+ class Server < Sinatra::Base
3
+ namespace "/drafts" do
4
+ get "/*?/?:path.:ext" do
5
+ ensure_requested_file
6
+ json requested_file.to_api(:include_content => true)
7
+ end
8
+
9
+ get "/?*" do
10
+ ensure_directory
11
+ json entries.map(&:to_api)
12
+ end
13
+
14
+ put "/*?/?:path.:ext" do
15
+ ensure_html_content
16
+
17
+ if renamed?
18
+ ensure_requested_file
19
+ delete_file_without_process path
20
+ end
21
+
22
+ write_file write_path, document_body
23
+ json written_file.to_api(:include_content => true)
24
+ end
25
+
26
+ delete "/*?/?:path.:ext" do
27
+ ensure_requested_file
28
+ delete_file path
29
+ content_type :json
30
+ status 200
31
+ halt
32
+ end
33
+
34
+ private
35
+
36
+ # return all documents in the 'posts' collection that output to an html
37
+ # file but reside in a separate directory, `<source_dir>/_drafts/`
38
+ def drafts
39
+ posts = site.collections.find { |l, _c| l == "posts" }
40
+ posts[1].docs.find_all { |d| d.output_ext == ".html" && d.draft? } if posts
41
+ end
42
+
43
+ # return drafts at the same level as directory
44
+ def directory_drafts
45
+ drafts.find_all { |d| File.dirname(d.path) == directory_path }
46
+ end
47
+
48
+ def reverse_sorted_drafts
49
+ directory_drafts.sort_by(&:date).reverse
50
+ end
51
+
52
+ # returns directories at the root of `/_drafts/` that contain drafts
53
+ def relevant_directory_paths
54
+ drafts.map { |doc| relative_draft_path(doc).split("/")[0] }.uniq
55
+ end
56
+
57
+ def relative_draft_path(document)
58
+ File.dirname(document.relative_path.sub("_drafts/", ""))
59
+ end
60
+
61
+ def ensure_directory
62
+ ensure_drafts
63
+ render_404 unless Dir.exist?(directory_path)
64
+ end
65
+
66
+ def ensure_drafts
67
+ render_404 if drafts.nil?
68
+ end
69
+
70
+ def ensure_html_content
71
+ return if html_content?
72
+
73
+ content_type :json
74
+ halt 422, json("error_message" => "Invalid file extension for drafts")
75
+ end
76
+
77
+ def entries
78
+ args = {
79
+ :base => site.source,
80
+ :content_type => "drafts",
81
+ :splat => params["splat"].first,
82
+ }
83
+ # get the directories inside the requested directory
84
+ directory = JekyllAdmin::Directory.new(directory_path, args)
85
+ directories = directory.directories
86
+
87
+ # exclude root level directories which do not have drafts
88
+ if params["splat"].first.empty?
89
+ directories = directories.select do |d|
90
+ relevant_directory_paths.include? d.name.to_s
91
+ end
92
+ end
93
+ # merge directories with the drafts at the same level
94
+ directories.concat(reverse_sorted_drafts)
95
+ end
96
+
97
+ def html_content?
98
+ draft = JekyllAdmin::PageWithoutAFile.new(
99
+ site,
100
+ site.source,
101
+ "_drafts",
102
+ request_payload["path"] || filename
103
+ )
104
+ draft.data = request_payload["front_matter"]
105
+ draft.html?
106
+ end
107
+ end
108
+ end
109
+ end