1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
|
defmodule Pandoc do
# https://github.com/jgm/pandoc/releases
@latest_version "3.6.1"
@moduledoc """
Pandoc is an installer, runner and watcher for [pandoc](https://pandoc.org).
## Profiles
You can define multiple pandoc profiles. By default, there is a profile
called `:default` which you can configure its args, current directory and
environment. You can make the args dynamic by defining a function.
config :pandoc,
version: "#{@latest_version}",
default: [
args: fn extra_args ->
{_, [input_file], _} = OptionParser.parse(extra_args, switches: [])
~w(--output=../priv/static/posts/\#{Path.rootname(input_file)}.html)
end,
cd: Path.expand("../documents", __DIR__)
]
## Pandoc configuration
There are four global configurations for the pandoc application:
* `:version` - the expected pandoc version
* `:version_check` - whether to perform the version check or not.
Useful when you manage the pandoc executable with an external
tool
* `:cacerts_path` - the directory to find certificates for
https connections
* `:path` - the path to find the pandoc executable at. By
default, it is automatically downloaded and placed inside
the `_build` directory of your current app
Overriding the `:path` is not recommended, as we will automatically download
and manage `pandoc` for you. But in case you can't download it, you may want
to set the `:path` to a configurable system location.
"""
require Logger
@doc false
# Latest known version at the time of publishing.
def latest_version, do: @latest_version
@doc """
Returns the configured pandoc version.
"""
def configured_version do
Application.get_env(:pandoc, :version, latest_version())
end
@doc """
Returns the configuration for the given profile.
Returns nil if the profile does not exist.
"""
def config_for!(profile) when is_atom(profile) do
Application.get_env(:pandoc, profile) ||
raise ArgumentError, """
unknown pandoc profile. Make sure the profile is defined in your config/config.exs file, such as:
config :pandoc,
version: "#{@latest_version}",
#{profile}: [
cd: Path.expand("../documents", __DIR__)
]
"""
end
@doc """
Returns the path to the executable.
The executable may not be available if it was not yet installed.
"""
def bin_path do
name = "pandoc-#{target()}"
Application.get_env(:pandoc, :path) ||
if Code.ensure_loaded?(Mix.Project) do
relative_build_dir = Mix.Project.build_path() |> Path.dirname() |> Path.relative_to_cwd()
project_dir = Path.dirname(Mix.Project.project_file())
Path.join([project_dir, relative_build_dir, name])
else
Path.expand("_build/#{name}")
end
end
@doc """
Returns the version of the pandoc executable.
Returns `{:ok, version_string}` on success or `:error` when the executable
is not available.
"""
def bin_version do
path = bin_path()
with true <- File.exists?(path),
{out, 0} <- System.cmd(path, ["--version"]),
[vsn] <- Regex.run(~r/#{Path.basename(path)} ([^\s]+)/, out, capture: :all_but_first) do
{:ok, vsn}
else
_ -> :error
end
end
@doc """
Starts a file system watcher that runs the given command with `args` when a
file event is received for a file that matches the given pattern.
The given args will be appended to the configured args. The task output will
be streamed directly to stdio.
"""
def watch(profile, extra_args \\ [], pattern \\ ~r/\.md$/) when is_atom(profile) do
config = config_for!(profile)
opts = [dirs: [config[:cd] || File.cwd!()]]
ref =
__MODULE__.Supervisor
|> Supervisor.start_child(
Supervisor.child_spec({Pandoc.Watcher, [profile, opts, pattern, extra_args]},
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
@doc """
Runs the given command with `args`.
The given args will be appended to the configured args. The task output will
be streamed directly to stdio. It returns the status of the underlying call.
"""
def run(profile, extra_args) when is_atom(profile) and is_list(extra_args) do
config = config_for!(profile)
args =
case config[:args] do
args_fn when is_function(args_fn) -> args_fn.(extra_args)
args -> args || []
end
if args == [] and extra_args == [] do
raise "no arguments passed to pandoc"
end
opts = [
cd: config[:cd] || File.cwd!(),
into: IO.stream(:stdio, :line),
stderr_to_stdout: true
]
{parsed_args, _, _} = OptionParser.parse(args, switches: [output: :string])
{_, input_files, _} = OptionParser.parse(extra_args, switches: [])
if parsed_args[:output] &&
not File.cd!(opts[:cd], fn ->
input_files |> Enum.map(&File.exists?(&1)) |> Enum.all?()
end) do
parsed_args[:output] |> Path.expand(opts[:cd]) |> File.rm()
else
bin_path() |> System.cmd(args ++ extra_args, opts) |> elem(1)
end
end
defp start_unique_install_worker() do
ref =
__MODULE__.Supervisor
|> Supervisor.start_child(
Supervisor.child_spec({Task, &install/0}, restart: :transient, id: __MODULE__.Installer)
)
|> case do
{:ok, pid} -> pid
{:error, {:already_started, pid}} -> pid
end
|> Process.monitor()
receive do
{:DOWN, ^ref, _, _, _} -> :ok
end
end
@doc """
Installs, if not available, and then runs `pandoc`.
This task may be invoked concurrently and it will avoid concurrent installs.
Returns the same as `run/2`.
"""
def install_and_run(profile, args) do
File.exists?(bin_path()) || start_unique_install_worker()
run(profile, args)
end
@doc """
The default URL to install Pandoc from.
"""
def default_base_url do
"https://github.com/jgm/pandoc/releases/download/$version/pandoc-$version-$target.tar.gz"
end
@doc """
Installs pandoc with `configured_version/0`.
If invoked concurrently, this task will perform concurrent installs.
"""
def install(base_url \\ default_base_url()) do
version = configured_version()
tmp_opts = if System.get_env("MIX_XDG"), do: %{os: :linux}, else: %{}
tmp_dir =
freshdir_p(:filename.basedir(:user_cache, "phx-pandoc", tmp_opts)) ||
freshdir_p(Path.join(System.tmp_dir!(), "phx-pandoc")) ||
raise "could not install pandoc. Set MIX_XGD=1 and then set XDG_CACHE_HOME to the path you want to use as cache"
url = get_url(base_url)
tar = fetch_body!(url)
case :erl_tar.extract({:binary, tar}, [:compressed, cwd: to_charlist(tmp_dir)]) do
:ok -> :ok
other -> raise "couldn't unpack archive: #{inspect(other)}"
end
bin_path = bin_path()
File.mkdir_p!(Path.dirname(bin_path))
[tmp_dir, "pandoc-" <> version, "bin", "pandoc"] |> Path.join() |> File.cp!(bin_path)
end
defp freshdir_p(path) do
with {:ok, _} <- File.rm_rf(path),
:ok <- File.mkdir_p(path) do
path
else
_ -> nil
end
end
defp fetch_body!(url, retry \\ true) do
scheme = URI.parse(url).scheme
url = String.to_charlist(url)
Logger.debug("Downloading pandoc from #{url}")
{:ok, _} = Application.ensure_all_started(:inets)
{:ok, _} = Application.ensure_all_started(:ssl)
if proxy = proxy_for_scheme(scheme) do
%{host: host, port: port} = URI.parse(proxy)
Logger.debug("Using #{String.upcase(scheme)}_PROXY: #{proxy}")
set_option = if "https" == scheme, do: :https_proxy, else: :proxy
:httpc.set_options([{set_option, {{String.to_charlist(host), port}, []}}])
end
# https://erlef.github.io/security-wg/secure_coding_and_deployment_hardening/inets
cacertfile = cacertfile() |> String.to_charlist()
http_options =
[
ssl: [
verify: :verify_peer,
cacertfile: cacertfile,
depth: 2,
customize_hostname_check: [
match_fun: :public_key.pkix_verify_hostname_match_fun(:https)
],
versions: protocol_versions()
]
]
|> maybe_add_proxy_auth(scheme)
options = [body_format: :binary]
case {retry, :httpc.request(:get, {url, []}, http_options, options)} do
{_, {:ok, {{_, 200, _}, _headers, body}}} ->
body
{true, {:error, {:failed_connect, [{:to_address, _}, {inet, _, reason}]}}}
when inet in [:inet, :inet6] and
reason in [:ehostunreach, :enetunreach, :eprotonosupport, :nxdomain] ->
:httpc.set_options(ipfamily: fallback(inet))
fetch_body!(url, false)
other ->
raise """
Couldn't fetch #{url}: #{inspect(other)}
This typically means we cannot reach the source or you are behind a proxy.
You can try again later and, if that does not work, you might:
1. If behind a proxy, ensure your proxy is configured and that
your certificates are set via the cacerts_path configuration
2. Manually download the executable from the URL above and
place it inside "_build/pandoc-#{target()}"
"""
end
end
defp fallback(:inet), do: :inet6
defp fallback(:inet6), do: :inet
defp proxy_for_scheme("http") do
System.get_env("HTTP_PROXY") || System.get_env("http_proxy")
end
defp proxy_for_scheme("https") do
System.get_env("HTTPS_PROXY") || System.get_env("https_proxy")
end
defp maybe_add_proxy_auth(http_options, scheme) do
case proxy_auth(scheme) do
nil -> http_options
auth -> [{:proxy_auth, auth} | http_options]
end
end
defp proxy_auth(scheme) do
with proxy when is_binary(proxy) <- proxy_for_scheme(scheme),
%{userinfo: userinfo} when is_binary(userinfo) <- URI.parse(proxy),
[username, password] <- String.split(userinfo, ":") do
{String.to_charlist(username), String.to_charlist(password)}
else
_ -> nil
end
end
defp cacertfile() do
Application.get_env(:pandoc, :cacerts_path) || CAStore.file_path()
end
defp protocol_versions do
if otp_version() < 25, do: [:"tlsv1.2"], else: [:"tlsv1.2", :"tlsv1.3"]
end
defp otp_version do
:erlang.system_info(:otp_release) |> List.to_integer()
end
# Available targets: https://github.com/jgm/pandoc/releases
# We support only linux-amd64, for now.
defp target do
case :os.type() do
# Assuming it's an x86 CPU
{:win32, _} ->
raise "pandoc does not currently support OS family: Windows"
{:unix, osname} ->
arch_str = :erlang.system_info(:system_architecture)
[arch | _] = arch_str |> List.to_string() |> String.split("-")
case arch do
"amd64" -> "#{osname}-amd64"
"x86_64" -> "#{osname}-amd64"
_ -> raise "pandoc does not currently support architecture: #{arch_str}"
end
end
end
defp get_url(base_url) do
base_url
|> String.replace("$version", configured_version())
|> String.replace("$target", target())
end
end
|