-
Notifications
You must be signed in to change notification settings - Fork 15
/
utils.jl
143 lines (128 loc) · 4.05 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
using Dates
function hfun_youtube(params)
id = params[1]
return """
<div style=position:relative;padding-bottom:56.25%;height:0;overflow:hidden>
<iframe
src=https://www.youtube.com/embed/$id
style="position: absolute; top: 0; left: 0; width: 100%; height: 100%; border:0;"
allowfullscreen
title="YouTube Video">
</iframe>
</div>
"""
end
const MONTH = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul",
"Aug", "Sep", "Oct", "Nov", "Dec"]
function blogpost_name(fp)
lag = ifelse(endswith(fp, "index.md"), 1, 0)
return splitpath(fp)[end-lag]
end
function getdate(fname)
y, m, d, _ = split(fname, "-")
return parse.(Int, (y, m, d))
end
function hfun_redirect(params)
url = params[1]
# NOTE: we don't do a <meta> or <script> redirect, because that would
# end up in the RSS feed which some sites pick up verbatim.
return """<p>This post is located at <a href="$url">$url</a></p>"""
end
function hfun_post_date()
# capture the RSS publication date from the file name
fd_url = locvar(:fd_url)::String
d = Date(match(r"(20\d\d-\d\d-\d\d)", fd_url).captures[1])
Franklin.set_var!(Franklin.LOCAL_VARS, "rss_pubdate", d)
fname = blogpost_name(locvar(:fd_rpath)::String)
y, m, d = getdate(fname)
author = locvar(:author)
author_line = ""
if !isnothing(author)
author_line = """
<i data-feather=edit-2></i>
$author
"""
end
return """
<i data-feather=calendar></i>
<time datetime=$y-$m-$d>$(MONTH[m]) $d, $y</time><br>
$author_line
"""
end
function blogpost_entry_html(link, title, y, m, d; ext=false)
return """
<p>
<a class=font-125 href="$link">
$title
</a>$(ifelse(ext, "<span> ↗</span>", ""))
<br>
<i data-feather=calendar></i>
<time datetime=$y-$m-$d>$(MONTH[m]) $d, $y</time><br>
</p>
"""
end
function blogpost_entry(fpath)
rpath = joinpath("post", fpath)
if isdir(rpath)
rpath = joinpath(rpath, "index.md")
end
hidden = pagevar(rpath, :hidden)
if hidden === true
return nothing
end
ext = something(pagevar(rpath, :external), false)
title = pagevar(rpath, :title)::String
y, m, d = getdate(fpath)
rpath = replace(fpath, r"\.md$" => "")
date = Date(y, m, d)
return (date, blogpost_entry_html("/post/$rpath/", title, y, m, d; ext))
end
function blogpost_external_entries()
return [(d, blogpost_entry_html(l, t, year(d), month(d), day(d); ext=true))
for (d, t, l) in locvar(:external_entries)]
end
function hfun_blogposts()
io = IOBuffer()
elements = filter(readdir("post")) do entry
entry in ("index.md", ".DS_Store") && return false
return true
end
entries = [blogpost_entry(fp) for fp in elements]
entries = [e for e in entries if !isnothing(e)]
append!(entries, blogpost_external_entries())
sort!(entries, by=(e -> e[1]), rev=true)
for entry in entries
write(io, entry[2])
end
return String(take!(io))
end
function hfun_img(params)
img = params[1]
cap = params[2]
path = replace(locvar(:fd_url), "/index.html" => "/")
return """
<figure>
<img src="$(path)$img" alt="$cap">
</figure>
"""
end
function hfun_abstract()
abstract = locvar(:abstract)::String
descr = fd2html(abstract; internal=true, nop=true)
Franklin.set_var!(Franklin.LOCAL_VARS, "rss_description", descr)
return "<p>$descr</p>"
end
function hfun_postredirect()
fd_url = locvar(:fd_url)::String
path = replace(fd_url, "/post/" => "/")
return Franklin.hfun_redirect([path])
end
function hfun_rss_guid()
rss_guid = locvar("guid")
if rss_guid !== nothing
return rss_guid::String
else
# Hugo-style GUID without `/post/` or `index.html`
return replace(replace(locvar(:fd_full_url), r"index.html$" => ""), "/post/" => "/")
end
end