Web Dev Solutions

Catalin Mititiuc

aboutsummaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorCatalin Mititiuc <webdevcat@proton.me>2024-12-05 08:47:27 -0800
committerCatalin Mititiuc <webdevcat@proton.me>2024-12-05 08:47:27 -0800
commitd90365c58497274f65cd0c7b05606029bd756065 (patch)
treef72d04453bcce109677210a3b753537dd87670d5 /lib
Initial commitv0.1.0
Diffstat (limited to 'lib')
-rw-r--r--lib/pandoc.ex61
-rw-r--r--lib/pandoc/application.ex25
2 files changed, 86 insertions, 0 deletions
diff --git a/lib/pandoc.ex b/lib/pandoc.ex
new file mode 100644
index 0000000..bf01873
--- /dev/null
+++ b/lib/pandoc.ex
@@ -0,0 +1,61 @@
+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}
+ end
+
+ def run(profile) do
+ # Application.get_env(:pandoc, a1) |> inspect(pretty: true) |> IO.puts
+ Application.get_all_env(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)
+ )
+ |> case do
+ {:ok, pid} -> pid
+ {:error, {:already_started, pid}} -> pid
+ end
+ |> Process.monitor()
+
+ receive do
+ {:DOWN, ^ref, _, _, _} -> :ok
+ end
+ end
+end
+
diff --git a/lib/pandoc/application.ex b/lib/pandoc/application.ex
new file mode 100644
index 0000000..162341a
--- /dev/null
+++ b/lib/pandoc/application.ex
@@ -0,0 +1,25 @@
+defmodule Pandoc.Application do
+ # See https://hexdocs.pm/elixir/Application.html
+ # for more information on OTP Applications
+ @moduledoc false
+
+ use Application
+
+ @impl true
+ def start(type, args) do
+ IO.puts "*********************************************"
+ IO.puts "#{inspect(type)} #{inspect(args)}"
+ # Application.get_env(:pandoc, profile)
+
+ children = [
+ # Starts a worker by calling: Pandoc.Worker.start_link(arg)
+ # {Pandoc.Worker, arg}
+ # {Pandoc, dirs: ["priv/posts"]}
+ ]
+
+ # See https://hexdocs.pm/elixir/Supervisor.html
+ # for other strategies and supported options
+ opts = [strategy: :one_for_one, name: Pandoc.Supervisor]
+ Supervisor.start_link(children, opts)
+ end
+end