From fea1af69882a9f6111c12c9b88640a5eb93ecce0 Mon Sep 17 00:00:00 2001 From: Julio Biason Date: Tue, 26 Feb 2019 12:50:15 -0300 Subject: [PATCH] Capture titles, make them slugs --- Cargo.lock | 16 ++++++++++++++++ Cargo.toml | 3 ++- src/main.rs | 42 ++++++++++++++++++++++++++++++++++-------- 3 files changed, 52 insertions(+), 9 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 69a778b..977b21f 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3,12 +3,28 @@ name = "RustyXML" version = "0.1.1" source = "registry+https://github.com/rust-lang/crates.io-index" +[[package]] +name = "deunicode" +version = "0.4.3" +source = "registry+https://github.com/rust-lang/crates.io-index" + [[package]] name = "enexparser" version = "0.1.0" dependencies = [ "RustyXML 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)", + "slug 0.1.4 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "slug" +version = "0.1.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "deunicode 0.4.3 (registry+https://github.com/rust-lang/crates.io-index)", ] [metadata] "checksum RustyXML 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "9222d58bccd9e6e3b82098a2ec142ad34e5d433de986d46cec03ad3a2b5fd529" +"checksum deunicode 0.4.3 (registry+https://github.com/rust-lang/crates.io-index)" = "850878694b7933ca4c9569d30a34b55031b9b139ee1fc7b94a527c4ef960d690" +"checksum slug 0.1.4 (registry+https://github.com/rust-lang/crates.io-index)" = "b3bc762e6a4b6c6fcaade73e77f9ebc6991b676f88bb2358bddb56560f073373" diff --git a/Cargo.toml b/Cargo.toml index 9b00de0..d778c83 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -5,4 +5,5 @@ authors = ["Julio Biason "] edition = "2018" [dependencies] -RustyXML = "0.1.1" \ No newline at end of file +RustyXML = "0.1.1" +slug = "0.1.4" \ No newline at end of file diff --git a/src/main.rs b/src/main.rs index 3675285..8b6329d 100644 --- a/src/main.rs +++ b/src/main.rs @@ -2,6 +2,11 @@ use std::env; use xml::{Event, Parser}; use std::fs::File; use std::io::prelude::*; +use slug::slugify; + +enum State { + Title +} fn main() { let args:Vec<_> = env::args().collect(); @@ -24,13 +29,34 @@ fn main() { let mut parser = Parser::new(); parser.feed_str(&contents); - for event in parser { - match event.unwrap() { - Event::ElementStart(tag) => println!("Start: {}", tag.name), - Event::ElementEnd(tag) => println!("End: {}", tag.name), - Event::Characters(data) => println!("Data: {}", data), - Event::CDATA(data) => println!("CDATA: {}", data), - _ => () + parser.fold(None, {|state:Option, element| { + match element.unwrap() { + Event::ElementStart(tag) => { + // println!("Start: {}", tag.name); + match tag.name.as_ref() { + "title" => Some(State::Title), + _ => None + } + }, + Event::ElementEnd(tag) => { + // println!("End: {}", tag.name); + None // ending a tag always remove the state + }, + Event::Characters(data) => { + // println!("Data: {}", data); + match state { + Some(State::Title) => { + println!("TITLE: {}", slugify(data)); + None + }, + _ => state + } + }, + Event::CDATA(data) => { + // println!("CDATA: {}", data); + state + }, + _ => state } - } + }}); }