Преглед изворни кода

Do formatting at insert time

master
Dylan Baker пре 5 година
родитељ
комит
fee9ecf685

+ 1
- 0
migrations/2019-05-01-204240_add_formatted_body_to_gists/down.sql Прегледај датотеку

1
+ALTER TABLE gists DROP COLUMN formatted_body;

+ 1
- 0
migrations/2019-05-01-204240_add_formatted_body_to_gists/up.sql Прегледај датотеку

1
+ALTER TABLE gists ADD COLUMN formatted_body TEXT NOT NULL;

+ 28
- 1
src/gists/mod.rs Прегледај датотеку

1
 use chrono::NaiveDateTime;
1
 use chrono::NaiveDateTime;
2
 use diesel::pg::PgConnection as PGC;
2
 use diesel::pg::PgConnection as PGC;
3
 use diesel::prelude::*;
3
 use diesel::prelude::*;
4
+use syntect::highlighting::ThemeSet;
5
+use syntect::html::highlighted_html_for_string;
6
+use syntect::parsing::SyntaxSet;
4
 
7
 
5
 use crate::schema::gists;
8
 use crate::schema::gists;
6
 
9
 
11
     pub body: String,
14
     pub body: String,
12
     pub created_at: Option<NaiveDateTime>,
15
     pub created_at: Option<NaiveDateTime>,
13
     pub filetype: Option<String>,
16
     pub filetype: Option<String>,
17
+    pub formatted_body: String,
14
 }
18
 }
15
 
19
 
16
 #[derive(Insertable, AsChangeset, Serialize, Deserialize)]
20
 #[derive(Insertable, AsChangeset, Serialize, Deserialize)]
19
     pub title: String,
23
     pub title: String,
20
     pub body: String,
24
     pub body: String,
21
     pub filetype: Option<String>,
25
     pub filetype: Option<String>,
26
+    pub formatted_body: String,
27
+}
28
+
29
+fn format_gist(filetype: Option<String>, body: String) -> String {
30
+    filetype.map_or(
31
+        format!("<pre class='plaintext'>{}</pre>", body),
32
+        |filetype| {
33
+            let ps = SyntaxSet::load_defaults_newlines();
34
+            let ts = ThemeSet::load_defaults();
35
+            ps.find_syntax_by_extension(&filetype).map_or(
36
+                format!("<pre class='plaintext'>{}</pre>", body),
37
+                |syntax| {
38
+                    highlighted_html_for_string(&body, &ps, syntax, &ts.themes["Solarized (light)"])
39
+                },
40
+            )
41
+        },
42
+    )
22
 }
43
 }
23
 
44
 
24
 pub fn all(connection: &PGC) -> QueryResult<Vec<Gist>> {
45
 pub fn all(connection: &PGC) -> QueryResult<Vec<Gist>> {
30
 }
51
 }
31
 
52
 
32
 pub fn insert(gist: InsertableGist, connection: &PGC) -> QueryResult<Gist> {
53
 pub fn insert(gist: InsertableGist, connection: &PGC) -> QueryResult<Gist> {
54
+    let formatted_gist = InsertableGist {
55
+        filetype: gist.filetype.clone(),
56
+        title: gist.title,
57
+        body: gist.body.clone(),
58
+        formatted_body: format_gist(gist.filetype, gist.body),
59
+    };
33
     diesel::insert_into(gists::table)
60
     diesel::insert_into(gists::table)
34
-        .values(gist)
61
+        .values(formatted_gist)
35
         .get_result(connection)
62
         .get_result(connection)
36
 }
63
 }

+ 1
- 20
src/routes.rs Прегледај датотеку

4
 use rocket_contrib::json::Json;
4
 use rocket_contrib::json::Json;
5
 use rocket_contrib::templates::Template;
5
 use rocket_contrib::templates::Template;
6
 use std::collections::HashMap;
6
 use std::collections::HashMap;
7
-use syntect::highlighting::ThemeSet;
8
-use syntect::html::highlighted_html_for_string;
9
-use syntect::parsing::SyntaxSet;
10
 
7
 
11
 use crate::connection::DbConn;
8
 use crate::connection::DbConn;
12
 use crate::gists;
9
 use crate::gists;
19
     }
16
     }
20
 }
17
 }
21
 
18
 
22
-fn format_gist(filetype: Option<String>, body: String) -> String {
23
-    filetype.map_or(
24
-        format!("<pre class='plaintext'>{}</pre>", body),
25
-        |filetype| {
26
-            let ps = SyntaxSet::load_defaults_newlines();
27
-            let ts = ThemeSet::load_defaults();
28
-            ps.find_syntax_by_extension(&filetype).map_or(
29
-                format!("<pre class='plaintext'>{}</pre>", body),
30
-                |syntax| {
31
-                    highlighted_html_for_string(&body, &ps, syntax, &ts.themes["Solarized (light)"])
32
-                },
33
-            )
34
-        },
35
-    )
36
-}
37
-
38
 #[get("/")]
19
 #[get("/")]
39
 pub fn index(connection: DbConn) -> Result<Json<Vec<Gist>>, Status> {
20
 pub fn index(connection: DbConn) -> Result<Json<Vec<Gist>>, Status> {
40
     gists::all(&connection)
21
     gists::all(&connection)
54
             String::from("404 - Gist not found"),
35
             String::from("404 - Gist not found"),
55
             format!("<h3'>{}</h3>", "Gist not found"),
36
             format!("<h3'>{}</h3>", "Gist not found"),
56
         ),
37
         ),
57
-        |gist| (gist.title, format_gist(gist.filetype, gist.body)),
38
+        |gist| (gist.title, gist.formatted_body),
58
     );
39
     );
59
 
40
 
60
     let mut context: HashMap<&str, String> = HashMap::new();
41
     let mut context: HashMap<&str, String> = HashMap::new();

+ 1
- 0
src/schema.rs Прегледај датотеку

5
         body -> Text,
5
         body -> Text,
6
         created_at -> Nullable<Timestamp>,
6
         created_at -> Nullable<Timestamp>,
7
         filetype -> Nullable<Varchar>,
7
         filetype -> Nullable<Varchar>,
8
+        formatted_body -> Text,
8
     }
9
     }
9
 }
10
 }

Loading…
Откажи
Сачувај