Move watcher into a submodule; add mix task
This commit is contained in:
parent
5a6a899365
commit
a1b1eff3d0
16
lib/mix/tasks/pandoc.ex
Normal file
16
lib/mix/tasks/pandoc.ex
Normal file
@ -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
|
@ -2,50 +2,36 @@ defmodule Pandoc do
|
|||||||
@moduledoc """
|
@moduledoc """
|
||||||
Documentation for `Pandoc`.
|
Documentation for `Pandoc`.
|
||||||
"""
|
"""
|
||||||
use GenServer
|
|
||||||
|
|
||||||
def start_link(args) do
|
def install_and_run(path) do
|
||||||
GenServer.start_link(__MODULE__, args)
|
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
|
end
|
||||||
|
|
||||||
def init([profile | args]) do
|
def install_and_run(profile, ["--watch"]) 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_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 =
|
ref =
|
||||||
__MODULE__.Supervisor
|
__MODULE__.Supervisor
|
||||||
|> Supervisor.start_child(
|
|> Supervisor.start_child(
|
||||||
Supervisor.child_spec({Pandoc, [profile, dirs: ["priv/posts"]]}, restart: :transient, id:
|
Supervisor.child_spec({Pandoc.Watcher, [profile, dirs: ["priv/posts"]]},
|
||||||
__MODULE__.Watcher)
|
restart: :transient,
|
||||||
|
id: __MODULE__.Watcher
|
||||||
|
)
|
||||||
)
|
)
|
||||||
|> case do
|
|> case do
|
||||||
{:ok, pid} -> pid
|
{:ok, pid} -> pid
|
||||||
@ -58,4 +44,3 @@ defmodule Pandoc do
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -7,8 +7,8 @@ defmodule Pandoc.Application do
|
|||||||
|
|
||||||
@impl true
|
@impl true
|
||||||
def start(type, args) do
|
def start(type, args) do
|
||||||
IO.puts "*********************************************"
|
IO.puts("*********************************************")
|
||||||
IO.puts "#{inspect(type)} #{inspect(args)}"
|
IO.puts("#{inspect(type)} #{inspect(args)}")
|
||||||
# Application.get_env(:pandoc, profile)
|
# Application.get_env(:pandoc, profile)
|
||||||
|
|
||||||
children = [
|
children = [
|
||||||
|
33
lib/pandoc/watcher.ex
Normal file
33
lib/pandoc/watcher.ex
Normal file
@ -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
|
Loading…
x
Reference in New Issue
Block a user