Get path data from config vars
This commit is contained in:
parent
a1b1eff3d0
commit
9cb7bf35ab
@ -1,16 +1,38 @@
|
|||||||
defmodule Mix.Tasks.Pandoc do
|
defmodule Mix.Tasks.Pandoc do
|
||||||
use Mix.Task
|
use Mix.Task
|
||||||
|
|
||||||
@out_dir "priv/static"
|
|
||||||
|
|
||||||
@impl true
|
@impl true
|
||||||
def run([profile | _args]) do
|
def run(args) do
|
||||||
IO.puts "profile: #{profile}"
|
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!()
|
|> File.ls!()
|
||||||
|> Enum.each(fn path ->
|
|> 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)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
defp install_and_run([]) do
|
||||||
|
Mix.raise("`mix pandoc` expects the profile as argument")
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
@ -3,32 +3,16 @@ defmodule Pandoc do
|
|||||||
Documentation for `Pandoc`.
|
Documentation for `Pandoc`.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
def install_and_run(path) do
|
def run(profile, ["--watch"]) do
|
||||||
System.cmd("pandoc", [
|
IO.puts("""
|
||||||
"--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
|
Pandoc watcher starting, env: #{Application.get_env(:pandoc, profile) |> inspect(pretty: true)}
|
||||||
# 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 =
|
ref =
|
||||||
__MODULE__.Supervisor
|
__MODULE__.Supervisor
|
||||||
|> Supervisor.start_child(
|
|> Supervisor.start_child(
|
||||||
Supervisor.child_spec({Pandoc.Watcher, [profile, dirs: ["priv/posts"]]},
|
Supervisor.child_spec({Pandoc.Watcher, [profile, dirs: ["documents"]]},
|
||||||
restart: :transient,
|
restart: :transient,
|
||||||
id: __MODULE__.Watcher
|
id: __MODULE__.Watcher
|
||||||
)
|
)
|
||||||
@ -43,4 +27,34 @@ defmodule Pandoc do
|
|||||||
{:DOWN, ^ref, _, _, _} -> :ok
|
{:DOWN, ^ref, _, _, _} -> :ok
|
||||||
end
|
end
|
||||||
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
|
end
|
||||||
|
@ -6,15 +6,10 @@ defmodule Pandoc.Application do
|
|||||||
use Application
|
use Application
|
||||||
|
|
||||||
@impl true
|
@impl true
|
||||||
def start(type, args) do
|
def start(_type, _args) do
|
||||||
IO.puts("*********************************************")
|
|
||||||
IO.puts("#{inspect(type)} #{inspect(args)}")
|
|
||||||
# Application.get_env(:pandoc, profile)
|
|
||||||
|
|
||||||
children = [
|
children = [
|
||||||
# Starts a worker by calling: Pandoc.Worker.start_link(arg)
|
# Starts a worker by calling: Pandoc.Worker.start_link(arg)
|
||||||
# {Pandoc.Worker, arg}
|
# {Pandoc.Worker, arg}
|
||||||
# {Pandoc, dirs: ["priv/posts"]}
|
|
||||||
]
|
]
|
||||||
|
|
||||||
# See https://hexdocs.pm/elixir/Supervisor.html
|
# See https://hexdocs.pm/elixir/Supervisor.html
|
||||||
|
@ -1,25 +1,21 @@
|
|||||||
defmodule Pandoc.Watcher do
|
defmodule Pandoc.Watcher do
|
||||||
use GenServer
|
use GenServer
|
||||||
|
|
||||||
|
@ext ".md"
|
||||||
|
|
||||||
def start_link(args) do
|
def start_link(args) do
|
||||||
GenServer.start_link(__MODULE__, args)
|
GenServer.start_link(__MODULE__, args)
|
||||||
end
|
end
|
||||||
|
|
||||||
def init([profile | args]) do
|
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)
|
{:ok, watcher_pid} = FileSystem.start_link(args)
|
||||||
FileSystem.subscribe(watcher_pid)
|
FileSystem.subscribe(watcher_pid)
|
||||||
{:ok, %{watcher_pid: watcher_pid}}
|
{:ok, %{watcher_pid: watcher_pid, profile: profile}}
|
||||||
end
|
end
|
||||||
|
|
||||||
def handle_info({:file_event, watcher_pid, {path, events}}, %{watcher_pid: watcher_pid} = state) do
|
def handle_info({:file_event, watcher_pid, {path, events}}, %{watcher_pid: watcher_pid} = state) do
|
||||||
case {Path.extname(path), :closed in events} do
|
case {Path.extname(path), :closed in events} do
|
||||||
{".md", true} -> Pandoc.run(path)
|
{@ext, true} -> Pandoc.run(state[:profile], [path])
|
||||||
_ -> nil
|
_ -> nil
|
||||||
end
|
end
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user