diff options
Diffstat (limited to '')
-rw-r--r-- | lib/lsg_irc/link_plugin/pdf.ex | 39 |
1 files changed, 39 insertions, 0 deletions
diff --git a/lib/lsg_irc/link_plugin/pdf.ex b/lib/lsg_irc/link_plugin/pdf.ex new file mode 100644 index 0000000..8c4869c --- /dev/null +++ b/lib/lsg_irc/link_plugin/pdf.ex @@ -0,0 +1,39 @@ +defmodule LSG.IRC.LinkPlugin.PDF do + require Logger + @behaviour LSG.IRC.LinkPlugin + + @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 |