-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild
executable file
·103 lines (84 loc) · 2.88 KB
/
build
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
#!/usr/bin/env bash
shopt -s globstar
# get array of all posts, also used to pick latest post for home page
declare -a unsorted_posts
for post in $(ls -t posts/*.md); do
if [[ $post != "posts/index.md" ]]; then
# get the front matter
post_fm=$(sed -n '/^---$/,/^---$/p' "${post}")
# skip if draft
if [[ ${post_fm} == *"draft: true"* ]]; then
echo "Skipping draft ${post}"
continue
fi
post_date=$(sed -n 's/^date: //p' <<< "${post_fm}")
if [[ -z ${post_date} ]]; then
echo "Error: no date in frontmatter of ${post}"
continue
fi
post_name=$(basename $post)
output_name=${post_name%*.md}
post_name="${post_date}_${post_name}"
unsorted_posts+=(${post_name})
pandoc -s "${post}" -o "posts/${output_name}.html" --template assets/template.html \
--variable assets-folder="../assets" || \
echo "Error: failed to build ${md}"
fi
done
# sort all posts by created date, also used to get latest post
declare -a sorted_posts
IFS=$'\n' sorted_posts=($(sort -r <<< ${unsorted_posts[*]}))
unset IFS
declare -a latest_posts
for post in ${sorted_posts[*]}; do
name_to_add="${post/*_/}"
name_to_add="${name_to_add%.md}"
# echo ${name_to_add}
latest_posts+=("--variable latest-posts=${name_to_add%*.md}")
done
# don't use latest post, use featured post instead
featured_post="chatgpt-site-improvements.md"
# latest_post="${sorted_posts[0]/*_/}"
# echo "${latest_post}"
# latest_post_text="$(pandoc posts/${latest_post} --template assets/post-preview-template.html --variable post-link=${latest_post%.md})"
featured_post_text="$(pandoc posts/${featured_post} --template assets/post-preview-template.html --variable post-link=${featured_post%.md})"
# echo "${featured_post_text}"
for md in **/*.md; do
dir=$(dirname $md)
filename=$(basename $md)
filename=${filename%*.md}
assets_folder="assets"
if [[ ${dir} != "." ]]; then
assets_folder="../assets"
fi
# don't build project readme
if [[ ${md} == "README.md" ]]; then
continue
fi
# build homepage
if [[ ${md} == "index.md" ]]; then
echo "building ${md}"
pandoc -s "${md}" -o "${dir}"/index.html --template assets/template.html \
--variable assets-folder="${assets_folder}" \
--variable post-preview="${featured_post_text}"
continue
fi
# build posts page
if [[ ${md} == "posts/index.md" ]]; then
echo "building ${md}"
pandoc -s "${md}" -o "${dir}"/index.html --template assets/template.html \
--variable assets-folder="${assets_folder}" \
${latest_posts[*]} || \
echo "Error: failed to build ${md}"
continue
fi
# posts already built
if [[ ${dir} == "posts" ]]; then
continue
fi
# all other pages
echo "building ${md}"
pandoc -s "${md}" -o "${dir}"/${filename}.html --template assets/template.html \
--variable assets-folder="${assets_folder}" || \
echo "Error: failed to build ${md}"
done