Sync removal too;

This commit is contained in:
Benjamin Bouvier 2019-07-01 01:06:19 +02:00
parent c92bdb8e17
commit 7f7cf3a436

View File

@ -72,6 +72,11 @@ impl Context {
self.tags = Some(tags);
}
fn sanitize_title(title: &str) -> String {
// TODO do a better sanitize of the title.
title.replace(" ", "_")
}
fn publish_public(&self, notes: &NotesMap) -> Result<(), io::Error> {
let public_tag_id = if let Some(id) = &self.public_tag_id {
id
@ -117,8 +122,7 @@ impl Context {
+ &comrak::markdown_to_html(&md_content, &options)
+ TEMPLATE_FOOTER;
// TODO do a better sanitize of the title.
let filename = title.replace(" ", "_");
let filename = Context::sanitize_title(title);
let path = self.config.path_out.clone() + &format!("/{}.html", filename);
println!("Publishing {}", path);
@ -132,6 +136,22 @@ impl Context {
Ok(())
}
fn remove_public(&mut self, note: &NoteMap) {
// TODO this will only remove the file if the note has been removed, not if the public tag
// has been removed. Handle this too.
let title = if let Some(content) = note.get("markdown_content") {
content.split("\n").next().unwrap()
} else {
return;
};
let filename = Context::sanitize_title(&title);
let path = self.config.path_out.clone() + &format!("/{}.html", filename);
// Missing files or errors on deletion are fine.
let _ = fs::remove_file(&path);
}
}
fn collect_tags(notes: &NotesMap) -> TagMap {
@ -245,21 +265,20 @@ fn main() -> Result<(), io::Error> {
match rx.recv() {
Ok(evt) => {
use DebouncedEvent::*;
let should_run_hooks = match &evt {
match &evt {
Create(path) | Write(path) => {
files.insert(path.clone(), read_file(path)?);
true
}
Remove(path) => {
files.remove(path);
true
}
_ => false,
};
println!("{:?}", evt);
if should_run_hooks {
run_hooks(&mut context, &files)?;
}
Remove(path) => {
if let Some(note) = files.get(path) {
context.remove_public(note);
}
files.remove(path);
}
_ => {}
};
println!("{:?}", evt);
}
Err(e) => println!("watch error: {:?}", e),
}