From 7f7cf3a4363c904923e0bbd0b18e800caa70b3a3 Mon Sep 17 00:00:00 2001 From: Benjamin Bouvier Date: Mon, 1 Jul 2019 01:06:19 +0200 Subject: [PATCH] Sync removal too; --- src/main.rs | 37 ++++++++++++++++++++++++++++--------- 1 file changed, 28 insertions(+), 9 deletions(-) diff --git a/src/main.rs b/src/main.rs index 94a701d..2cd2d89 100644 --- a/src/main.rs +++ b/src/main.rs @@ -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 + run_hooks(&mut context, &files)?; } Remove(path) => { + if let Some(note) = files.get(path) { + context.remove_public(note); + } files.remove(path); - true } - _ => false, + _ => {} }; println!("{:?}", evt); - if should_run_hooks { - run_hooks(&mut context, &files)?; - } } Err(e) => println!("watch error: {:?}", e), }