-
Notifications
You must be signed in to change notification settings - Fork 0
/
newpost.sh
executable file
·372 lines (294 loc) · 8.26 KB
/
newpost.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
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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
#!/bin/bash
#
# Description:
#
# This script creates two new blog post with metadata, one in English and the
# other in Chinese. They are stored in two different directories for two
# different collections:
#
# - EN: _posts/
# - CN: _cn/
#
# Both articles mush the same name because the internationalization (i18n)
# system relies on this name to find the other version, if exists.
# Date will be generated according to the current time in the system.
#
# Functions
# -----
function print_usage {
cat << EOF
Usage:
newpost.sh [OPTIONS] TITLE
Options:
-d,--debug Print debug logs
-h,--help Show help
-e,--en Generate English post
-c,--cn Generate Chinese post
-a,--all Generate post in all languages (English and Chinese)
--qna Generate post using a Q&A (Question and Answer) template
Examples:
newpost.sh My Post Title
newpost.sh --all My Post Title
newpost.sh -a My Post Title
newpost.sh --en My Post Title
newpost.sh --cn My Post Title
newpost.sh -h
EOF
}
function append_metadata_en {
path="$1"
title="$2"
post_type="$3"
cat << EOF >> "$path"
---
article_num: ${article_num_curr}
layout: post
type: ${post_type}
title: ${title}
subtitle: >
Given one sentence to expand the title or explain why this article may interest your readers.
lang: en
date: $(date +"%Y-%m-%d %H:%M:%S %z")
categories: [elasticsearch]
tags: [elasticsearch]
comments: true
excerpt: >
TODO
image: /assets/bg-coffee-84624_1280.jpg
cover: /assets/bg-coffee-84624_1280.jpg
article_header:
type: overlay
theme: dark
background_color: "#203028"
background_image:
gradient: "linear-gradient(135deg, rgba(0, 0, 0, .6), rgba(0, 0, 0, .4))"
wechat: false
---
EOF
}
function append_metadata_cn {
path="$1"
title="$2"
post_type="$3"
cat << EOF >> "$path"
---
article_num: ${article_num_curr}
layout: post
type: ${post_type}
title: ${title}
subtitle: >
Given one sentence to expand the title or explain why this article may interest your readers.
lang: zh
date: $(date +"%Y-%m-%d %H:%M:%S %z")
categories: [java-core]
tags: []
comments: true
excerpt: >
Try to limit at 140 character, two lines: (80-4) *2 = 152
image: /assets/bg-coffee-84624_1280.jpg
cover: /assets/bg-coffee-84624_1280.jpg
article_header:
type: overlay
theme: dark
background_color: "#203028"
background_image:
gradient: "linear-gradient(135deg, rgba(0, 0, 0, .6), rgba(0, 0, 0, .4))"
wechat: true
---
EOF
}
function append_qna_content_en {
cat << EOF >> "$1"
## Question
Describe the question here: what am I trying to do? What did I try (so that
people can understand it may be confusing and requires explanation in the answer)?
Why is this question useful to motivate people to read the answer.
## Answer
Provide some text to explain what is it, requirements, how does it work, etc.
```java
// Then follow by a code snippet to provide a concrete example
```
Explain the example a bit or continue the answer here.
## Going Further
How to go further from here?
- Other answers that we didn't see
- More context about the framework
- Other tricks regarding the question
Hope you enjoy this article, see you the next time!
## References
EOF
}
function append_classic_content_en {
cat << EOF >> "$1"
## Introduction
Explain context here to attract people's attention... like:
- topic: what you want to talk about?
- audience: who are you targeting?
- motiviation: why is it interesting for them? Or why is it important to understand this topic?
After reading this article, you will understand:
(choose one of the following structures)
dive deep:
* Some prequisites
* Some general concepts
* Some specific concepts to dig deeper
* How to go further from this article
different dimensions (broad):
* section 1
* section 2
* section 3
* How to go further from this article
... then specify some information about the context, such as framework version, language version.
Now, let's get started!
## Section 1
## Section 2
## Section 3
## Going Further
How to go further from here?
## Conclusion
What did we talk in this article? Take notes from introduction again.
Interested to know more? You can subscribe to [the feed of my blog](/feed.xml), follow me
on [Twitter](https://twitter.com/mincong_h) or
[GitHub](https://github.com/mincong-h/). Hope you enjoy this article, see you the next time!
## References
EOF
}
function append_content_cn {
cat << EOF >> "$1"
<!--
Replace asset link with following on Chinese Platforms:
https://github.com/mincong-h/mincong-h.github.io/raw/master/
-->
## 前言
为什么读者想读这篇文章?
阅读本文后,你会明白:
事不宜迟,让我们马上开始吧!
## 扩展
如何从这篇文章中拓展出去?
## 结论
在本文中,我们看到了。。。最后,我们还简要讨论了其他的场景,并且分享了一些让大家拓展出去的资源。希望这篇文章能够给你带来一些思考,让你的系统变得。。。如果你有兴趣了解更多的资讯,欢迎关注我的 GitHub 账号 [mincong-h](https://github.com/mincong-h "GitHub") 或者微信订阅号【码农小黄】。谢谢大家!
## 参考文献
<!--
WeChat:
原创不易,希望大家点个赞、点个在看支持一下,谢谢!
![](https://mincong.io/assets/wechat-QR-code.jpg)
CSDN:
![扫码关注](https://img-blog.csdnimg.cn/img_convert/f07c6cc9272c721180bad20c599e4ff7.png#pic_center =600x333)
-->
EOF
}
# Main
# -----
title="${*:1}"
if [[ -z "$title" ]]
then
print_usage
exit 1
fi
bloghome=$(cd "$(dirname "$0")" || exit; pwd)
create_en=1
create_cn=0
debug=0
post_type="classic"
article_num_prev="$(cat "${bloghome}/.article_num" | xargs)"
article_num_curr="$((article_num_prev + 1))"
for i in "$@"
do
case $i in
-a|--all)
create_en=1
create_cn=1
shift
;;
-e|--en)
create_en=1
create_cn=0
shift
;;
-c|--cn)
create_en=0
create_cn=1
shift
;;
-d|--debug)
debug=1
shift
;;
-h|--help)
print_usage
exit 0
;;
--qna)
post_type="Q&A"
shift
;;
*)
title="${@}"
;;
esac
done
url=$(echo "$title" | tr '[:upper:]' '[:lower:]' | tr ' ' '-')
filepath_en="${bloghome}/_posts/$(date +"%Y-%m-%d")-${url}.md"
filepath_cn="${bloghome}/_cn/${url}.md"
if [[ $debug -eq "1" ]]
then
cat << EOF
create_en=${create_en}
create_cn=${create_cn}
filepath_en=${filepath_en}
filepath_cn=${filepath_cn}
debug=${debug}
title=${title}
url=${url}
post_type=${post_type}
article_num_prev=${article_num_prev}
article_num_curr=${article_num_curr}
EOF
fi
if [[ $create_en -eq "1" ]]
then
if [[ -f "$filepath_en" ]]
then
echo "${filepath_en} already exists."
exit 1
fi
append_metadata_en "$filepath_en" "$title" "$post_type"
case $post_type in
qna)
append_qna_content_en "$filepath_en" ;;
classic)
append_classic_content_en "$filepath_en" ;;
*)
esac
fi
if [[ $create_cn -eq "1" ]]
then
if [[ -f "$filepath_cn" ]]; then
echo "${filepath_cn} already exists."
exit 1
fi
append_metadata_cn "$filepath_cn" "$title" "$post_type"
append_content_cn "$filepath_cn"
fi
if [[ $create_en -eq "1" && $create_cn -eq "1" ]]
then
cat << EOF
Blog post (${post_type}) created!
EN: ${filepath_en}
CN: ${filepath_cn}
EOF
elif [[ $create_en -eq "1" ]]
then
cat << EOF
Blog post (${post_type}) created!
EN: ${filepath_en}
CN: (disabled)
EOF
else
cat << EOF
Blog post (${post_type}) created!
EN: (disabled)
CN: ${filepath_cn}
EOF
fi
# Increment article number
echo "$article_num_curr" > "${bloghome}/.article_num"