Skip to content

Commit 0917131

Browse files
committed
Pretty shell format.
1 parent 19eb18d commit 0917131

7 files changed

Lines changed: 50 additions & 88 deletions

File tree

_scripts/sh/create_pages.sh

Lines changed: 14 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -18,32 +18,28 @@ TYPE_TAG=1
1818
category_count=0
1919
tag_count=0
2020

21-
2221
_read_yaml() {
2322
local _endline="$(grep -n "\-\-\-" "$1" | cut -d: -f 1 | sed -n '2p')"
2423
head -"$_endline" "$1"
2524
}
2625

27-
2826
read_categories() {
2927
local _yaml="$(_read_yaml "$1")"
3028
local _categories="$(echo "$_yaml" | grep "^categories *:")"
3129
local _category="$(echo "$_yaml" | grep "^category *:")"
3230

33-
if [[ ! -z "$_categories" ]]; then
31+
if [[ -n $_categories ]]; then
3432
echo "$_categories" | sed "s/categories *: *//;s/\[//;s/\].*//;s/, */,/g;s/\"//g;s/'//g"
35-
elif [[ ! -z "_category" ]]; then
33+
elif [[ -n "_category" ]]; then
3634
echo "$_category" | sed "s/category *: *//;s/\[//;s/\].*//;s/, */,/g;s/\"//g;s/'//g"
3735
fi
3836
}
3937

40-
4138
read_tags() {
4239
local _yaml="$(_read_yaml "$1")"
4340
echo "$_yaml" | grep "^tags *:" | sed "s/tags *: *//;s/\[//;s/\].*//;s/, */,/g;s/\"//g;s/'//g"
4441
}
4542

46-
4743
init() {
4844

4945
if [[ -d categories ]]; then
@@ -61,52 +57,49 @@ init() {
6157
mkdir categories tags
6258
}
6359

64-
6560
create_category() {
66-
if [[ ! -z $1 ]]; then
61+
if [[ -n $1 ]]; then
6762
local _name=$1
6863
local _filepath="categories/$(echo "$_name" | sed 's/ /-/g' | awk '{print tolower($0)}').html"
6964

70-
if [[ ! -f "$_filepath" ]]; then
65+
if [[ ! -f $_filepath ]]; then
7166
echo "---" > "$_filepath"
7267
echo "layout: category" >> "$_filepath"
7368
echo "title: $_name" >> "$_filepath"
7469
echo "category: $_name" >> "$_filepath"
7570
echo "---" >> "$_filepath"
7671

77-
((category_count=category_count+1))
72+
((category_count = category_count + 1))
7873
fi
7974
fi
8075
}
8176

82-
8377
create_tag() {
84-
if [[ ! -z $1 ]]; then
78+
if [[ -n $1 ]]; then
8579
local _name=$1
86-
local _filepath="tags/$( echo "$_name" | sed "s/ /-/g;s/'//g" | awk '{print tolower($0)}' ).html"
80+
local _filepath="tags/$(echo "$_name" | sed "s/ /-/g;s/'//g" | awk '{print tolower($0)}').html"
8781

88-
if [[ ! -f "$_filepath" ]]; then
82+
if [[ ! -f $_filepath ]]; then
8983

9084
echo "---" > "$_filepath"
9185
echo "layout: tag" >> "$_filepath"
9286
echo "title: $_name" >> "$_filepath"
9387
echo "tag: $_name" >> "$_filepath"
9488
echo "---" >> "$_filepath"
9589

96-
((tag_count=tag_count+1))
90+
((tag_count = tag_count + 1))
9791
fi
9892
fi
9993
}
10094

101-
10295
#########################################
10396
# Create HTML pages for Categories/Tags.
10497
# Arguments:
10598
# $1 - an array string
10699
# $2 - type specified option
107100
#########################################
108101
create_pages() {
109-
if [[ ! -z $1 ]]; then
102+
if [[ -n $1 ]]; then
110103
# split string to array
111104
IFS_BAK=$IFS
112105
IFS=','
@@ -126,23 +119,20 @@ create_pages() {
126119
done
127120
;;
128121

129-
*)
130-
;;
122+
*) ;;
131123

132-
esac
124+
esac
133125

134-
IFS=$IFS_BAK
126+
IFS=$IFS_BAK
135127
fi
136128

137129
}
138130

