forked from IBBD/IBBD.github.io
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcreate_index
executable file
·178 lines (150 loc) · 4.51 KB
/
create_index
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
#!/bin/bash
#
# 生成index文件列表
#
# 索引文件
index_filename='0-index.md'
index_date_filename='0-index-date.md'
index_tag_filename='0-index-tag.md'
# 字典配置
declare -A dict
dict[database]=数据库
dict[front-end]=前端
dict[micro-services]=微服务
dict[elk]=ELK
dict[php]=PHP
dict[tech]=Tech
dict[golang]=Golang
dict[linux]=Linux
dict[data-science]=数据科学
dict[dsp]=DSP
dict[machine-learning]=机器学习
# 获取git的用户名与邮箱
user_name=$(git config user.name)
user_email=$(git config user.email)
############## 按目录排序 ######################
output_index() {
sortby=$1
if [ 'date' = $sortby ]; then
echo "[首页](https://github.com/IBBD/blog ) | [按目录索引]($index_filename ) | 按日期索引"
else
echo "[首页](https://github.com/IBBD/blog ) | 按目录索引 | [按修改日期索引]($index_date_filename )"
fi
}
files=$(find ./[0-9a-z_]*)
echo '## 文章目录 - 按目录索引' > $index_filename
echo >> $index_filename
echo ' Created by '$0' date: '$(date)>> $index_filename
echo >> $index_filename
output_index 'dir' >> $index_filename
echo >> $index_filename
# 递归输出一个目录
# @param 目录路径,值如:./linux/shell
output_dir() {
#echo 'output_dir '$1'================='
p=$1
f=${p##*/}
num=$(echo $1 | awk -F'/' '{print NF-1}')
num=$[($num-1)*2+1]
spaces=$(seq -s' ' $num | tr -d '[0-9]')
if [ -d $p ]; then
# 输出目录
if [ '.' != $p ]; then
if [ ${dict[$f]} ]
then
title=${dict[$f]}
else
title=$f
fi
echo "$spaces- $title" | tee -a $index_filename
fi
# 先处理子目录
list=$(find $1/[0-9a-zA-Z][0-9a-zA-Z\-_]* -maxdepth 0 -type d)
for child in $list
do
output_dir $child
done
# 处理子文件
# 这里使用变量p,会有问题,变量被覆盖了
list=$(find $1/*.md -maxdepth 0 -type f)
for child in $list
do
output_dir $child
done
else
# 输出文件
if [ 'README' != ${f%.*} -a $index_filename != ${child:2} -a $index_date_filename != ${child:2} ]; then
title=$(head -n 1 $p|sed 's/^#*\s*//'|sed 's/\s*$//')
[ ${#title} -lt 2 ] && title=${p%.*}
echo "$spaces- [$title](${p:2})" | tee -a $index_filename
fi
fi
}
output_dir .
echo '============= END OF 按目录索引 ================'
echo
echo
############## 按日期排序 ######################
echo '## 文章目录 - 按修改日期索引' > $index_date_filename
echo >> $index_date_filename
echo ' Created by '$0' date: '$(date)>> $index_date_filename
echo >> $index_date_filename
output_index 'date' >> $index_date_filename
echo >> $index_date_filename
# 记录按日期排序好的文件路径
# 每行记录一个文件
# 格式:日期;路径
tmp_date_index_file=.tmp_date_index_file
if [ ! -f $tmp_date_index_file ]; then
echo >> $tmp_date_index_file
fi
files=$(find ./ -name "*.md" | xargs ls -t)
for p in $files
do
f=${p##*/}
if [ '0-index' != ${f:0:7} -a 'README.md' != $f ]; then
# 判断文件是否已经在临时文件中
if
cat $tmp_date_index_file|grep "$p"
then
continue
fi
# 新文件
last_line=$(tail -n 1 $p)
file_date=$(ls $p --full-time|awk -F' ' '{print $6}')
if
echo $last_line|grep -P "201\d\-[01]\d\-[0123]\d"
then
file_date=${last_line:6:10}
fi
# 写入临时文件中
echo "$file_date;$p" >> $tmp_date_index_file
fi
done
# 临时文件进行排序
cat $tmp_date_index_file | sort -t";" -k1 -r | tee $tmp_date_index_file
# 生成最后的目录文件
file_date_last=
rows=$(cat $tmp_date_index_file)
for row in $rows
do
if [ ${#row} -lt 10 ]; then
continue
fi
# 判断是否需要输出日期
file_date=${row:0:10}
if [ "$file_date_last" != "$file_date" ]; then
echo | tee -a $index_date_filename
echo "#### $file_date" | tee -a $index_date_filename
echo | tee -a $index_date_filename
file_date_last=$file_date
fi
p=${row:11}
title=$(head -n 1 $p|sed 's/^#*\s*//'|sed 's/\s*$//')
[ ${#title} -lt 2 ] && title=${p%.*}
echo "- [$title](${p:2})" | tee -a $index_date_filename
done
echo
echo '============= END OF 按日期索引 ================'
echo
echo '====> finish!'