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
|
defmodule Nola.Plugins.Link.PDF do
require Logger
@behaviour Nola.Plugins.Link
@impl true
def match(_, _), do: false
@impl true
def post_match(_url, "application/pdf" <> _, _header, _opts) do
{:file, nil}
end
def post_match(_, _, _, _), do: false
@impl true
def post_expand(url, file, _, _) do
case System.cmd("pdftitle", ["-p", file]) do
{text, 0} ->
text =
text
|> String.trim()
if text == "" do
:error
else
basename = Path.basename(url, ".pdf")
text =
("[#{basename}] " <> text)
|> String.split("\n")
{:ok, text}
end
{_, 127} ->
Logger.error(
"dependency `pdftitle` is missing, please install it: `pip3 install pdftitle`."
)
:error
{error, code} ->
Logger.warn("command `pdftitle` exited with status code #{code}:\n#{inspect(error)}")
:error
end
end
end
|