Skip to content

Commit add170b

Browse files
committed
Improved script tools.
- Allows posts to be placed in subdirectories (cotes2020#41, cotes2020#87). - Identify posts file with the suffix ‘.markdown’. **Page creator** - Pass the posts without any categories or tags. - Omit the YAML comments for categories/tags. **Lastmod** - compatible with one-digit month or day post files.
1 parent 45628d8 commit add170b

2 files changed

Lines changed: 56 additions & 56 deletions

File tree

_scripts/sh/create_pages.sh

Lines changed: 52 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -31,16 +31,16 @@ read_categories() {
3131
local _category=$(echo "$_yaml" | grep "^category:")
3232

3333
if [[ ! -z "$_categories" ]]; then
34-
echo "$_categories" | sed "s/categories: *//;s/\[//;s/\]//;s/, */,/g;s/\"//g;s/'//g"
34+
echo "$_categories" | sed "s/categories: *//;s/\[//;s/\].*//;s/, */,/g;s/\"//g;s/'//g"
3535
elif [[ ! -z "_category" ]]; then
36-
echo "$_category" | sed "s/category: *//;s/\[//;s/\]//;s/, */,/g;s/\"//g;s/'//g"
36+
echo "$_category" | sed "s/category: *//;s/\[//;s/\].*//;s/, */,/g;s/\"//g;s/'//g"
3737
fi
3838
}
3939

4040

4141
read_tags() {
4242
local _yaml=$(_read_yaml $1)
43-
echo "$_yaml" | grep "^tags:" | sed "s/tags: *//;s/\[//;s/\]//;s/, */,/g;s/\"//g;s/'//g"
43+
echo "$_yaml" | grep "^tags:" | sed "s/tags: *//;s/\[//;s/\].*//;s/, */,/g;s/\"//g;s/'//g"
4444
}
4545

4646

@@ -63,34 +63,38 @@ init() {
6363

6464

6565
create_category() {
66-
local _name=$1
67-
local _filepath="categories/$(echo $_name | sed 's/ /-/g' | awk '{print tolower($0)}').html"
68-
69-
if [[ ! -f $_filepath ]]; then
70-
echo "---" > $_filepath
71-
echo "layout: category" >> $_filepath
72-
echo "title: $_name" >> $_filepath
73-
echo "category: $_name" >> $_filepath
74-
echo "---" >> $_filepath
75-
76-
((category_count=category_count+1))
66+
if [[ ! -z $1 ]]; then
67+
local _name=$1
68+
local _filepath="categories/$(echo $_name | sed 's/ /-/g' | awk '{print tolower($0)}').html"
69+
70+
if [[ ! -f $_filepath ]]; then
71+
echo "---" > $_filepath
72+
echo "layout: category" >> $_filepath
73+
echo "title: $_name" >> $_filepath
74+
echo "category: $_name" >> $_filepath
75+
echo "---" >> $_filepath
76+
77+
((category_count=category_count+1))
78+
fi
7779
fi
7880
}
7981

8082

8183
create_tag() {
82-
local _name=$1
83-
local _filepath="tags/$( echo $_name | sed "s/ /-/g;s/'//g" | awk '{print tolower($0)}' ).html"
84+
if [[ ! -z $1 ]]; then
85+
local _name=$1
86+
local _filepath="tags/$( echo $_name | sed "s/ /-/g;s/'//g" | awk '{print tolower($0)}' ).html"
8487

85-
if [[ ! -f $_filepath ]]; then
88+
if [[ ! -f $_filepath ]]; then
8689

87-
echo "---" > $_filepath
88-
echo "layout: tag" >> $_filepath
89-
echo "title: $_name" >> $_filepath
90-
echo "tag: $_name" >> $_filepath
91-
echo "---" >> $_filepath
90+
echo "---" > $_filepath
91+
echo "layout: tag" >> $_filepath
92+
echo "title: $_name" >> $_filepath
93+
echo "tag: $_name" >> $_filepath
94+
echo "---" >> $_filepath
9295

93-
((tag_count=tag_count+1))
96+
((tag_count=tag_count+1))
97+
fi
9498
fi
9599
}
96100

@@ -102,47 +106,45 @@ create_tag() {
102106
# $2 - type specified option
103107
#########################################
104108
create_pages() {
105-
if [[ $1 == '' ]]; then
106-
exit 0
107-
fi
109+
if [[ ! -z $1 ]]; then
110+
# split string to array
111+
IFS_BAK=$IFS
112+
IFS=','
113+
local _string=$1
108114

109-
# split string to array
110-
IFS_BAK=$IFS
111-
IFS=','
112-
local _string=$1
115+
case $2 in
113116

114-
case $2 in
117+
$TYPE_CATEGORY)
118+
for i in ${_string#,}; do
119+
create_category $i
120+
done
121+
;;
115122

116-
$TYPE_CATEGORY)
117-
for i in ${_string#,}; do
118-
create_category $i
119-
done
120-
;;
123+
$TYPE_TAG)
124+
for i in ${_string#,}; do
125+
create_tag $i
126+
done
127+
;;
121128

122-
$TYPE_TAG)
123-
for i in ${_string#,}; do
124-
create_tag $i
125-
done
126-
;;
129+
*)
130+
;;
127131

128-
*)
129-
;;
132+
esac
130133

131-
esac
134+
IFS=$IFS_BAK
135+
fi
132136

133-
IFS=$IFS_BAK
134-
}
137+
}
135138

136139

137140
main() {
138141

139142
init
140143

141-
for _file in $(ls "_posts")
144+
for _file in $(find "_posts" -type f \( -iname \*.md -o -iname \*.markdown \))
142145
do
143-
local _path="_posts/$_file"
144-
local _categories=$(read_categories "$_path")
145-
local _tags=$(read_tags "$_path")
146+
local _categories=$(read_categories "$_file")
147+
local _tags=$(read_tags "$_file")
146148

147149
create_pages "$_categories" $TYPE_CATEGORY
148150
create_pages "$_tags" $TYPE_TAG

_scripts/sh/dump_lastmod.sh

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -74,14 +74,12 @@ main() {
7474

7575
local _count=0
7676

77-
for _file in $(ls -r "$POST_DIR")
77+
for _file in $(find ${POST_DIR} -type f \( -iname \*.md -o -iname \*.markdown \))
7878
do
79-
_filepath="$POST_DIR/$_file"
80-
_filename="${_file%.*}" # jekyll cannot read the extension of a file, so omit it.
81-
_filename=${_filename:11} # remove the date
79+
_filename=$(basename $_file | sed 's/[0-9]\([0-9]*-\)//g;s/\..*//' ) # remove date and extension
8280

83-
if _has_changed "$_filepath"; then
84-
_dump "$_filename" "$_filepath"
81+
if _has_changed "$_file"; then
82+
_dump "$_filename" "$_file"
8583
((_count=_count+1))
8684
fi
8785

0 commit comments

Comments
 (0)