Web Dev Solutions

Catalin Mititiuc

aboutsummaryrefslogtreecommitdiff
blob: 336aeecf9cdb29c53c9b28f1ada1fdd626ced801 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
defmodule Pandoc do
  @moduledoc """
  Documentation for `Pandoc`.
  """

  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 install_and_run(profile, ["--watch"]) do
    # Application.get_env(:pandoc, a1) |> 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.Watcher, [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