Skip to content

Commit

Permalink
Merge pull request #3 from OliverBrotchie/#1-CDATA
Browse files Browse the repository at this point in the history
CDATA unescape
  • Loading branch information
OliverBrotchie committed Sep 4, 2021
2 parents fe93d99 + 11dcd9b commit e3d5de9
Showing 1 changed file with 28 additions and 8 deletions.
36 changes: 28 additions & 8 deletions src/main.rs
Expand Up @@ -160,16 +160,25 @@ fn delete(mut blog_file: BlogFile, config: Config) -> Result<(), Box<dyn std::er
Ok(())
}

fn remove_xml(file: PathBuf, entry: &Entry) -> Result<(), Box<dyn std::error::Error>> {
let path = fs::read_to_string(&file)?;
let mut r = Reader::from_str(&path);
fn remove_xml(path: PathBuf, entry: &Entry) -> Result<(), Box<dyn std::error::Error>> {
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::<u8>::new();
let mut found = false;

// Loop over the xml tags
loop {
match r.read_event(&mut buf) {
Ok(Event::CData(e)) if !found => {
w.write(
format!(
"<![CDATA[{}]]>\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| {
Expand All @@ -188,7 +197,7 @@ fn remove_xml(file: PathBuf, entry: &Entry) -> Result<(), Box<dyn std::error::Er
}
buf.clear();
}
fs::write(file, w.into_inner().into_inner())?;
fs::write(path, w.into_inner().into_inner())?;
Ok(())
}

Expand Down Expand Up @@ -235,14 +244,14 @@ fn publish_draft(
}

fn insert_xml(
file: &Path,
path: &Path,
config: &Config,
entry: &Entry,
html: &str,
flag: &str,
) -> Result<(), Box<dyn std::error::Error>> {
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::<u8>::new();

Expand Down Expand Up @@ -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!(
"<![CDATA[{}]]>\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;
Expand Down Expand Up @@ -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!(
Expand All @@ -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(),
)?;
Expand Down

0 comments on commit e3d5de9

Please sign in to comment.