Web Dev Solutions

Catalin Mititiuc

From b8d8b3dbd88ab42fc8f050af0d18fc4dd66d7ffe Mon Sep 17 00:00:00 2001 From: Catalin Mititiuc Date: Thu, 9 Jan 2025 11:25:52 -0800 Subject: Handle installing Pandoc --- lib/mix/tasks/pandoc.ex | 62 ++++++++++++++++++++++++++++++------------------- 1 file changed, 38 insertions(+), 24 deletions(-) (limited to 'lib/mix/tasks/pandoc.ex') diff --git a/lib/mix/tasks/pandoc.ex b/lib/mix/tasks/pandoc.ex index 2c1573d..a04ab26 100644 --- a/lib/mix/tasks/pandoc.ex +++ b/lib/mix/tasks/pandoc.ex @@ -1,13 +1,44 @@ defmodule Mix.Tasks.Pandoc do - use Mix.Task + @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 - @ext ".md" + 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 @@ -19,28 +50,11 @@ defmodule Mix.Tasks.Pandoc do install_and_run(remaining_args) end - defp install_and_run([profile | _args] = all) do - IO.puts("Converting markdown...") - - profile = String.to_atom(profile) - config = Application.get_env(:pandoc, profile) - args = config[:args] || [] - opts = [cd: config[:cd] || File.cwd!()] - - out_path = List.last(args) - full_out_path = [opts[:cd], out_path] |> Path.join() |> Path.expand() - File.rm_rf!(full_out_path) - File.mkdir_p!(full_out_path) - - opts[:cd] - |> Path.join("*#{@ext}") - |> Path.wildcard() - |> Enum.each(fn path -> - case Pandoc.run(profile, path) do - 0 -> :ok - status -> Mix.raise("`mix pandoc #{Enum.join(all, " ")}` exited with #{status}") - end - 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 -- cgit v1.2.3