Browse Source

Initial commit. Can parse posts.

master
Dylan Baker 5 years ago
commit
0243511f8f
4 changed files with 165 additions and 0 deletions
  1. 2
    0
      .gitignore
  2. 96
    0
      Cargo.lock
  3. 8
    0
      Cargo.toml
  4. 59
    0
      src/main.rs

+ 2
- 0
.gitignore View File

@@ -0,0 +1,2 @@
1
+/target
2
+**/*.rs.bk

+ 96
- 0
Cargo.lock View File

@@ -0,0 +1,96 @@
1
+[[package]]
2
+name = "aho-corasick"
3
+version = "0.6.9"
4
+source = "registry+https://github.com/rust-lang/crates.io-index"
5
+dependencies = [
6
+ "memchr 2.1.1 (registry+https://github.com/rust-lang/crates.io-index)",
7
+]
8
+
9
+[[package]]
10
+name = "cfg-if"
11
+version = "0.1.6"
12
+source = "registry+https://github.com/rust-lang/crates.io-index"
13
+
14
+[[package]]
15
+name = "lazy_static"
16
+version = "1.2.0"
17
+source = "registry+https://github.com/rust-lang/crates.io-index"
18
+
19
+[[package]]
20
+name = "libc"
21
+version = "0.2.43"
22
+source = "registry+https://github.com/rust-lang/crates.io-index"
23
+
24
+[[package]]
25
+name = "memchr"
26
+version = "2.1.1"
27
+source = "registry+https://github.com/rust-lang/crates.io-index"
28
+dependencies = [
29
+ "cfg-if 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)",
30
+ "libc 0.2.43 (registry+https://github.com/rust-lang/crates.io-index)",
31
+ "version_check 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)",
32
+]
33
+
34
+[[package]]
35
+name = "regex"
36
+version = "1.0.6"
37
+source = "registry+https://github.com/rust-lang/crates.io-index"
38
+dependencies = [
39
+ "aho-corasick 0.6.9 (registry+https://github.com/rust-lang/crates.io-index)",
40
+ "memchr 2.1.1 (registry+https://github.com/rust-lang/crates.io-index)",
41
+ "regex-syntax 0.6.3 (registry+https://github.com/rust-lang/crates.io-index)",
42
+ "thread_local 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)",
43
+ "utf8-ranges 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)",
44
+]
45
+
46
+[[package]]
47
+name = "regex-syntax"
48
+version = "0.6.3"
49
+source = "registry+https://github.com/rust-lang/crates.io-index"
50
+dependencies = [
51
+ "ucd-util 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)",
52
+]
53
+
54
+[[package]]
55
+name = "thread_local"
56
+version = "0.3.6"
57
+source = "registry+https://github.com/rust-lang/crates.io-index"
58
+dependencies = [
59
+ "lazy_static 1.2.0 (registry+https://github.com/rust-lang/crates.io-index)",
60
+]
61
+
62
+[[package]]
63
+name = "tlon"
64
+version = "0.1.0"
65
+dependencies = [
66
+ "lazy_static 1.2.0 (registry+https://github.com/rust-lang/crates.io-index)",
67
+ "regex 1.0.6 (registry+https://github.com/rust-lang/crates.io-index)",
68
+]
69
+
70
+[[package]]
71
+name = "ucd-util"
72
+version = "0.1.3"
73
+source = "registry+https://github.com/rust-lang/crates.io-index"
74
+
75
+[[package]]
76
+name = "utf8-ranges"
77
+version = "1.0.2"
78
+source = "registry+https://github.com/rust-lang/crates.io-index"
79
+
80
+[[package]]
81
+name = "version_check"
82
+version = "0.1.5"
83
+source = "registry+https://github.com/rust-lang/crates.io-index"
84
+
85
+[metadata]
86
+"checksum aho-corasick 0.6.9 (registry+https://github.com/rust-lang/crates.io-index)" = "1e9a933f4e58658d7b12defcf96dc5c720f20832deebe3e0a19efd3b6aaeeb9e"
87
+"checksum cfg-if 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)" = "082bb9b28e00d3c9d39cc03e64ce4cea0f1bb9b3fde493f0cbc008472d22bdf4"
88
+"checksum lazy_static 1.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "a374c89b9db55895453a74c1e38861d9deec0b01b405a82516e9d5de4820dea1"
89
+"checksum libc 0.2.43 (registry+https://github.com/rust-lang/crates.io-index)" = "76e3a3ef172f1a0b9a9ff0dd1491ae5e6c948b94479a3021819ba7d860c8645d"
90
+"checksum memchr 2.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "0a3eb002f0535929f1199681417029ebea04aadc0c7a4224b46be99c7f5d6a16"
91
+"checksum regex 1.0.6 (registry+https://github.com/rust-lang/crates.io-index)" = "ee84f70c8c08744ea9641a731c7fadb475bf2ecc52d7f627feb833e0b3990467"
92
+"checksum regex-syntax 0.6.3 (registry+https://github.com/rust-lang/crates.io-index)" = "fbc557aac2b708fe84121caf261346cc2eed71978024337e42eb46b8a252ac6e"
93
+"checksum thread_local 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)" = "c6b53e329000edc2b34dbe8545fd20e55a333362d0a321909685a19bd28c3f1b"
94
+"checksum ucd-util 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)" = "535c204ee4d8434478593480b8f86ab45ec9aae0e83c568ca81abf0fd0e88f86"
95
+"checksum utf8-ranges 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)" = "796f7e48bef87609f7ade7e06495a87d5cd06c7866e6a5cbfceffc558a243737"
96
+"checksum version_check 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)" = "914b1a6776c4c929a602fafd8bc742e06365d4bcbe48c30f9cca5824f70dc9dd"

+ 8
- 0
Cargo.toml View File

@@ -0,0 +1,8 @@
1
+[package]
2
+name = "tlon"
3
+version = "0.1.0"
4
+authors = ["Dylan Baker"]
5
+
6
+[dependencies]
7
+lazy_static = "1.2.0"
8
+regex = "1"

+ 59
- 0
src/main.rs View File

@@ -0,0 +1,59 @@
1
+#[macro_use]
2
+extern crate lazy_static;
3
+extern crate regex;
4
+
5
+use std::env;
6
+use std::fs;
7
+use std::path;
8
+
9
+use regex::Regex;
10
+
11
+#[derive(Debug)]
12
+struct Post {
13
+    title: String,
14
+    body: String,
15
+    slug: String,
16
+}
17
+
18
+fn parse_post(path: path::PathBuf) -> Post {
19
+    let contents = fs::read_to_string(&path).expect("Couldn't read post file");
20
+
21
+    lazy_static! {
22
+        static ref re: Regex = Regex::new(r"^# (?P<title>.*)\n\n(?P<body>.*)").unwrap();
23
+        static ref slug_re: Regex = Regex::new(r"(?P<slug>\S+).md").unwrap();
24
+    }
25
+
26
+    let title = &re.captures(&contents).expect("Couldn't parse title")["title"];
27
+
28
+    let body = &re.captures(&contents).expect("Couldn't parse body")["body"];
29
+
30
+    let filename = &path.file_name().unwrap().to_str().unwrap();
31
+    let slug = &slug_re.captures(filename).expect("Couldn't parse slug")["slug"];
32
+    Post {
33
+        title: String::from(title),
34
+        body: String::from(body),
35
+        slug: String::from(slug),
36
+    }
37
+}
38
+
39
+fn read_posts_dir() -> fs::ReadDir {
40
+    let cwd = env::current_dir()
41
+        .expect("Couldn't read current directory")
42
+        .join("posts");
43
+    match fs::read_dir(cwd) {
44
+        Ok(posts) => posts,
45
+        Err(err) => panic!(err),
46
+    }
47
+}
48
+
49
+fn main() {
50
+    let post_paths = read_posts_dir();
51
+    for path in post_paths {
52
+        match path {
53
+            Ok(p) => {
54
+                let post = parse_post(p.path());
55
+            },
56
+            Err(err) => panic!(err),
57
+        }
58
+    }
59
+}

Loading…
Cancel
Save