Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CDATA unescape #3

Merged
merged 1 commit into from
Sep 4, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
36 changes: 28 additions & 8 deletions src/main.rs
Original file line number Diff line number Diff line change
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