Web Dev Solutions

Catalin Mititiuc

aboutsummaryrefslogtreecommitdiff
blob: a04ab26295d0facaefe59c54654f1ecc51d1cfce (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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
defmodule Mix.Tasks.Pandoc do
  @moduledoc """
  Invokes pandoc with the given args.

  Usage:

      $ mix pandoc TASK_OPTIONS PROFILE PANDOC_ARGS

  Example:

      $ mix pandoc default documents/hello.md -o priv/static/posts/hello.html

  If pandoc is not installed, it is automatically downloaded. Note the
  arguments given to this task will be appended to any configured arguments.

  ## Options

    * `--runtime-config` - load the runtime configuration
      before executing command

  Note flags to control this Mix task must be given before the profile:

      $ mix pandoc --runtime-config default documents/hello.md

  """

  @shortdoc "Invokes pandoc with the profile and args"
  @compile {:no_warn_undefined, Mix}

  use Mix.Task

  @impl true
  def run(args) do
    switches = [runtime_config: :boolean]
    {opts, remaining_args} = OptionParser.parse_head!(args, switches: switches)

    if function_exported?(Mix, :ensure_application!, 1) do
      Mix.ensure_application!(:inets)
      Mix.ensure_application!(:ssl)
    end

    if opts[:runtime_config] do
      Mix.Task.run("app.config")
    else
      Mix.Task.run("loadpaths")
      Application.ensure_all_started(:pandoc)
    end

    Mix.Task.reenable("pandoc")
    install_and_run(remaining_args)
  end

  defp install_and_run([profile | args] = all) do
    case Pandoc.install_and_run(String.to_atom(profile), args) do
      0 -> :ok
      status -> Mix.raise("`mix pandoc #{Enum.join(all, " ")}` exited with #{status}")
    end
  end

  defp install_and_run([]) do
    Mix.raise("`mix pandoc` expects the profile as argument")
  end
end