Browse Source

Do formatting at insert time

master
Dylan Baker 5 years ago
parent
commit
fee9ecf685

+ 1
- 0
migrations/2019-05-01-204240_add_formatted_body_to_gists/down.sql View File

@@ -0,0 +1 @@
1
+ALTER TABLE gists DROP COLUMN formatted_body;

+ 1
- 0
migrations/2019-05-01-204240_add_formatted_body_to_gists/up.sql View File

@@ -0,0 +1 @@
1
+ALTER TABLE gists ADD COLUMN formatted_body TEXT NOT NULL;

+ 28
- 1
src/gists/mod.rs View File

@@ -1,6 +1,9 @@
1 1
 use chrono::NaiveDateTime;
2 2
 use diesel::pg::PgConnection as PGC;
3 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 8
 use crate::schema::gists;
6 9
 
@@ -11,6 +14,7 @@ pub struct Gist {
11 14
     pub body: String,
12 15
     pub created_at: Option<NaiveDateTime>,
13 16
     pub filetype: Option<String>,
17
+    pub formatted_body: String,
14 18
 }
15 19
 
16 20
 #[derive(Insertable, AsChangeset, Serialize, Deserialize)]
@@ -19,6 +23,23 @@ pub struct InsertableGist {
19 23
     pub title: String,
20 24
     pub body: String,
21 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 45
 pub fn all(connection: &PGC) -> QueryResult<Vec<Gist>> {
@@ -30,7 +51,13 @@ pub fn get(connection: &PGC, id: i32) -> QueryResult<Gist> {
30 51
 }
31 52
 
32 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 60
     diesel::insert_into(gists::table)
34
-        .values(gist)
61
+        .values(formatted_gist)
35 62
         .get_result(connection)
36 63
 }

+ 1
- 20
src/routes.rs View File

@@ -4,9 +4,6 @@ use rocket::response::status;
4 4
 use rocket_contrib::json::Json;
5 5
 use rocket_contrib::templates::Template;
6 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 8
 use crate::connection::DbConn;
12 9
 use crate::gists;
@@ -19,22 +16,6 @@ fn error_response(error: Error) -> Status {
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 19
 #[get("/")]
39 20
 pub fn index(connection: DbConn) -> Result<Json<Vec<Gist>>, Status> {
40 21
     gists::all(&connection)
@@ -54,7 +35,7 @@ pub fn show_gist(id: i32, connection: DbConn) -> Template {
54 35
             String::from("404 - Gist not found"),
55 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 41
     let mut context: HashMap<&str, String> = HashMap::new();

+ 1
- 0
src/schema.rs View File

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

Loading…
Cancel
Save