-
Notifications
You must be signed in to change notification settings - Fork 0
/
helper
executable file
·208 lines (176 loc) · 4.91 KB
/
helper
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
#!/usr/bin/env bash
# 构建 meta/objs.yml , 归档页等依赖
function build_objs_meta() {
SRC="$*"
rm -rf meta/*-layout.yml
for meta in $SRC; do
LAYOUT=$(yq .layout <"$meta" | sed 's/\"//g')
LAYOUT_RAW="meta/${LAYOUT}-layout.yml"
if [ ! -f "$LAYOUT_RAW" ]; then
echo "$LAYOUT:" >"$LAYOUT_RAW"
fi
sed 's/^/ /g' <"$meta" | sed '1s/^ /- /g' >>"$LAYOUT_RAW"
done
OUTPUT=meta/objs.yml
rm -f $OUTPUT && touch $OUTPUT
layouts=$(ls meta/*-layout.yml)
for file in $layouts; do
LAYOUT=$(echo "$file" | awk -F '[-/]' '{print $2}')
echo "$LAYOUT:" >>$OUTPUT
yq ".${LAYOUT}[]" <"$file" | jq -s -c 'sort_by(.modify_date, .date) | reverse' | yq -y . >>$OUTPUT
done
rm -rf meta/*-layout.yml
}
# 提取元信息
function build_meta() {
SRC=$1
OUTPUT=$2
NAME=$3
# 临时文件
OTT="${OUTPUT}.tmp"
# 创建目录
OUTPUT_DIR=$(dirname "$OUTPUT")
[[ ! -d "$OUTPUT_DIR" ]] && mkdir -p "$OUTPUT_DIR"
# 提取 meta
grep -n -E "^----*$" <"$SRC" | head -n 2 | awk -F ":" '{print $1}' | xargs | awk '{print $1+1 "," $2-1}' | xargs -I {} sed -n '{}p' "$SRC" >$OTT
META_YML=$(cat ${OTT})
# 默认非草稿
DRAFT=$(echo "$META_YML" | yq '.draft')
if [[ "true" != "$DRAFT" ]]; then
DRAFT="false"
fi
yq -i -y ".draft|=$DRAFT" $OTT
# 默认开启评论
COMMENT=$(echo "$META_YML" | yq '.comment')
if [[ "false" != "$COMMENT" ]]; then
COMMENT="true"
fi
yq -i -y ".comment|=$COMMENT" $OTT
# 默认开启 TOC
TOC=$(echo "$META_YML" | yq '.comment')
if [[ "false" != "$COMMENT" ]]; then
TOC="true"
fi
yq -i -y ".toc|=$TOC" $OTT
# 默认 LAYOUT 为当前文件所在目录名
LAYOUT=$(echo "$META_YML" | yq '.layout' | sed 's/\"//g')
if [ "$LAYOUT" == "null" ]; then
LAYOUT=$(echo "$NAME" | awk -F '/' '{print $1}')
fi
yq -i -y ".layout|=\"$LAYOUT\"" $OTT
# 链接 类似 /post/test.html
LINK="/$NAME.html"
yq -i -y ".link|=\"$LINK\"" $OTT
# TITLE 默认值文件名
TITLE=$(echo "$META_YML" | yq '.title')
if [[ "null" == "$TITLE" ]]; then
yq -i -y ".title|=\"$NAME\"" $OTT
fi
# 创建时间,默认 2020-01-01 00:00:00
C_AT=$(echo "$META_YML" | yq '.date' | sed 's/\"//g' | sed 's/T/ /g')
if [[ -z "$C_AT" ]] || [[ "null" == "$C_AT" ]]; then
C_AT="2020-01-01 00:00:00"
fi
C_SHORT_AT=${C_AT:0:10}
yq -i -y ".date|=\"$C_AT\"" $OTT
yq -i -y ".short_date|=\"$C_SHORT_AT\"" $OTT
# 修改时间,默认 git 的提交时间 -> 创建时间
M_AT=$(echo "$META_YML" | yq '.modify_date' | sed 's/\"//g' | sed 's/T/ /g')
if [[ -z "$M_AT" ]] || [[ "null" == "$M_AT" ]]; then
if [[ -d ".git" ]]; then
M_AT=$(git log -1 --format="%ci" -- "$SRC" | awk '{print $1,$2}')
fi
fi
if [[ -z "$M_AT" ]] || [[ "" == "$M_AT" ]]; then
M_AT=$C_AT
fi
M_SHORT_AT=${M_AT:0:10}
yq -i -y ".modify_date|=\"$M_AT\"" $OTT
yq -i -y ".modify_short_date|=\"$M_SHORT_AT\"" $OTT
mv $OTT $OUTPUT
}
# 构建 html
function build_obj() {
HIGHLIGHT=$1
THEME=$2
DIST_DIR=$3
SRC=$4
OUTPUT=$5
SRC_NAME=$6
# 输出目录
OUTPUT_DIR=$(dirname "$OUTPUT")
[[ ! -d "$OUTPUT_DIR" ]] && mkdir -p "$OUTPUT_DIR"
META_FILE="meta/$SRC_NAME.yml"
DRAFT=$(cat $META_FILE | yq .draft)
if [[ $DRAFT != "true" ]]; then
ARGS=" --highlight-style=${HIGHLIGHT} --from markdown --metadata-file basic.yml --metadata-file meta/${SRC_NAME}.yml -H ${DIST_DIR}/header.html -B ${DIST_DIR}/before.html -A ${DIST_DIR}/after.html -s"
# toc, 默认为 true
TOC=$(cat $META_FILE | yq .toc)
if [ $TOC != "false" ]; then
ARGS="$ARGS --toc"
fi
# layout
LAYOUT=$(cat $META_FILE | yq .layout | sed 's/\"//g')
if [ $LAYOUT == "null" ]; then
LAYOUT=$(echo $SRC_NAME | awk -F '/' '{print $1}')
fi
ARGS="$ARGS --template=templates/${THEME}/${LAYOUT}.template"
if [[ $LAYOUT == *"index" ]]; then
ARGS="$ARGS --metadata-file meta/objs.yml"
fi
pandoc $ARGS $SRC | sed 's/^ //g' >$OUTPUT
else
echo "" >$OUTPUT
fi
}
function usage() {
echo "Usage: ${0##*/} {build_objs_meta|new|build_meta|build_obj|serve|watch|help} "
exit 0
}
# ./helper new post test post -> 在 src/post/ 目录下创建 test-post.md 文件,并写入基本 metadata 。
function new() {
layout=$1
shift
NAME=$(echo "$*" | sed 's/ /-/g')
FILENAME=src/$layout/${NAME}.md
touch "$FILENAME"
echo '---' >"$FILENAME"
echo "title: $*" >>"$FILENAME"
C_AT=$(date '+%Y-%m-%d %H:%M:%S')
echo "date: $C_AT" >>"$FILENAME"
echo "draft: true" >>"$FILENAME"
echo '---' >>"$FILENAME"
echo "$*" >>"$FILENAME"
}
function watch_action() {
echo "build ..."
make all
chrome-cli reload
echo "build done"
}
function main() {
[ $# -gt 0 ] || usage
ACTION=$1
shift
case "$ACTION" in
build_objs_meta)
build_objs_meta "$@"
;;
new)
new "$@"
;;
build_obj)
build_obj "$@"
;;
build_meta)
build_meta "$@"
;;
watch)
watch_action
;;
*)
usage
;;
esac
}
main "$@"