-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathblog.ex
176 lines (144 loc) · 3.68 KB
/
blog.ex
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
defimpl Solid.Matcher, for: Tuple do
def match(data, []), do: {:ok, data}
def match(data, ["size"]) do
{:ok, tuple_size(data)}
end
def match(data, [key | keys]) when is_integer(key) do
try do
value = elem(data, key)
@protocol.match(value, keys)
rescue
ArgumentError ->
{:error, :not_found}
end
end
end
defmodule Blog do
defmodule Filters do
require Tableau.Document.Helper
def render(inner_content) do
Tableau.Document.Helper.render(inner_content)
end
def print(term) do
dbg(term)
""
end
def og_image_url(permalink) do
file = Tableau.OgExtension.file_name(permalink)
Path.join("https://f005.backblazeb2.com/file/BlogOgImages/og", file)
end
def markdownify(markdown) do
MDEx.to_html(markdown)
end
def absolute_url(url) do
host = Application.get_env(:tableau, :config)[:url]
host
|> URI.parse()
|> URI.merge(url)
|> URI.to_string()
end
def book_url(links, goodreads_id) do
link =
Enum.find(links, fn link ->
link["id"] == goodreads_id
end)
Map.get(link, "link", "https://amazon.com/s?k=#{Base.url_encode64(link["title"])}")
end
def array_to_sentence_string(items) do
Enum.join(items, ", ")
end
def to_date_time(datetime) do
DateTimeParser.parse_datetime!(datetime)
end
def group_by_year(books) do
if books do
books
|> Enum.group_by(fn book ->
book["date_read"].year
end)
|> Enum.sort_by(fn {year, _} -> year end, :desc)
else
[]
end
end
def reading_time(post) do
words = post["body"] |> String.split(" ") |> Enum.count()
mins =
if words < 360 do
1
else
words / 180
end
|> Kernel./(2)
|> ceil()
"#{mins} minute read"
end
def reload(env) do
if env == "dev" do
Tableau.live_reload(%{})
else
""
end
end
def get_review(id, posts) do
Enum.find(posts || [], fn post ->
get_goodreads_id(post) == id
end)
end
defp get_goodreads_id(post) do
unless post["book"] do
%{}
else
post["book"]["goodreads_id"]
end
end
def slugify(str) do
String.replace(str, " ", "-")
end
def app_css(path) do
File.read!(path)
end
end
defmacro sigil_L({:<<>>, _, [binary]}, []) do
{:ok, template} = Solid.parse(binary)
fs = Solid.LocalFileSystem.new("_includes", "%s.html")
base =
Macro.escape(%{
tableau: %{
environment: to_string(Mix.env())
}
})
quote do
case Solid.render(
unquote(Macro.escape(template)),
Blog.stringify(Map.merge(unquote(base), var!(assigns))),
custom_filters: Blog.Filters,
strict_variables: false,
file_system: {Solid.LocalFileSystem, unquote(Macro.escape(fs))}
) do
{:ok, iolist} ->
IO.iodata_to_binary(iolist)
{:error, error, iolist} ->
require Logger
Logger.warning(inspect(error, pretty: true))
iolist |> IO.iodata_to_binary()
end
end
end
def stringify(%struct{}) when struct not in [Date, DateTime, NaiveDateTime] do
Map.from_struct(struct) |> stringify()
end
def stringify(%_struct{} = data) do
data
end
def stringify(map) when is_map(map) do
Map.new(map, fn
{k, v} when is_atom(k) -> {to_string(k), stringify(v)}
other -> other
end)
end
def stringify(list) when is_list(list) do
Enum.map(list, &stringify/1)
end
def stringify(other), do: other
end