Browse code

Capture titles, make them slugs

Julio Biason authored on 26/02/2019 07:50:15
Showing 3 changed files

... ...
@@ -3,12 +3,28 @@ name = "RustyXML"
3 3
 version = "0.1.1"
4 4
 source = "registry+https://github.com/rust-lang/crates.io-index"
5 5
 
6
+[[package]]
7
+name = "deunicode"
8
+version = "0.4.3"
9
+source = "registry+https://github.com/rust-lang/crates.io-index"
10
+
6 11
 [[package]]
7 12
 name = "enexparser"
8 13
 version = "0.1.0"
9 14
 dependencies = [
10 15
  "RustyXML 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)",
16
+ "slug 0.1.4 (registry+https://github.com/rust-lang/crates.io-index)",
17
+]
18
+
19
+[[package]]
20
+name = "slug"
21
+version = "0.1.4"
22
+source = "registry+https://github.com/rust-lang/crates.io-index"
23
+dependencies = [
24
+ "deunicode 0.4.3 (registry+https://github.com/rust-lang/crates.io-index)",
11 25
 ]
12 26
 
13 27
 [metadata]
14 28
 "checksum RustyXML 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "9222d58bccd9e6e3b82098a2ec142ad34e5d433de986d46cec03ad3a2b5fd529"
29
+"checksum deunicode 0.4.3 (registry+https://github.com/rust-lang/crates.io-index)" = "850878694b7933ca4c9569d30a34b55031b9b139ee1fc7b94a527c4ef960d690"
30
+"checksum slug 0.1.4 (registry+https://github.com/rust-lang/crates.io-index)" = "b3bc762e6a4b6c6fcaade73e77f9ebc6991b676f88bb2358bddb56560f073373"
... ...
@@ -5,4 +5,5 @@ authors = ["Julio Biason <julio.biason@deliverit.com.br>"]
5 5
 edition = "2018"
6 6
 
7 7
 [dependencies]
8
-RustyXML = "0.1.1"
9 8
\ No newline at end of file
9
+RustyXML = "0.1.1"
10
+slug = "0.1.4"
10 11
\ No newline at end of file
... ...
@@ -2,6 +2,11 @@ use std::env;
2 2
 use xml::{Event, Parser};
3 3
 use std::fs::File;
4 4
 use std::io::prelude::*;
5
+use slug::slugify;
6
+
7
+enum State {
8
+    Title
9
+}
5 10
 
6 11
 fn main() {
7 12
     let args:Vec<_> = env::args().collect();
... ...
@@ -24,13 +29,34 @@ fn main() {
24 29
     let mut parser = Parser::new();
25 30
     parser.feed_str(&contents);
26 31
     
27
-    for event in parser {
28
-        match event.unwrap() {
29
-            Event::ElementStart(tag) => println!("Start: {}", tag.name),
30
-            Event::ElementEnd(tag) => println!("End: {}", tag.name),
31
-            Event::Characters(data) => println!("Data: {}", data),
32
-            Event::CDATA(data) => println!("CDATA: {}", data),
33
-            _ => ()
32
+    parser.fold(None, {|state:Option<State>, element| {
33
+        match element.unwrap() {
34
+            Event::ElementStart(tag) => {
35
+                // println!("Start: {}", tag.name);
36
+                match tag.name.as_ref() {
37
+                    "title" => Some(State::Title),
38
+                    _ => None
39
+                }
40
+            },
41
+            Event::ElementEnd(tag) => {
42
+                // println!("End: {}", tag.name);
43
+                None    // ending a tag always remove the state
44
+            },
45
+            Event::Characters(data) => {
46
+                // println!("Data: {}", data);
47
+                match state {
48
+                    Some(State::Title) => {
49
+                        println!("TITLE: {}", slugify(data));
50
+                        None
51
+                    },
52
+                    _ => state
53
+                }
54
+            },
55
+            Event::CDATA(data) => {
56
+                // println!("CDATA: {}", data);
57
+                state
58
+            },
59
+            _ => state
34 60
         }
35
-    }
61
+    }});
36 62
 }