139-
140131
main() {
141132

142133
init
143134

144-
for _file in $(find "_posts" -type f \( -iname \*.md -o -iname \*.markdown \))
145-
do
135+
for _file in $(find "_posts" -type f \( -iname \*.md -o -iname \*.markdown \)); do
146136
local _categories=$(read_categories "$_file")
147137
local _tags=$(read_tags "$_file")
148138

_scripts/sh/dump_lastmod.sh

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ POST_DIR=_posts
1717
OUTPUT_DIR=_data
1818
OUTPUT_FILE=updates.yml
1919

20-
2120
_init() {
2221

2322
if [[ ! -d "$OUTPUT_DIR" ]]; then
@@ -33,7 +32,6 @@ _init() {
3332
fi
3433
}
3534

36-
3735
_has_changed() {
3836
local _log_count="$(git log --pretty=%ad "$1" | wc -l | sed 's/ *//')"
3937
_log_count=$((_log_count + 0))
@@ -45,7 +43,6 @@ _has_changed() {
4543
return 1 # false
4644
}
4745

48-
4946
###################################
5047
# Storage the posts' lastmod.
5148
#
@@ -67,20 +64,18 @@ _dump() {
6764
echo " lastmod: '$_lasmod'" >> "$OUTPUT_DIR/$OUTPUT_FILE"
6865
}
6966

70-
7167
main() {
7268

7369
_init
7470

7571
local _count=0
7672

77-
for _file in $(find ${POST_DIR} -type f \( -iname \*.md -o -iname \*.markdown \))
78-
do
79-
_filename="$(basename "$_file" | sed 's/-\-\+/-/;s/[[:digit:]]\([[:digit:]]*-\)//g;s/\..*//' )" # remove date and extension
73+
for _file in $(find ${POST_DIR} -type f \( -iname \*.md -o -iname \*.markdown \)); do
74+
_filename="$(basename "$_file" | sed 's/-\-\+/-/;s/[[:digit:]]\([[:digit:]]*-\)//g;s/\..*//')" # remove date and extension
8075

8176
if _has_changed "$_file"; then
8277
_dump "$_filename" "$_file"
83-
((_count=_count+1))
78+
((_count = _count + 1))
8479
fi
8580

8681
done
@@ -90,5 +85,4 @@ main() {
9085
fi
9186
}
9287

93-
9488
main

_scripts/sh/sync_monitor.sh

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
# $3 -> the destination sync directory
1212

1313
# Omit the system temp file
14-
if [[ ! -f "$1" ]]; then
14+
if [[ ! -f $1 ]]; then
1515
exit 0
1616
fi
1717

@@ -21,18 +21,17 @@ dir_prefix="$(realpath "$2")/"
2121

2222
related_dir="${src_dir:${#dir_prefix}}"
2323

24-
2524
dest="$(realpath "$3")/${related_dir}"
2625

27-
if [[ ! -d "$dest" ]]; then
26+
if [[ ! -d $dest ]]; then
2827
mkdir -p "$dest"
2928
fi
3029

31-
if [[ -f "$1" ]]; then
30+
if [[ -f $1 ]]; then
3231
cp "$1" "$dest"
3332
fi
3433

35-
if [[ "$related_dir" == "_posts" ]]; then
34+
if [[ $related_dir == "_posts" ]]; then
3635
bash "$3"/_scripts/sh/create_pages.sh
3736
bash "$3"/_scripts/sh/dump_lastmod.sh
3837
fi

tools/deploy.sh

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
# © 2020 Cotes Chung
88
# Published under MIT License
99

10-
1110
set -eu
1211

1312
PAGES_BRANCH="gh-pages"
@@ -28,11 +27,11 @@ backup() {
2827
mv _site "$_backup_dir"
2928
mv .git "$_backup_dir"
3029

31-
# When adding custom domain from Github website,
30+
# When adding custom domain from Github website,
3231
# the CANME only exist on `gh-pages` branch
33-
if [[ -f CNAME ]]; then
32+
if [[ -f CNAME ]]; then
3433
mv CNAME "$_backup_dir"
35-
fi
34+
fi
3635
}
3736

3837
flush() {
@@ -51,7 +50,7 @@ deoply() {
5150
git add -A
5251
git commit -m "[Automation] Site update No.${GITHUB_RUN_NUMBER}"
5352

54-
if [[ $_no_branch = true ]]; then
53+
if $_no_branch; then
5554
git push -u origin "$PAGES_BRANCH"
5655
else
5756
git push -f

tools/init.sh

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99

1010
set -eu
1111

12-
1312
ACTIONS_WORKFLOW=pages-deploy.yml
1413

1514
help() {
@@ -26,24 +25,23 @@ check_init() {
2625
local _has_inited=false
2726

2827
if [[ -d .github ]]; then
29-
if [[ -f .github/workflows/$ACTIONS_WORKFLOW
30-
&& $(find .github/workflows/ -type f -name "*.yml" | wc -l) == 1 ]]; then
28+
if [[ -f .github/workflows/$ACTIONS_WORKFLOW && \
29+
$(find .github/workflows/ -type f -name "*.yml" | wc -l) == 1 ]]; then
3130
_has_inited=true
3231
fi
3332
else
3433
_has_inited=true
3534
fi
3635

37-
if [[ $_has_inited = true ]]; then
36+
if $_has_inited; then
3837
echo "Already initialized."
3938
exit 0
4039
fi
4140
}
4241

43-
4442
init_files() {
4543

46-
if [[ $_no_gh = true ]]; then
44+
if $_no_gh; then
4745
rm -rf .github
4846
else
4947
mv .github/workflows/$ACTIONS_WORKFLOW.hook .
@@ -55,34 +53,32 @@ init_files() {
5553
rm -f .travis.yml
5654
rm -rf _posts/* docs
5755

58-
git add -A && git add .github -f
56+
git add -A && git add .github -f
5957
git commit -m "[Automation] Initialize the environment." -q
6058

6159
echo "[INFO] Initialization successful!"
6260
}
6361

64-
6562
check_init
6663

6764
_no_gh=false
6865

69-
while (( $# ))
70-
do
66+
while (($#)); do
7167
opt="$1"
7268
case $opt in
7369
--no-gh)
7470
_no_gh=true
7571
shift
7672
;;
77-
-h|--help)
73+
-h | --help)
7874
help
7975
exit 0
8076
;;
81-
*)
82-
# unknown option
83-
help
84-
exit 1
85-
;;
77+
*)
78+
# unknown option
79+
help
80+
exit 1
81+
;;
8682
esac
8783
done
8884

tools/publish.sh

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ LASTMOD=false
1515

1616
WORK_DIR="$(dirname "$(dirname "$(realpath "$0")")")"
1717

18-
1918
check_status() {
2019
local _change=$(git status . -s)
2120

@@ -26,13 +25,11 @@ check_status() {
2625
fi
2726
}
2827

29-
3028
update_files() {
3129
bash _scripts/sh/create_pages.sh
3230
bash _scripts/sh/dump_lastmod.sh
3331
}
3432

35-
3633
commit() {
3734
msg="Updated"
3835

@@ -44,7 +41,7 @@ commit() {
4441

4542
if [[ ! -z $(git status tags -s) ]]; then
4643
git add tags/
47-
if [[ $CATEGORIES = true ]]; then
44+
if $CATEGORIES; then
4845
msg+=","
4946
else
5047
msg+=" the"
@@ -53,9 +50,9 @@ commit() {
5350
TAGS=true
5451
fi
5552

56-
if [[ ! -z $(git status _data -s) ]]; then
53+
if [[ -n $(git status _data -s) ]]; then
5754
git add _data
58-
if [[ $CATEGORIES = true || $TAGS = true ]]; then
55+
if $CATEGORIES || $TAGS; then
5956
msg+=","
6057
else
6158
msg+=" the"
@@ -64,7 +61,7 @@ commit() {
6461
LASTMOD=true
6562
fi
6663

67-
if [[ $CATEGORIES = true || $TAGS = true || $LASTMOD = true ]]; then
64+
if $CATEGORIES || $TAGS || $LASTMOD; then
6865
msg+=" for post(s)."
6966
git commit -m "[Automation] $msg" -q
7067
else
@@ -73,13 +70,11 @@ commit() {
7370

7471
}
7572

76-
7773
push() {
7874
git push origin master -q
7975
echo "[INFO] Published successfully!"
8076
}
8177

82-
8378
main() {
8479

8580
cd "$WORK_DIR"
@@ -93,5 +88,4 @@ main() {
9388
push
9489
}
9590

96-
9791
main

0 commit comments

Comments
 (0)