index : pandoc | |
Hex package with a file-watcher and Mix task for using Pandoc to convert Markdown files to HTML. |
aboutsummaryrefslogtreecommitdiff |
diff options
-rw-r--r-- | .formatter.exs | 4 | ||||
-rw-r--r-- | .gitignore | 26 | ||||
-rw-r--r-- | README.md | 21 | ||||
-rw-r--r-- | lib/pandoc.ex | 61 | ||||
-rw-r--r-- | lib/pandoc/application.ex | 25 | ||||
-rw-r--r-- | mix.exs | 31 | ||||
-rw-r--r-- | test/pandoc_test.exs | 8 | ||||
-rw-r--r-- | test/test_helper.exs | 1 |
8 files changed, 177 insertions, 0 deletions
diff --git a/.formatter.exs b/.formatter.exs new file mode 100644 index 0000000..d2cda26 --- /dev/null +++ b/.formatter.exs @@ -0,0 +1,4 @@ +# Used by "mix format" +[ + inputs: ["{mix,.formatter}.exs", "{config,lib,test}/**/*.{ex,exs}"] +] diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..8b475d4 --- /dev/null +++ b/.gitignore @@ -0,0 +1,26 @@ +# The directory Mix will write compiled artifacts to. +/_build/ + +# If you run "mix test --cover", coverage assets end up here. +/cover/ + +# The directory Mix downloads your dependencies sources to. +/deps/ + +# Where third-party dependencies like ExDoc output generated docs. +/doc/ + +# Ignore .fetch files in case you like to edit your project deps locally. +/.fetch + +# If the VM crashes, it generates a dump, let's ignore it too. +erl_crash.dump + +# Also ignore archive artifacts (built via "mix archive.build"). +*.ez + +# Ignore package tarball (built via "mix hex.build"). +pandoc-*.tar + +# Temporary files, for example, from tests. +/tmp/ diff --git a/README.md b/README.md new file mode 100644 index 0000000..bf7b0b0 --- /dev/null +++ b/README.md @@ -0,0 +1,21 @@ +# Pandoc + +**TODO: Add description** + +## Installation + +If [available in Hex](https://hex.pm/docs/publish), the package can be installed +by adding `pandoc` to your list of dependencies in `mix.exs`: + +```elixir +def deps do + [ + {:pandoc, "~> 0.1.0"} + ] +end +``` + +Documentation can be generated with [ExDoc](https://github.com/elixir-lang/ex_doc) +and published on [HexDocs](https://hexdocs.pm). Once published, the docs can +be found at <https://hexdocs.pm/pandoc>. + diff --git a/lib/pandoc.ex b/lib/pandoc.ex new file mode 100644 index 0000000..bf01873 --- /dev/null +++ b/lib/pandoc.ex @@ -0,0 +1,61 @@ +defmodule Pandoc do + @moduledoc """ + Documentation for `Pandoc`. + """ + use GenServer + + 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)}" + # inspect(Application.get_all_env(:pandoc)) |> IO.puts + inspect(Application.get_env(:pandoc, profile)) |> IO.puts + {:ok, watcher_pid} = FileSystem.start_link(args) + FileSystem.subscribe(watcher_pid) + {:ok, %{watcher_pid: watcher_pid}} + 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} -> + System.cmd("pandoc", [ + "--mathjax", + path, + "-o", + Path.join("priv/static/posts", Path.basename(path) |> String.replace_suffix(".md", ".html") |> String.slice(11..-1//1)) + ]) + _ -> nil + end + + {:noreply, state} + end + + def handle_info({:file_event, watcher_pid, :stop}, %{watcher_pid: watcher_pid} = state) do + # Your own logic when monitor stop + {:noreply, state} + end + + def run(profile) do + # Application.get_env(:pandoc, a1) |> inspect(pretty: true) |> IO.puts + Application.get_all_env(profile) |> inspect(pretty: true) |> IO.puts + + ref = + __MODULE__.Supervisor + |> Supervisor.start_child( + Supervisor.child_spec({Pandoc, [profile, dirs: ["priv/posts"]]}, restart: :transient, id: + __MODULE__.Watcher) + ) + |> case do + {:ok, pid} -> pid + {:error, {:already_started, pid}} -> pid + end + |> Process.monitor() + + receive do + {:DOWN, ^ref, _, _, _} -> :ok + end + end +end + diff --git a/lib/pandoc/application.ex b/lib/pandoc/application.ex new file mode 100644 index 0000000..162341a --- /dev/null +++ b/lib/pandoc/application.ex @@ -0,0 +1,25 @@ +defmodule Pandoc.Application do + # See https://hexdocs.pm/elixir/Application.html + # for more information on OTP Applications + @moduledoc false + + use Application + + @impl true + def start(type, args) do + IO.puts "*********************************************" + IO.puts "#{inspect(type)} #{inspect(args)}" + # Application.get_env(:pandoc, profile) + + 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 + # for other strategies and supported options + opts = [strategy: :one_for_one, name: Pandoc.Supervisor] + Supervisor.start_link(children, opts) + end +end @@ -0,0 +1,31 @@ +defmodule Pandoc.MixProject do + use Mix.Project + + def project do + [ + app: :pandoc, + version: "0.1.0", + elixir: "~> 1.14", + deps: deps(), + description: "Pandoc", + package: [] + ] + end + + # Run "mix help compile.app" to learn about applications. + def application do + [ + extra_applications: [:logger], + mod: {Pandoc.Application, []} + ] + end + + # Run "mix help deps" to learn about dependencies. + defp deps do + [ + # {:dep_from_hexpm, "~> 0.3.0"}, + # {:dep_from_git, git: "https://github.com/elixir-lang/my_dep.git", tag: "0.1.0"} + {:file_system, "~> 1.0"} + ] + end +end diff --git a/test/pandoc_test.exs b/test/pandoc_test.exs new file mode 100644 index 0000000..1a4c067 --- /dev/null +++ b/test/pandoc_test.exs @@ -0,0 +1,8 @@ +defmodule PandocTest do + use ExUnit.Case + doctest Pandoc + + test "greets the world" do + assert Pandoc.hello() == :world + end +end diff --git a/test/test_helper.exs b/test/test_helper.exs new file mode 100644 index 0000000..869559e --- /dev/null +++ b/test/test_helper.exs @@ -0,0 +1 @@ +ExUnit.start() |