Web Dev Solutions

Catalin Mititiuc

aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--lib/mix/tasks/pandoc.ex16
-rw-r--r--lib/pandoc.ex59
-rw-r--r--lib/pandoc/application.ex4
-rw-r--r--lib/pandoc/watcher.ex33
4 files changed, 73 insertions, 39 deletions
diff --git a/lib/mix/tasks/pandoc.ex b/lib/mix/tasks/pandoc.ex
new file mode 100644
index 0000000..fe93208
--- /dev/null
+++ b/lib/mix/tasks/pandoc.ex
@@ -0,0 +1,16 @@
+defmodule Mix.Tasks.Pandoc do
+ use Mix.Task
+
+ @out_dir "priv/static"
+
+ @impl true
+ def run([profile | _args]) do
+ IO.puts "profile: #{profile}"
+
+ "priv/posts"
+ |> File.ls!()
+ |> Enum.each(fn path ->
+ Pandoc.install_and_run(Path.join(["priv", "posts", path]))
+ end)
+ end
+end
diff --git a/lib/pandoc.ex b/lib/pandoc.ex
index bf01873..336aeec 100644
--- a/lib/pandoc.ex
+++ b/lib/pandoc.ex
@@ -2,50 +2,36 @@ defmodule Pandoc do
@moduledoc """
Documentation for `Pandoc`.
"""
- use GenServer
- def start_link(args) do
- GenServer.start_link(__MODULE__, args)
- end
-
- def init([profile | args]) do
- IO.puts "pandoc filesystem watcher init args #{inspect(args)}"
- # inspect(Application.get_all_env(:pandoc)) |> IO.puts
- inspect(Application.get_env(:pandoc, profile)) |> IO.puts
- {:ok, watcher_pid} = FileSystem.start_link(args)
- FileSystem.subscribe(watcher_pid)
- {:ok, %{watcher_pid: watcher_pid}}
- end
-
- def handle_info({:file_event, watcher_pid, {path, events}}, %{watcher_pid: watcher_pid} = state) do
- case {Path.extname(path), :closed in events} do
- {".md", true} ->
- System.cmd("pandoc", [
- "--mathjax",
- path,
- "-o",
- Path.join("priv/static/posts", Path.basename(path) |> String.replace_suffix(".md", ".html") |> String.slice(11..-1//1))
- ])
- _ -> 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}
+ def install_and_run(path) do
+ System.cmd("pandoc", [
+ "--mathjax",
+ path,
+ "-o",
+ Path.join(
+ "priv/static/posts",
+ Path.basename(path)
+ |> String.replace_suffix(".md", ".html")
+ |> String.slice(11..-1//1)
+ )
+ ])
end
- def run(profile) do
+ def install_and_run(profile, ["--watch"]) do
# Application.get_env(:pandoc, a1) |> inspect(pretty: true) |> IO.puts
- Application.get_all_env(profile) |> inspect(pretty: true) |> IO.puts
+ IO.puts(
+ "Pandoc watcher starting, env: #{Application.get_env(:pandoc, profile) |> inspect(pretty: true)}"
+ )
+
+ # Application.get_env(:pandoc, profile) |> inspect(pretty: true) |> IO.puts()
ref =
__MODULE__.Supervisor
|> Supervisor.start_child(
- Supervisor.child_spec({Pandoc, [profile, dirs: ["priv/posts"]]}, restart: :transient, id:
- __MODULE__.Watcher)
+ Supervisor.child_spec({Pandoc.Watcher, [profile, dirs: ["priv/posts"]]},
+ restart: :transient,
+ id: __MODULE__.Watcher
+ )
)
|> case do
{:ok, pid} -> pid
@@ -58,4 +44,3 @@ defmodule Pandoc do
end
end
end
-
diff --git a/lib/pandoc/application.ex b/lib/pandoc/application.ex
index 162341a..fd1575f 100644
--- a/lib/pandoc/application.ex
+++ b/lib/pandoc/application.ex
@@ -7,8 +7,8 @@ defmodule Pandoc.Application do
@impl true
def start(type, args) do
- IO.puts "*********************************************"
- IO.puts "#{inspect(type)} #{inspect(args)}"
+ IO.puts("*********************************************")
+ IO.puts("#{inspect(type)} #{inspect(args)}")
# Application.get_env(:pandoc, profile)
children = [
diff --git a/lib/pandoc/watcher.ex b/lib/pandoc/watcher.ex
new file mode 100644
index 0000000..624763b
--- /dev/null
+++ b/lib/pandoc/watcher.ex
@@ -0,0 +1,33 @@
+defmodule Pandoc.Watcher do
+ use GenServer
+
+ def start_link(args) do
+ GenServer.start_link(__MODULE__, args)
+ end
+
+ def init([profile | args]) do
+ IO.puts("pandoc filesystem watcher init args #{inspect(args)}")
+
+ IO.puts(
+ "Application.get_env(:pandoc, :#{profile}): #{inspect(Application.get_env(:pandoc, profile))}"
+ )
+
+ {:ok, watcher_pid} = FileSystem.start_link(args)
+ FileSystem.subscribe(watcher_pid)
+ {:ok, %{watcher_pid: watcher_pid}}
+ end
+
+ def handle_info({:file_event, watcher_pid, {path, events}}, %{watcher_pid: watcher_pid} = state) do
+ case {Path.extname(path), :closed in events} do
+ {".md", true} -> Pandoc.run(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