-
-
Notifications
You must be signed in to change notification settings - Fork 37
/
utils.jl
117 lines (110 loc) · 3.53 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
using Dates
"""
{{meta}}
Plug in specific meta information for a blog page. The `meta` local page
variable should be given as a list of tuples of pairs like so:
```
@def meta = [("property"=>"og:video", "content"=>"http://example.com/"),
("propery"=>"og:title", "content"=>"The Rock")]
```
"""
function hfun_meta()
m = locvar(:meta)
io = IOBuffer()
for tuple in locvar(:meta)
write(io, "<meta ")
for (prop, val) in tuple
write(io, "$prop=\"$val\" ")
end
write(io, ">")
end
return String(take!(io))
end
function two_digits(n::Int)::String
pre = ifelse(div(n, 10) == 0, "0", "")
return "$pre$n"
end
"""
{{blogposts}}
Plug in the list of blog posts contained in the `/blog/` folder.
"""
@delay function hfun_blogposts()
curyear = Dates.Year(Dates.today()).value
io = IOBuffer()
for year in curyear:-1:2016
ys = "$year"
year < curyear && write(io, "\n**$year**\n")
for month in 12:-1:1
ms = two_digits(month)
# if you have an additional level of structure...
for day in 31:-1:1
ds = two_digits(day)
# check the folder
base = joinpath("news", ys, ms, ds)
isdir(base) || continue
# check the posts in the folder
posts = filter!(p -> endswith(p, ".md"), readdir(base))
for post in posts
ps = splitext(post)[1]
url = "/news/$ys/$ms/$ds/$ps/"
surl = strip(url, '/')
title = pagevar(surl, :title)
author = pagevar(surl, :author)
isnothing(author) && (author = "")
pubdate = "$ys-$ms-$ds"
write(io, "\n[$title]($url) $author ($pubdate) \n")
end
end
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
function redirect(url)
s = """
<!-- REDIRECT - DO NOT MODIFY-->
<!doctype html>
<html>
<head>
<meta http-equiv="refresh" content="0; url=$url">
</head>
</html>
"""
return s
end
function hfun_blog_post_redirects()
basepath = Franklin.FOLDER_PATH[]
for (root, _, files) in walkdir(joinpath(basepath, "news"))
for file in files
fullpath = joinpath(root, file)
fullpath == joinpath(basepath, "news", "index.md") && continue
# relative path: /news/...
relpath = splitext(fullpath[length(basepath)+1:end])[1]
dst = joinpath(basepath, "__site", strip(relpath, '/') * ".html")
isfile(dst) && continue
mkpath(splitdir(dst)[1])
write(dst, redirect(relpath * "/"))
end
end
return ""
end
function hfun_add_redirects()
basepath = Franklin.FOLDER_PATH[]
for n in readdir(basepath)
endswith(n, ".md") || continue
n in ("index.md", "config.md") && continue
name = splitext(n)[1]
dst = joinpath(basepath, "__site", name * ".html")
isfile(dst) && continue
prepath = globvar("prepath")
pre = ""
if prepath !== nothing && !isempty(prepath)
pre = "/" * strip(prepath, '/')
end
s = redirect("$pre/$name/")
write(dst, s)
end
return ""
end