pandoc/lib/pandoc/watcher.ex

30 lines
751 B
Elixir

defmodule Pandoc.Watcher do
use GenServer
@ext ".md"
def start_link(args) do
GenServer.start_link(__MODULE__, args)
end
def init([profile | args]) do
{:ok, watcher_pid} = FileSystem.start_link(args)
FileSystem.subscribe(watcher_pid)
{:ok, %{watcher_pid: watcher_pid, profile: profile}}
end
def handle_info({:file_event, watcher_pid, {path, events}}, %{watcher_pid: watcher_pid} = state) do
case {Path.extname(path), :closed in events} do
{@ext, true} -> Pandoc.run(state[:profile], [path])
_ -> nil
end
{:noreply, state}
end
def handle_info({:file_event, watcher_pid, :stop}, %{watcher_pid: watcher_pid} = state) do
# Your own logic when monitor stop
{:noreply, state}
end
end