소스 검색

Restructure entry parsing, should help with speed

master
Dylan Baker 5 년 전
부모
커밋
1fee1972ea
2개의 변경된 파일28개의 추가작업 그리고 28개의 파일을 삭제
  1. 25
    25
      src/entry.rs
  2. 3
    3
      src/lib.rs

+ 25
- 25
src/entry.rs 파일 보기

50
     }
50
     }
51
 }
51
 }
52
 
52
 
53
-pub fn parse_entry(contents: &str, path: &path::Path) -> Entry {
53
+pub fn parse_entry(kind: EntryKind, contents: &str, path: &path::Path) -> Entry {
54
     lazy_static! {
54
     lazy_static! {
55
         static ref re_with_date: Regex =
55
         static ref re_with_date: Regex =
56
             Regex::new(r"^# (?P<title>.*) \| (?P<date>\d{4}-\d{2}-\d{2})\n\n(?s)(?P<body>.*)")
56
             Regex::new(r"^# (?P<title>.*) \| (?P<date>\d{4}-\d{2}-\d{2})\n\n(?s)(?P<body>.*)")
61
     }
61
     }
62
 
62
 
63
     let filename = &path.file_name().unwrap().to_str().unwrap();
63
     let filename = &path.file_name().unwrap().to_str().unwrap();
64
-    let slug = &slug_re
64
+    let slug = slug_re
65
         .captures(filename)
65
         .captures(filename)
66
-        .expect("Couldn't parse slug from filename")["slug"];
66
+        .expect("Couldn't parse slug from filename")["slug"]
67
+        .to_string();
67
 
68
 
68
-    if let Some(date_string) = &re_with_date.captures(&contents) {
69
-        let title = &re_with_date
69
+    if kind == EntryKind::Post {
70
+        let captures = &re_with_date
70
             .captures(&contents)
71
             .captures(&contents)
71
-            .expect("Couldn't parse title")["title"];
72
-        let body = &re_with_date
73
-            .captures(&contents)
74
-            .expect("Couldn't parse title")["body"];
72
+            .expect("Couldn't parse post");
73
+        let title = captures["title"].to_string();
74
+        let body = captures["body"].to_string();
75
         let date = Some(
75
         let date = Some(
76
-            NaiveDate::parse_from_str(&date_string["date"], "%Y-%m-%d")
77
-                .expect("Couldn't parse date"),
76
+            NaiveDate::parse_from_str(&captures["date"], "%Y-%m-%d").expect("Couldn't parse date"),
78
         );
77
         );
79
         Entry {
78
         Entry {
80
-            kind: EntryKind::Post,
81
-            title: String::from(title),
82
-            body: String::from(body),
83
-            slug: String::from(slug),
84
-            date: date,
79
+            kind,
80
+            title,
81
+            body,
82
+            slug,
83
+            date,
85
         }
84
         }
86
     } else {
85
     } else {
87
-        let title = &re_without_date
88
-            .captures(&contents)
89
-            .expect("Couldn't parse title")["title"];
90
-        let body = &re_without_date
86
+        let captures = &re_without_date
91
             .captures(&contents)
87
             .captures(&contents)
92
-            .expect("Couldn't parse title")["body"];
88
+            .expect("Couldn't parse page");
89
+        let title = captures["title"].to_string();
90
+        let body = captures["body"].to_string();
93
         Entry {
91
         Entry {
94
-            kind: EntryKind::Page,
95
-            title: String::from(title),
96
-            body: String::from(body),
97
-            slug: String::from(slug),
92
+            kind,
93
+            title,
94
+            body,
95
+            slug,
98
             date: None,
96
             date: None,
99
         }
97
         }
100
     }
98
     }
358
     #[test]
356
     #[test]
359
     fn test_parse_post_entry() {
357
     fn test_parse_post_entry() {
360
         let post = parse_entry(
358
         let post = parse_entry(
359
+            EntryKind::Post,
361
             "# Test Title | 2000-01-01\n\nThis is the body",
360
             "# Test Title | 2000-01-01\n\nThis is the body",
362
             path::PathBuf::from("posts/one.md").as_path(),
361
             path::PathBuf::from("posts/one.md").as_path(),
363
         );
362
         );
372
     #[test]
371
     #[test]
373
     fn test_parse_page_entry() {
372
     fn test_parse_page_entry() {
374
         let post = parse_entry(
373
         let post = parse_entry(
374
+            EntryKind::Page,
375
             "# Test Title\n\nThis is the body",
375
             "# Test Title\n\nThis is the body",
376
             path::PathBuf::from("pages/one.md").as_path(),
376
             path::PathBuf::from("pages/one.md").as_path(),
377
         );
377
         );

+ 3
- 3
src/lib.rs 파일 보기

17
 use toml::Value;
17
 use toml::Value;
18
 
18
 
19
 use config::Config;
19
 use config::Config;
20
-use entry::{parse_entry, read_entry_dir, write_entry, write_entry_listing, Entry};
20
+use entry::{parse_entry, read_entry_dir, write_entry, write_entry_listing, Entry, EntryKind};
21
 
21
 
22
 mod config;
22
 mod config;
23
 mod entry;
23
 mod entry;
73
         .map(|entry| {
73
         .map(|entry| {
74
             let path = entry.path();
74
             let path = entry.path();
75
             let contents = fs::read_to_string(&path).expect("Couldn't read post file");
75
             let contents = fs::read_to_string(&path).expect("Couldn't read post file");
76
-            parse_entry(&contents, &path)
76
+            parse_entry(EntryKind::Post, &contents, &path)
77
         })
77
         })
78
         .collect::<Vec<Entry>>();
78
         .collect::<Vec<Entry>>();
79
 
79
 
86
     for entry in page_paths.into_iter() {
86
     for entry in page_paths.into_iter() {
87
         let path = entry.path();
87
         let path = entry.path();
88
         let contents = fs::read_to_string(&path).expect("Couldn't read page file");
88
         let contents = fs::read_to_string(&path).expect("Couldn't read page file");
89
-        let page = parse_entry(&contents, &path);
89
+        let page = parse_entry(EntryKind::Page, &contents, &path);
90
         write_entry(&cwd, &layout_template, &page_template, &page, &config);
90
         write_entry(&cwd, &layout_template, &page_template, &page, &config);
91
     }
91
     }
92
 
92
 

Loading…
취소
저장