forked from cotes2020/jekyll-theme-chirpy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
create_pages.sh
executable file
·162 lines (120 loc) · 3.14 KB
/
create_pages.sh
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
#!/usr/local/bin/bash
#
# Create HTML pages for Categories and Tags in posts.
#
# Usage:
# Call from the '_posts' sibling directory.
#
# v2.2
# https://github.com/cotes2020/jekyll-theme-chirpy
# © 2020 Cotes Chung
# Published under MIT License
set -eu
TYPE_CATEGORY=0
TYPE_TAG=1
category_count=0
tag_count=0
_read_yaml() {
local _endline="$(grep -n "\-\-\-" "$1" | cut -d: -f 1 | sed -n '2p')"
head -"$_endline" "$1"
}
read_categories() {
local _yaml="$(_read_yaml "$1")"
local _categories="$(echo "$_yaml" | grep "^categories:")"
local _category="$(echo "$_yaml" | grep "^category:")"
if [[ ! -z "$_categories" ]]; then
echo "$_categories" | sed "s/categories: *//;s/\[//;s/\].*//;s/, */,/g;s/\"//g;s/'//g"
elif [[ ! -z "_category" ]]; then
echo "$_category" | sed "s/category: *//;s/\[//;s/\].*//;s/, */,/g;s/\"//g;s/'//g"
fi
}
read_tags() {
local _yaml="$(_read_yaml "$1")"
echo "$_yaml" | grep "^tags:" | sed "s/tags: *//;s/\[//;s/\].*//;s/, */,/g;s/\"//g;s/'//g"
}
init() {
if [[ -d categories ]]; then
rm -rf categories
fi
if [[ -d tags ]]; then
rm -rf tags
fi
if [[ ! -d _posts ]]; then
exit 0
fi
mkdir categories tags
}
create_category() {
if [[ ! -z $1 ]]; then
local _name=$1
local _filepath="categories/$(echo "$_name" | sed 's/ /-/g' | awk '{print tolower($0)}').html"
if [[ ! -f "$_filepath" ]]; then
echo "---" > "$_filepath"
echo "layout: category" >> "$_filepath"
echo "title: $_name" >> "$_filepath"
echo "category: $_name" >> "$_filepath"
echo "---" >> "$_filepath"
((category_count=category_count+1))
fi
fi
}
create_tag() {
if [[ ! -z $1 ]]; then
local _name=$1
local _filepath="tags/$( echo "$_name" | sed "s/ /-/g;s/'//g" | awk '{print tolower($0)}' ).html"
if [[ ! -f "$_filepath" ]]; then
echo "---" > "$_filepath"
echo "layout: tag" >> "$_filepath"
echo "title: $_name" >> "$_filepath"
echo "tag: $_name" >> "$_filepath"
echo "---" >> "$_filepath"
((tag_count=tag_count+1))
fi
fi
}
#########################################
# Create HTML pages for Categories/Tags.
# Arguments:
# $1 - an array string
# $2 - type specified option
#########################################
create_pages() {
if [[ ! -z $1 ]]; then
# split string to array
IFS_BAK=$IFS
IFS=','
local _string=$1
case $2 in
$TYPE_CATEGORY)
for i in ${_string#,}; do
create_category "$i"
done
;;
$TYPE_TAG)
for i in ${_string#,}; do
create_tag "$i"
done
;;
*)
;;
esac
IFS=$IFS_BAK
fi
}
main() {
init
for _file in $(find "_posts" -type f \( -iname \*.md -o -iname \*.markdown \))
do
local _categories=$(read_categories "$_file")
local _tags=$(read_tags "$_file")
create_pages "$_categories" $TYPE_CATEGORY
create_pages "$_tags" $TYPE_TAG
done
if [[ $category_count -gt 0 ]]; then
echo "[INFO] Succeed! $category_count category-pages created."
fi
if [[ $tag_count -gt 0 ]]; then
echo "[INFO] Succeed! $tag_count tag-pages created."
fi
}
main