From bb77e0572a0b2aa47168f78fe7565f1fbc84cf98 Mon Sep 17 00:00:00 2001
From: Catalin Mititiuc
Date: Thu, 12 Dec 2024 10:54:22 -0800
Subject: Handle deleting a document
---
lib/pandoc.ex | 22 +++++++++++-----------
lib/pandoc/watcher.ex | 2 +-
2 files changed, 12 insertions(+), 12 deletions(-)
diff --git a/lib/pandoc.ex b/lib/pandoc.ex
index 9002bb5..f1f07f7 100644
--- a/lib/pandoc.ex
+++ b/lib/pandoc.ex
@@ -37,17 +37,17 @@ defmodule Pandoc do
stderr_to_stdout: true
]
- args =
- List.update_at(args, -1, fn v ->
- Path.join(
- v,
- path
- |> Path.basename()
- |> String.replace_suffix(".md", ".html")
- |> String.slice(11..-1//1)
- )
- end)
+ new_filename =
+ path |> Path.basename() |> String.replace_suffix(".md", ".html") |> String.slice(11..-1//1)
+
+ new_path = args |> List.last() |> Path.join(new_filename)
+ out_path = Path.join(opts[:cd], new_path) |> Path.expand()
- "pandoc" |> System.cmd(args ++ [path], opts) |> elem(1)
+ if File.exists?(path) do
+ args = List.replace_at(args, -1, out_path)
+ "pandoc" |> System.cmd(args ++ [path], opts) |> elem(1)
+ else
+ File.rm(out_path)
+ end
end
end
diff --git a/lib/pandoc/watcher.ex b/lib/pandoc/watcher.ex
index 304bb50..a174e46 100644
--- a/lib/pandoc/watcher.ex
+++ b/lib/pandoc/watcher.ex
@@ -14,7 +14,7 @@ defmodule Pandoc.Watcher do
end
def handle_info({:file_event, watcher_pid, {path, events}}, %{watcher_pid: watcher_pid} = state) do
- case {Path.extname(path), :closed in events} do
+ case {Path.extname(path), :closed in events or :deleted in events} do
{@ext, true} -> Pandoc.run(state[:profile], path)
_ -> nil
end
--
cgit v1.2.3