Browse Source

Capture titles, make them slugs

master
Julio Biason 6 years ago
parent
commit
fea1af6988
  1. 16
      Cargo.lock
  2. 3
      Cargo.toml
  3. 42
      src/main.rs

16
Cargo.lock generated

@ -3,12 +3,28 @@ name = "RustyXML"
version = "0.1.1" version = "0.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index" 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]] [[package]]
name = "enexparser" name = "enexparser"
version = "0.1.0" version = "0.1.0"
dependencies = [ dependencies = [
"RustyXML 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)", "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] [metadata]
"checksum RustyXML 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "9222d58bccd9e6e3b82098a2ec142ad34e5d433de986d46cec03ad3a2b5fd529" "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"

3
Cargo.toml

@ -5,4 +5,5 @@ authors = ["Julio Biason <julio.biason@deliverit.com.br>"]
edition = "2018" edition = "2018"
[dependencies] [dependencies]
RustyXML = "0.1.1" RustyXML = "0.1.1"
slug = "0.1.4"

42
src/main.rs

@ -2,6 +2,11 @@ use std::env;
use xml::{Event, Parser}; use xml::{Event, Parser};
use std::fs::File; use std::fs::File;
use std::io::prelude::*; use std::io::prelude::*;
use slug::slugify;
enum State {
Title
}
fn main() { fn main() {
let args:Vec<_> = env::args().collect(); let args:Vec<_> = env::args().collect();
@ -24,13 +29,34 @@ fn main() {
let mut parser = Parser::new(); let mut parser = Parser::new();
parser.feed_str(&contents); parser.feed_str(&contents);
for event in parser { parser.fold(None, {|state:Option<State>, element| {
match event.unwrap() { match element.unwrap() {
Event::ElementStart(tag) => println!("Start: {}", tag.name), Event::ElementStart(tag) => {
Event::ElementEnd(tag) => println!("End: {}", tag.name), // println!("Start: {}", tag.name);
Event::Characters(data) => println!("Data: {}", data), match tag.name.as_ref() {
Event::CDATA(data) => println!("CDATA: {}", data), "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
} }
} }});
} }

Loading…
Cancel
Save