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,90 +1,91 @@
1
- module JekyllAdmin
2
- class Server < Sinatra::Base
3
- namespace "/pages" 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, page_body
23
-
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
- def ensure_html_content
38
- return if html_content?
39
- content_type :json
40
- halt 422, json("error_message" => "Invalid file extension for pages")
41
- end
42
-
43
- def html_content?
44
- page = JekyllAdmin::PageWithoutAFile.new(
45
- site,
46
- site.source,
47
- "",
48
- request_payload["path"] || filename
49
- )
50
- page.data = request_payload["front_matter"]
51
- page.html?
52
- end
53
-
54
- def pages
55
- site.pages.select(&:html?)
56
- end
57
-
58
- def directory_pages
59
- pages.find_all do |p|
60
- sanitized_path(File.dirname(p.path)) == directory_path
61
- end
62
- end
63
-
64
- # returns relative path of root level directories that contain pages
65
- def directory_paths
66
- pages.map { |p| File.dirname(p.path).split("/")[0] }.uniq
67
- end
68
-
69
- def entries
70
- args = {
71
- :base => site.source,
72
- :content_type => "pages",
73
- :splat => params["splat"].first,
74
- }
75
- # get all directories inside the requested directory
76
- directory = JekyllAdmin::Directory.new(directory_path, args)
77
- directories = directory.directories
78
-
79
- # exclude root level directories which do not have pages
80
- if params["splat"].first.empty?
81
- directories = directories.select do |d|
82
- directory_paths.include? d.name.to_s
83
- end
84
- end
85
- # merge directories with the pages at the same level
86
- directories.concat(directory_pages)
87
- end
88
- end
89
- end
90
- end
1
+ module JekyllAdmin
2
+ class Server < Sinatra::Base
3
+ namespace "/pages" 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, page_body
23
+
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
+ def ensure_html_content
38
+ return if html_content?
39
+
40
+ content_type :json
41
+ halt 422, json("error_message" => "Invalid file extension for pages")
42
+ end
43
+
44
+ def html_content?
45
+ page = JekyllAdmin::PageWithoutAFile.new(
46
+ site,
47
+ site.source,
48
+ "",
49
+ request_payload["path"] || filename
50
+ )
51
+ page.data = request_payload["front_matter"]
52
+ page.html?
53
+ end
54
+
55
+ def pages
56
+ site.pages.select(&:html?)
57
+ end
58
+
59
+ def directory_pages
60
+ pages.find_all do |p|
61
+ sanitized_path(File.dirname(p.path)) == directory_path
62
+ end
63
+ end
64
+
65
+ # returns relative path of root level directories that contain pages
66
+ def directory_paths
67
+ pages.map { |p| File.dirname(p.path).split("/")[0] }.uniq
68
+ end
69
+
70
+ def entries
71
+ args = {
72
+ :base => site.source,
73
+ :content_type => "pages",
74
+ :splat => params["splat"].first,
75
+ }
76
+ # get all directories inside the requested directory
77
+ directory = JekyllAdmin::Directory.new(directory_path, args)
78
+ directories = directory.directories
79
+
80
+ # exclude root level directories which do not have pages
81
+ if params["splat"].first.empty?
82
+ directories = directories.select do |d|
83
+ directory_paths.include? d.name.to_s
84
+ end
85
+ end
86
+ # merge directories with the pages at the same level
87
+ directories.concat(directory_pages)
88
+ end
89
+ end
90
+ end
91
+ end
@@ -1,61 +1,61 @@
1
- module JekyllAdmin
2
- class Server < Sinatra::Base
3
- namespace "/static_files" do
4
- get do
5
- json static_files.map(&:to_api)
6
- end
7
-
8
- get "/*" do
9
- if requested_file
10
- json requested_file.to_api(:include_content => true)
11
- elsif !static_files_for_path.empty?
12
- json static_files_for_path.map(&:to_api)
13
- else
14
- render_404
15
- end
16
- end
17
-
18
- put "/*" do
19
- if renamed?
20
- ensure_requested_file
21
- delete_file_without_process path
22
- end
23
-
24
- write_file(write_path, static_file_body)
25
- json written_file.to_api(:include_content => true)
26
- end
27
-
28
- delete "/*" do
29
- ensure_requested_file
30
- delete_file path
31
- content_type :json
32
- status 200
33
- halt
34
- end
35
-
36
- private
37
-
38
- def static_file_body
39
- if !request_payload["raw_content"].to_s.empty?
40
- request_payload["raw_content"].to_s
41
- else
42
- Base64.decode64 request_payload["encoded_content"].to_s
43
- end
44
- end
45
-
46
- def static_files
47
- site.static_files
48
- end
49
-
50
- def file_list_dir(path) end
51
-
52
- def static_files_for_path
53
- # Joined with / to ensure user can't do partial paths
54
- base_path = File.join(path, "/")
55
- static_files.select do |f|
56
- f.path.start_with? base_path
57
- end
58
- end
59
- end
60
- end
61
- end
1
+ module JekyllAdmin
2
+ class Server < Sinatra::Base
3
+ namespace "/static_files" do
4
+ get do
5
+ json static_files.map(&:to_api)
6
+ end
7
+
8
+ get "/*" do
9
+ if requested_file
10
+ json requested_file.to_api(:include_content => true)
11
+ elsif !static_files_for_path.empty?
12
+ json static_files_for_path.map(&:to_api)
13
+ else
14
+ render_404
15
+ end
16
+ end
17
+
18
+ put "/*" do
19
+ if renamed?
20
+ ensure_requested_file
21
+ delete_file_without_process path
22
+ end
23
+
24
+ write_file(write_path, static_file_body)
25
+ json written_file.to_api(:include_content => true)
26
+ end
27
+
28
+ delete "/*" do
29
+ ensure_requested_file
30
+ delete_file path
31
+ content_type :json
32
+ status 200
33
+ halt
34
+ end
35
+
36
+ private
37
+
38
+ def static_file_body
39
+ if !request_payload["raw_content"].to_s.empty?
40
+ request_payload["raw_content"].to_s
41
+ else
42
+ Base64.decode64 request_payload["encoded_content"].to_s
43
+ end
44
+ end
45
+
46
+ def static_files
47
+ site.static_files
48
+ end
49
+
50
+ def file_list_dir(path) end
51
+
52
+ def static_files_for_path
53
+ # Joined with / to ensure user can't do partial paths
54
+ base_path = File.join(path, "/")
55
+ static_files.select do |f|
56
+ f.path.start_with? base_path
57
+ end
58
+ end
59
+ end
60
+ end
61
+ end
@@ -1,24 +1,24 @@
1
- module JekyllAdmin
2
- class StaticServer < Sinatra::Base
3
- set :public_dir, File.expand_path("./public", File.dirname(__FILE__))
4
-
5
- MUST_BUILD_MESSAGE = "Front end not yet built. Run `script/build` to build.".freeze
6
-
7
- # Allow `/admin` and `/admin/`, and `/admin/*` to serve `/public/dist/index.html`
8
- get "/*" do
9
- send_file index_path
10
- end
11
-
12
- # Provide a descriptive error message in dev. if frontend is not build
13
- not_found do
14
- status 404
15
- MUST_BUILD_MESSAGE if settings.development? || settings.test?
16
- end
17
-
18
- private
19
-
20
- def index_path
21
- @index_path ||= File.join(settings.public_folder, "index.html")
22
- end
23
- end
24
- end
1
+ module JekyllAdmin
2
+ class StaticServer < Sinatra::Base
3
+ set :public_dir, File.expand_path("./public", File.dirname(__FILE__))
4
+
5
+ MUST_BUILD_MESSAGE = "Front end not yet built. Run `script/build` to build.".freeze
6
+
7
+ # Allow `/admin` and `/admin/`, and `/admin/*` to serve `/public/dist/index.html`
8
+ get "/*" do
9
+ send_file index_path
10
+ end
11
+
12
+ # Provide a descriptive error message in dev. if frontend is not build
13
+ not_found do
14
+ status 404
15
+ MUST_BUILD_MESSAGE if settings.development? || settings.test?
16
+ end
17
+
18
+ private
19
+
20
+ def index_path
21
+ @index_path ||= File.join(settings.public_folder, "index.html")
22
+ end
23
+ end
24
+ end
@@ -1,67 +1,71 @@
1
- module JekyllAdmin
2
- # Abstract module to be included in Convertible and Document to provide
3
- # additional, URL-specific functionality without duplicating logic
4
- module URLable
5
-
6
- # Absolute URL to the HTTP(S) rendered/served representation of this resource
7
- def http_url
8
- return if is_a?(Jekyll::Collection) || is_a?(JekyllAdmin::DataFile)
9
- return if is_a?(Jekyll::Document) && !collection.write?
10
- @http_url ||= Addressable::URI.new(
11
- :scheme => scheme, :host => host, :port => port,
12
- :path => path_with_base(JekyllAdmin.site.config["baseurl"], url)
13
- ).normalize.to_s
14
- end
15
-
16
- # Absolute URL to the API representation of this resource
17
- def api_url
18
- @api_url ||= Addressable::URI.new(
19
- :scheme => scheme, :host => host, :port => port,
20
- :path => path_with_base("/_api", resource_path)
21
- ).normalize.to_s
22
- end
23
-
24
- def entries_url
25
- return unless is_a?(Jekyll::Collection)
26
- "#{api_url}/entries"
27
- end
28
-
29
- private
30
-
31
- # URL path relative to `_api/` to retreive the given resource via the API
32
- # Note: we can't use a case statement here, because === doesn't like includes
33
- def resource_path
34
- if is_a?(Jekyll::Document) && draft?
35
- "/#{relative_path.sub(%r!\A_!, "")}"
36
- elsif is_a?(Jekyll::Document)
37
- "/collections/#{relative_path.sub(%r!\A_!, "")}"
38
- elsif is_a?(Jekyll::Collection)
39
- "/collections/#{label}"
40
- elsif is_a?(JekyllAdmin::DataFile)
41
- "/data/#{relative_path}"
42
- elsif is_a?(Jekyll::StaticFile)
43
- "/static_files/#{relative_path}"
44
- elsif is_a?(Jekyll::Page)
45
- "/pages/#{relative_path}"
46
- end
47
- end
48
-
49
- # URI.join doesn't like joining two relative paths, and File.join may join
50
- # with `\` rather than with `/` on windows
51
- def path_with_base(base, path)
52
- [base, path].join("/").squeeze("/")
53
- end
54
-
55
- def scheme
56
- JekyllAdmin.site.config["scheme"] || "http"
57
- end
58
-
59
- def host
60
- JekyllAdmin.site.config["host"].sub("127.0.0.1", "localhost")
61
- end
62
-
63
- def port
64
- JekyllAdmin.site.config["port"]
65
- end
66
- end
67
- end
1
+ module JekyllAdmin
2
+ # Abstract module to be included in Convertible and Document to provide
3
+ # additional, URL-specific functionality without duplicating logic
4
+ module URLable
5
+ # Absolute URL to the HTTP(S) rendered/served representation of this resource
6
+ def http_url
7
+ return if is_a?(Jekyll::Collection) || is_a?(JekyllAdmin::DataFile)
8
+ return if is_a?(Jekyll::Document) && !collection.write?
9
+
10
+ @http_url ||= Addressable::URI.new(
11
+ :scheme => scheme, :host => host, :port => port,
12
+ :path => path_with_base(JekyllAdmin.site.config["baseurl"], url)
13
+ ).normalize.to_s
14
+ end
15
+
16
+ # Absolute URL to the API representation of this resource
17
+ def api_url
18
+ @api_url ||= Addressable::URI.new(
19
+ :scheme => scheme, :host => host, :port => port,
20
+ :path => path_with_base("/_api", resource_path)
21
+ ).normalize.to_s
22
+ end
23
+
24
+ def entries_url
25
+ return unless is_a?(Jekyll::Collection)
26
+
27
+ "#{api_url}/entries"
28
+ end
29
+
30
+ private
31
+
32
+ # URL path relative to `_api/` to retreive the given resource via the API
33
+ # Note: we can't use a case statement here, because === doesn't like includes
34
+ #
35
+ # rubocop:disable Metrics/CyclomaticComplexity
36
+ def resource_path
37
+ if is_a?(Jekyll::Document) && draft?
38
+ "/#{relative_path.sub(%r!\A_!, "")}"
39
+ elsif is_a?(Jekyll::Document)
40
+ "/collections/#{relative_path.sub(%r!\A_!, "")}"
41
+ elsif is_a?(Jekyll::Collection)
42
+ "/collections/#{label}"
43
+ elsif is_a?(JekyllAdmin::DataFile)
44
+ "/data/#{relative_path}"
45
+ elsif is_a?(Jekyll::StaticFile)
46
+ "/static_files/#{relative_path}"
47
+ elsif is_a?(Jekyll::Page)
48
+ "/pages/#{relative_path}"
49
+ end
50
+ end
51
+ # rubocop:enable Metrics/CyclomaticComplexity
52
+
53
+ # URI.join doesn't like joining two relative paths, and File.join may join
54
+ # with `\` rather than with `/` on windows
55
+ def path_with_base(base, path)
56
+ [base, path].join("/").squeeze("/")
57
+ end
58
+
59
+ def scheme
60
+ JekyllAdmin.site.config["scheme"] || "http"
61
+ end
62
+
63
+ def host
64
+ JekyllAdmin.site.config["host"].sub("127.0.0.1", "localhost")
65
+ end
66
+
67
+ def port
68
+ JekyllAdmin.site.config["port"]
69
+ end
70
+ end
71
+ end