Get path data from config vars

This commit is contained in:
Catalin Constantin Mititiuc 2025-06-17 11:17:37 -07:00
parent a1b1eff3d0
commit 9cb7bf35ab
4 changed files with 68 additions and 41 deletions

View File

@ -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)
"priv/posts"
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
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

View File

@ -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 run(profile, ["--watch"]) do
IO.puts("""
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()
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

View File

@ -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

View File

@ -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