61 lines
1.3 KiB
Elixir
61 lines
1.3 KiB
Elixir
defmodule Pandoc do
|
|
@moduledoc """
|
|
Documentation for `Pandoc`.
|
|
"""
|
|
|
|
def run(profile, ["--watch" | dirs]) do
|
|
IO.puts("""
|
|
|
|
Pandoc watcher starting, env: #{Application.get_env(:pandoc, profile) |> inspect(pretty: true)}
|
|
""")
|
|
|
|
ref =
|
|
__MODULE__.Supervisor
|
|
|> Supervisor.start_child(
|
|
Supervisor.child_spec({Pandoc.Watcher, [profile, dirs: dirs]},
|
|
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
|
|
|
|
def run(profile, extra_args) do
|
|
config = Application.get_env(:pandoc, profile)
|
|
args = config[:args] || []
|
|
|
|
opts = [
|
|
cd: config[:cd] || File.cwd!(),
|
|
into: IO.stream(:stdio, :line),
|
|
stderr_to_stdout: true
|
|
]
|
|
|
|
[path] = extra_args
|
|
|
|
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)
|
|
|
|
IO.puts("""
|
|
System command running: #{inspect(Enum.join(["pandoc" | args ++ extra_args], " "))}
|
|
""")
|
|
|
|
"pandoc" |> System.cmd(args ++ extra_args, opts) |> elem(1)
|
|
end
|
|
end
|