index : pandoc | |
Hex package with a file-watcher and Mix task for using Pandoc to convert Markdown files to HTML. |
aboutsummaryrefslogtreecommitdiff |
diff options
author | Catalin Mititiuc <webdevcat@proton.me> | 2024-12-05 14:00:23 -0800 |
---|---|---|
committer | Catalin Mititiuc <webdevcat@proton.me> | 2024-12-05 14:40:37 -0800 |
commit | 6fc8e8ec192051938da4d31459698d55b1e40ea2 (patch) | |
tree | 351a1e949c6d1d075370cdbd6a951f94eb43df87 | |
parent | d9dd7e8cadebf41581d341aac3ce2b647bd7ad0a (diff) |
Get path data from config vars
-rw-r--r-- | lib/mix/tasks/pandoc.ex | 34 | ||||
-rw-r--r-- | lib/pandoc.ex | 56 | ||||
-rw-r--r-- | lib/pandoc/application.ex | 7 | ||||
-rw-r--r-- | lib/pandoc/watcher.ex | 12 |
4 files changed, 68 insertions, 41 deletions
diff --git a/lib/mix/tasks/pandoc.ex b/lib/mix/tasks/pandoc.ex index fe93208..4da8bf1 100644 --- a/lib/mix/tasks/pandoc.ex +++ b/lib/mix/tasks/pandoc.ex @@ -1,16 +1,38 @@ defmodule Mix.Tasks.Pandoc do use Mix.Task - @out_dir "priv/static" - @impl true - def run([profile | _args]) do - IO.puts "profile: #{profile}" + def run(args) do + switches = [runtime_config: :boolean] + {opts, remaining_args} = OptionParser.parse_head!(args, switches: switches) + + 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 - "priv/posts" + defp install_and_run([profile | args] = _all) do + IO.puts("mix task args #{inspect(args)}") + + "documents" |> File.ls!() |> Enum.each(fn path -> - Pandoc.install_and_run(Path.join(["priv", "posts", path])) + all = [profile, path] + + case Pandoc.run(String.to_atom(profile), [path]) do + 0 -> :ok + status -> Mix.raise("`mix pandoc #{Enum.join(all, " ")}` exited with #{status}") + end end) end + + defp install_and_run([]) do + Mix.raise("`mix pandoc` expects the profile as argument") + end end diff --git a/lib/pandoc.ex b/lib/pandoc.ex index 336aeec..1c3970d 100644 --- a/lib/pandoc.ex +++ b/lib/pandoc.ex @@ -3,32 +3,16 @@ defmodule Pandoc do 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)}" - ) + def run(profile, ["--watch"]) do + IO.puts(""" - # Application.get_env(:pandoc, profile) |> inspect(pretty: true) |> 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: ["priv/posts"]]}, + Supervisor.child_spec({Pandoc.Watcher, [profile, dirs: ["documents"]]}, restart: :transient, id: __MODULE__.Watcher ) @@ -43,4 +27,34 @@ defmodule Pandoc 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 diff --git a/lib/pandoc/application.ex b/lib/pandoc/application.ex index fd1575f..e30e2fa 100644 --- a/lib/pandoc/application.ex +++ b/lib/pandoc/application.ex @@ -6,15 +6,10 @@ defmodule Pandoc.Application do use Application @impl true - def start(type, args) do - IO.puts("*********************************************") - IO.puts("#{inspect(type)} #{inspect(args)}") - # Application.get_env(:pandoc, profile) - + def start(_type, _args) do children = [ # Starts a worker by calling: Pandoc.Worker.start_link(arg) # {Pandoc.Worker, arg} - # {Pandoc, dirs: ["priv/posts"]} ] # See https://hexdocs.pm/elixir/Supervisor.html diff --git a/lib/pandoc/watcher.ex b/lib/pandoc/watcher.ex index 624763b..e5439f6 100644 --- a/lib/pandoc/watcher.ex +++ b/lib/pandoc/watcher.ex @@ -1,25 +1,21 @@ defmodule Pandoc.Watcher do use GenServer + @ext ".md" + 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}} + {:ok, %{watcher_pid: watcher_pid, profile: profile}} 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) + {@ext, true} -> Pandoc.run(state[:profile], [path]) _ -> nil end |