-
-
Notifications
You must be signed in to change notification settings - Fork 448
/
utils.jl
237 lines (222 loc) · 7.31 KB
/
utils.jl
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
using Dates
"""
{{meta}}
Plug in specific meta information for a blog page. The `meta` local page
variable should be given as an iterable of 3-tuples like so:
```
@def meta = [("property", "og:video", "http://example.com/"),
("name", "twitter:player", "https://www.youtube.com/embed/XXXXXX")]
```
A full example can be found in `blog/2020/05/rr.md`.
"""
function hfun_meta()
title = locvar(:title)
isnothing(title) && (title = "The Julia Language")
descr = locvar(:rss_description)
isnothing(descr) && (descr = "Official website for the Julia programming language")
p = "property"
# default og properties, can be overwritten by the user
ogdflt = (
title = (p, "og:title", title),
descr = (p, "og:description", descr),
image = (p, "og:image", "/assets/images/julia-open-graph.png"),
)
# check what the user has provided (if anything) use defaults otherwise
meta = locvar(:meta)
if !isnothing(meta)
for c in keys(ogdflt)
any(m -> m[2] == "og:$c", meta) || push!(meta, getindex(ogdflt, c))
end
else
meta = values(ogdflt)
end
io = IOBuffer()
for m in meta
write(io, "<meta $(m[1])=\"$(m[2])\" content=\"$(m[3])\">\n")
end
return String(take!(io))
end
"""
{{blogposts}}
Plug in the list of blog posts contained in the `/blog/` folder.
"""
function hfun_blogposts()
curyear = year(Dates.today())
io = IOBuffer()
for year in curyear:-1:2012
ys = "$year"
year < curyear && write(io, "\n**$year**\n")
for month in 12:-1:1
ms = "0"^(month < 10) * "$month"
base = joinpath("blog", ys, ms)
isdir(base) || continue
posts = filter!(p -> endswith(p, ".md"), readdir(base))
days = zeros(Int, length(posts))
lines = Vector{String}(undef, length(posts))
for (i, post) in enumerate(posts)
ps = splitext(post)[1]
url = "/blog/$ys/$ms/$ps/"
surl = strip(url, '/')
# Franklin.pagevar appears to be an internal function that has a guard against
# recursive overflow so we need to reset the counter to 0, otherwise we cannot read
# pagevars for all pages.
Franklin.PAGEVAR_DEPTH[] = 0
title = pagevar(surl, :title; default="Untitled")
Franklin.PAGEVAR_DEPTH[] = 0
pubdate = pagevar(surl, :published)
if isnothing(pubdate)
date = "$ys-$ms-01"
days[i] = 1
else
date = Date(pubdate, dateformat"d U Y")
days[i] = day(date)
end
lines[i] = "\n[$title]($url) $date \n"
end
# sort by day
foreach(line -> write(io, line), lines[sortperm(days, rev=true)])
end
end
# markdown conversion adds `<p>` beginning and end but
# we want to avoid this to avoid an empty separator
r = Franklin.fd2html(String(take!(io)), internal=true)
return r
end
"""
{{recentblogposts}}
Input the 3 latest blog posts.
"""
function hfun_recentblogposts()
curyear = Dates.Year(Dates.today()).value
ntofind = 3
nfound = 0
recent = Vector{Pair{String,Date}}(undef, ntofind)
for year in curyear:-1:2019
for month in 12:-1:1
ms = "0"^(1-div(month, 10)) * "$month"
base = joinpath("blog", "$year", "$ms")
isdir(base) || continue
posts = filter!(p -> endswith(p, ".md"), readdir(base))
days = zeros(Int, length(posts))
surls = Vector{String}(undef, length(posts))
for (i, post) in enumerate(posts)
ps = splitext(post)[1]
surl = "blog/$year/$ms/$ps"
surls[i] = surl
Franklin.PAGEVAR_DEPTH[] = 0
pubdate = pagevar(surl, :published)
days[i] = isnothing(pubdate) ?
1 : day(Date(pubdate, dateformat"d U Y"))
end
# go over month post in antichronological orders
sp = sortperm(days, rev=true)
for (i, surl) in enumerate(surls[sp])
recent[nfound + 1] = (surl => Date(year, month, days[sp[i]]))
nfound += 1
nfound == ntofind && break
end
nfound == ntofind && break
end
nfound == ntofind && break
end
#
io = IOBuffer()
for (surl, date) in recent
url = "/$surl/"
Franklin.PAGEVAR_DEPTH[] = 0
title = pagevar(surl, :title)
title === nothing && (title = "Untitled")
sdate = "$(day(date)) $(monthname(date)) $(year(date))"
Franklin.PAGEVAR_DEPTH[] = 0
blurb = pagevar(surl, :rss_description)
blurb = blurb === nothing ? "" : "<p>$blurb</p>"
write(io, """
<div class="col-lg-4 col-md-12 blog">
<h3><a href="$url" class="title" data-proofer-ignore>$title</a>
</h3><span class="article-date">$sdate</span>
$blurb
</div>
""")
end
return String(take!(io))
end
"""
{{redirect url}}
Creates a HTML layout for a redirect to `url`.
"""
function hfun_redirect(url)
s = """
<!-- REDIRECT -->
<!doctype html>
<html>
<head>
<meta http-equiv="refresh" content="0; url=$(url[1])">
</head>
</html>
"""
return s
end
function get_author_twitter()
meta = locvar(:meta)
if meta !== nothing
for (kind, tag, value) in meta
if kind == "name" && tag == "twitter:creator:id"
return "https://twitter.com/intent/user?user_id=$value"
end
end
end
return ""
end
"""
{{author_twitter}}
"""
function hfun_author_twitter()
url = get_author_twitter()
isempty(url) && return ""
return "<a href=\"$url\"><img src=\"/assets/infra/twitter.svg\"/ width=\"22px\" height=\"22px\" style=\"margin-left:2px\"></a>"
end
"""
{{about_the_author}}
"""
function hfun_about_the_author()
# verify that author_img and author_blurb are given
any(isnothing ∘ locvar, (:author_img, :author_blurb)) && return ""
img = "/assets/$(locvar(:author_img))"
twitter = get_author_twitter()
social = ""
if !isempty(twitter)
social *= """
<li class="author-social-link-twitter">
<a href=\"$twitter\"><i class="fa fa-twitter"></i></a>
</li>
"""
end
html = """
<div class="author-info">
<img src="$img" class="author-img" alt="$(locvar(:author))" width="150px">
<h3>$(locvar(:author))</h3>
<div class="author-description">
$(locvar(:author_blurb))
</div>
<div class="author-social">
<ul class="author-social-icons">
$social
</ul>
</div>
</div>
"""
return html
end
function hfun_all_gsoc_projects()
base_dir = joinpath("jsoc", "gsoc")
all_projects = readdir(base_dir)
md = IOBuffer()
for project in all_projects
project in ("general.md", "tooling.md", "graphics.md") && continue
endswith(project, ".md") || continue
write(md, read(joinpath(base_dir, project)))
write(md, "\n\n")
end
allmd = String(take!(md))
return fd2html(allmd, internal=true)
end