1
1
use std:: path:: { Path , PathBuf } ;
2
2
3
3
use comrak:: { format_html_with_plugins, parse_document, Arena , ComrakPlugins } ;
4
- use rocket:: { http:: Status , route:: Route , State } ;
4
+ use rocket:: {
5
+ fs:: NamedFile ,
6
+ http:: { uri:: Origin , Status } ,
7
+ route:: Route ,
8
+ State ,
9
+ } ;
5
10
use yaml_rust:: YamlLoader ;
6
11
7
12
use crate :: {
@@ -24,35 +29,63 @@ async fn search(query: &str, index: &State<markdown::SearchIndex>) -> ResponseOk
24
29
)
25
30
}
26
31
32
+ struct Content {
33
+ name : String ,
34
+ }
27
35
28
- use rocket:: fs:: NamedFile ;
29
- use rocket:: http:: uri:: Origin ;
36
+ impl Content {
37
+ pub fn gitbook_asset_path ( & self , path : & str ) -> PathBuf {
38
+ PathBuf :: from ( & config:: cms_dir ( ) )
39
+ . join ( self . name . to_lowercase ( ) )
40
+ . join ( ".gitbook" )
41
+ . join ( "assets" )
42
+ . join ( path)
43
+ }
44
+ }
45
+
46
+ pub async fn get_asset ( path : & PathBuf ) -> Option < NamedFile > {
47
+ NamedFile :: open ( path) . await . ok ( )
48
+ }
30
49
31
50
#[ get( "/careers/.gitbook/assets/<path>" , rank = 10 ) ]
32
- pub async fn careers_assets ( path : PathBuf ) -> Option < NamedFile > {
33
- let path = PathBuf :: from ( & config:: docs_dir ( ) )
34
- . join ( "careers" ) . join ( ".gitbook" ) . join ( "assets" )
51
+ pub async fn get_asset_careers ( path : & str ) -> Option < NamedFile > {
52
+ let content = Content {
53
+ name : "Careers" . to_owned ( ) ,
54
+ } ;
55
+ get_asset ( & content. gitbook_asset_path ( path) ) . await
56
+ }
57
+
58
+ #[ get( "/docs/.gitbook/assets/<path>" , rank = 10 ) ]
59
+ pub async fn get_asset_docs ( path : & str ) -> Option < NamedFile > {
60
+ let path = PathBuf :: from ( & config:: cms_dir ( ) )
61
+ . join ( "docs" )
62
+ . join ( ".gitbook" )
63
+ . join ( "assets" )
35
64
. join ( path) ;
36
65
37
66
NamedFile :: open ( path) . await . ok ( )
38
67
}
39
68
40
69
#[ get( "/careers/<path..>" , rank = 5 ) ]
41
- async fn careers_contenthandler ( mut path : PathBuf , cluster : & Cluster , origin : & Origin < ' _ > ) -> Result < ResponseOk , Status > {
70
+ async fn careers_handler (
71
+ mut path : PathBuf ,
72
+ cluster : & Cluster ,
73
+ origin : & Origin < ' _ > ,
74
+ ) -> Result < ResponseOk , Status > {
42
75
// Rocket 0.5 began stripping trailing '/' from the path
43
76
if origin. path ( ) . ends_with ( "/" ) {
44
77
path = path. join ( "/" ) ;
45
78
}
46
79
let root = PathBuf :: from ( "careers/" ) ;
47
- let index_path = PathBuf :: from ( & config:: docs_dir ( ) )
80
+ let index_path = PathBuf :: from ( & config:: cms_dir ( ) )
48
81
. join ( & root)
49
82
. join ( "SUMMARY.md" ) ;
50
83
let contents = tokio:: fs:: read_to_string ( & index_path) . await . expect (
51
84
format ! (
52
85
"could not read table of contents markdown: {:?}" ,
53
86
index_path
54
87
)
55
- . as_str ( ) ,
88
+ . as_str ( ) ,
56
89
) ;
57
90
let mdast = :: markdown:: to_mdast ( & contents, & :: markdown:: ParseOptions :: default ( ) )
58
91
. expect ( "could not parse table of contents markdown" ) ;
@@ -65,28 +98,24 @@ async fn careers_contenthandler(mut path: PathBuf, cluster: &Cluster, origin: &O
65
98
careers,
66
99
"Careers" ,
67
100
& Path :: new ( "careers" ) ,
68
- & config:: docs_dir ( ) ,
101
+ config:: cms_dir ( ) ,
69
102
)
70
- . await
71
- }
72
- #[ get( "/docs/.gitbook/assets/<path>" , rank = 10 ) ]
73
- pub async fn docs_gitbook_assets ( path : PathBuf ) -> Option < NamedFile > {
74
- let path = PathBuf :: from ( & config:: docs_dir ( ) )
75
- . join ( "docs/.gitbook/assets/" )
76
- . join ( path) ;
77
-
78
- NamedFile :: open ( path) . await . ok ( )
103
+ . await
79
104
}
80
105
81
106
#[ get( "/docs/<path..>" , rank = 5 ) ]
82
- async fn doc_handler ( mut path : PathBuf , cluster : & Cluster , origin : & Origin < ' _ > ) -> Result < ResponseOk , Status > {
107
+ async fn docs_handler (
108
+ mut path : PathBuf ,
109
+ cluster : & Cluster ,
110
+ origin : & Origin < ' _ > ,
111
+ ) -> Result < ResponseOk , Status > {
83
112
info ! ( "path: {:?}" , path) ;
84
113
if origin. path ( ) . ends_with ( "/" ) {
85
114
path = path. join ( "" ) ;
86
115
}
87
116
info ! ( "joined path: {:?}" , path) ;
88
117
let root = PathBuf :: from ( "docs/" ) ;
89
- let index_path = PathBuf :: from ( & config:: docs_dir ( ) )
118
+ let index_path = PathBuf :: from ( & config:: cms_dir ( ) )
90
119
. join ( & root)
91
120
. join ( "SUMMARY.md" ) ;
92
121
let contents = tokio:: fs:: read_to_string ( & index_path) . await . expect (
@@ -107,7 +136,7 @@ async fn doc_handler(mut path: PathBuf, cluster: &Cluster, origin: &Origin<'_>)
107
136
guides,
108
137
"Docs" ,
109
138
& Path :: new ( "docs" ) ,
110
- & config:: docs_dir ( ) ,
139
+ config:: cms_dir ( ) ,
111
140
)
112
141
. await
113
142
}
@@ -172,7 +201,7 @@ async fn blog_handler<'a>(path: PathBuf, cluster: &Cluster) -> Result<ResponseOk
172
201
] ,
173
202
"Blog" ,
174
203
& Path :: new ( "blog" ) ,
175
- & config:: blogs_dir ( ) ,
204
+ config:: blogs_dir ( ) ,
176
205
)
177
206
. await
178
207
}
@@ -183,7 +212,7 @@ async fn render<'a>(
183
212
mut nav_links : Vec < NavLink > ,
184
213
nav_title : & ' a str ,
185
214
folder : & ' a Path ,
186
- content : & ' a str ,
215
+ content : & ' a Path ,
187
216
) -> Result < ResponseOk , Status > {
188
217
let mut path = path
189
218
. to_str ( )
@@ -301,7 +330,14 @@ async fn render<'a>(
301
330
}
302
331
303
332
pub fn routes ( ) -> Vec < Route > {
304
- routes ! [ docs_gitbook_assets, doc_handler, blog_handler, careers_handler, careers_gitbook_assets, search]
333
+ routes ! [
334
+ get_asset_careers,
335
+ get_asset_docs,
336
+ docs_handler,
337
+ blog_handler,
338
+ careers_handler,
339
+ search
340
+ ]
305
341
}
306
342
307
343
#[ cfg( test) ]
0 commit comments