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
|
# Pandoc
A watcher and a Mix task that uses Pandoc to convert markdown files to html.
## Requirements
- inotify-tools
- pandoc
## Installation
```elixir
# mix.exs
def deps do
[
{:pandoc, "~> 0.2.0", runtime: Mix.env() == :dev}
]
end
```
## Use
```elixir
# mix.exs
# ...
defp aliases do
[
# ...
"documents.build": ["pandoc hello"],
"statics.build": ["assets.build", "documents.build"],
"statics.deploy": ["assets.deploy", "documents.build"]
]
end
```
```elixir
# config/config.exs
config :pandoc,
hello: [
args: ~w(--mathjax -o ../priv/static/posts),
cd: Path.expand("../documents", __DIR__)
]
```
```elixir
# config/dev.exs
config :hello, HelloWeb.Endpoint,
# ...
watchers: [
# ...
pandoc: {Pandoc, :run, [:hello, ~w(--watch)]}
]
config :pandoc, hello: [pattern: "**/*.md"]
```
```elixir
# lib/hello_web/router.ex
scope "/", HelloWeb do
pipe_through :browser
get "/drafts/:id", PostController, :draft
get "/posts/:id", PostController, :show
get "/posts", PostController, :index
get "/", PageController, :home
end
```
```elixir
# lib/hello_web/controllers/posts_controller.ex
defmodule HelloWeb.PostController do
use HelloWeb, :controller
alias Hello.Document
@path "documents/**/*.md"
paths = Path.wildcard(@path)
@paths_hash :erlang.md5(paths)
for path <- paths, do: @external_resource(path)
@posts Document.list()
def __mix_recompile__?(), do: @path |> Path.wildcard() |> :erlang.md5() != @paths_hash
def index(conn, _params) do
render(conn, :index, posts: @posts)
end
def show(conn, %{"id" => id}) do
assigns = [
post: :hello |> :code.priv_dir() |> Path.join("static/posts/#{id}.html") |> File.read!()
]
render(conn, :show, assigns)
end
def drafts(conn, %{"id" => id}) do
config = Application.get_env(:pandoc, :hello)
opts = [
cd: config[:cd] || File.cwd!()
]
filename = List.keyfind(@posts, id, 0) |> elem(1) |> Map.get(:filename)
path = Path.join("_drafts", filename)
render(conn, :show, post: "pandoc" |> System.cmd([path], opts) |> elem(0))
end
```
```elixir
# lib/hello/document.ex
defmodule Stasis.Document do
require Logger
@ext ".md"
@pattern Application.compile_env(:pandoc, [:hello, :pattern])
def list() do
"documents"
|> Path.join(@pattern)
|> Path.wildcard()
|> Enum.map(fn path -> {Path.basename(path), path} end)
|> Enum.sort(fn {basename_a, _}, {basename_b, _} -> basename_a < basename_b end)
|> Enum.reduce([], fn {filename, path}, acc ->
id = Path.rootname(filename, @ext)
data = if "_drafts" in Path.split(path), do: %{:draft, true}, else: %{}
[{id, data} | acc]
end)
end
```
```elixir
# lib/hello_web/controllers/post_html.ex
# ...
defp href(filename, draft \\ false) do
root = (draft && "/drafts") || "/posts"
Path.join(root, filename |> Path.basename(".md"))
end
```
```heex
<!-- lib/hello_web/controllers/post_html/index.html.heex -->
<%= for {id, data} <- @posts do %>
<p>
<.link href={href(Path.rootname(filename), data[:draft])} method="get">
<%= id %>
</.link>
</p>
<% end %>
```
```heex
<!-- lib/hello_web/controllers/post_html/show.html.heex -->
<%= raw(@post) %>
```
|