From 11dcd9b6c74c416a5b14277284a291db64ace4a4 Mon Sep 17 00:00:00 2001 From: Oliver Brotchie Date: Sat, 4 Sep 2021 18:45:05 +0100 Subject: [PATCH] CDATA unescape --- src/main.rs | 36 ++++++++++++++++++++++++++++-------- 1 file changed, 28 insertions(+), 8 deletions(-) diff --git a/src/main.rs b/src/main.rs index 09a8ff5..9726cf7 100644 --- a/src/main.rs +++ b/src/main.rs @@ -160,9 +160,9 @@ fn delete(mut blog_file: BlogFile, config: Config) -> Result<(), Box Result<(), Box> { - let path = fs::read_to_string(&file)?; - let mut r = Reader::from_str(&path); +fn remove_xml(path: PathBuf, entry: &Entry) -> Result<(), Box> { + let file = fs::read_to_string(&path)?; + let mut r = Reader::from_str(&file); let mut w = Writer::new(Cursor::new(Vec::new())); let mut buf = Vec::::new(); let mut found = false; @@ -170,6 +170,15 @@ fn remove_xml(file: PathBuf, entry: &Entry) -> Result<(), Box { + w.write( + format!( + "\n", + str::from_utf8(&e.unescaped()?.into_owned())? + ) + .as_bytes(), + )?; + } Ok(Event::Start(ref e)) if (e.name() == b"item" || e.name() == b"li") && e.attributes().any(|a| { @@ -188,7 +197,7 @@ fn remove_xml(file: PathBuf, entry: &Entry) -> Result<(), Box Result<(), Box> { - let path = fs::read_to_string(&file)?; - let mut r = Reader::from_str(&path); + let file = fs::read_to_string(&path)?; + let mut r = Reader::from_str(&file); let mut w = Writer::new(Cursor::new(Vec::new())); let mut buf = Vec::::new(); @@ -296,6 +305,15 @@ fn insert_xml( // Loop over every tag loop { match r.read_event(&mut buf) { + Ok(Event::CData(e)) if found && count <= config.items => { + w.write( + format!( + "\n", + str::from_utf8(&e.unescaped()?.into_owned())? + ) + .as_bytes(), + )?; + } // Remove excess items on the rss feed Ok(Event::Start(e)) if flag == "rss" && e.name() == b"item" => { count += 1; @@ -326,6 +344,8 @@ fn insert_xml( } } Ok(Event::Eof) => break, + + // Remove excess items on the rss feed Ok(_) if found && count > config.items => (), Ok(e) => w.write_event(e)?, Err(e) => panic!( @@ -342,7 +362,7 @@ fn insert_xml( if flag == "template" { PathBuf::from(format!("blog/{}.html", entry.kebab)) } else { - file.to_path_buf() + path.to_path_buf() }, w.into_inner().into_inner(), )?;