Skip to content

Commit 2144806

Browse files
committed
Improved script tools.
1 parent ad992db commit 2144806

File tree

4 files changed

+44
-13
lines changed

4 files changed

+44
-13
lines changed

_scripts/py/pages_generator.py

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,13 @@
1616

1717

1818
import os
19-
import glob
2019
import shutil
2120
import sys
2221
import subprocess
2322

2423
from ruamel.yaml import YAML
2524
from utils.common import get_yaml
25+
from utils.common import get_makrdown_files
2626
from utils.common import check_py_version
2727

2828

@@ -64,7 +64,10 @@ def get_categories():
6464

6565
for dir in POSTS_DIR:
6666
path = get_path(dir)
67-
for file in glob.glob(os.path.join(path, '*.md')):
67+
posts = get_makrdown_files(path)
68+
69+
for file in posts:
70+
6871
meta = yaml.load(get_yaml(file)[0])
6972

7073
if 'category' in meta:
@@ -98,6 +101,10 @@ def get_categories():
98101

99102
def generate_category_pages(is_verbose):
100103
categories = get_categories()
104+
105+
if len(categories) <= 0:
106+
return
107+
101108
path = get_path(CATEGORIES_DIR)
102109

103110
if os.path.exists(path):
@@ -129,7 +136,9 @@ def get_all_tags():
129136

130137
for dir in POSTS_DIR:
131138
path = get_path(dir)
132-
for file in glob.glob(os.path.join(path, '*.md')):
139+
posts = get_makrdown_files(path)
140+
141+
for file in posts:
133142
meta = yaml.load(get_yaml(file)[0])
134143

135144
if 'tags' in meta:
@@ -145,6 +154,10 @@ def get_all_tags():
145154

146155
def generate_tag_pages(is_verbose):
147156
all_tags = get_all_tags()
157+
158+
if len(all_tags) <= 0:
159+
return
160+
148161
tag_path = get_path(TAG_DIR)
149162

150163
if os.path.exists(tag_path):

_scripts/py/update_posts_lastmod.py

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
"""
1717

1818
import sys
19-
import glob
2019
import os
2120
import getopt
2221
import subprocess
@@ -28,6 +27,7 @@
2827
from ruamel.yaml import YAML
2928

3029
from utils.common import get_yaml
30+
from utils.common import get_makrdown_files
3131
from utils.common import check_py_version
3232

3333

@@ -48,11 +48,11 @@ def help():
4848
"'git' for git-log, 'fs' for filesystem, default to 'git'.\n")
4949

5050

51-
def update_lastmod(path, verbose, date):
51+
def update_lastmod(posts, verbose, date):
5252
count = 0
5353
yaml = YAML()
5454

55-
for post in glob.glob(path):
55+
for post in posts:
5656

5757
lastmod = ''
5858

@@ -127,7 +127,8 @@ def update_lastmod(path, verbose, date):
127127
def main(argv):
128128
check_py_version()
129129

130-
path = os.path.join(POSTS_PATH, "*.md")
130+
specific = False
131+
posts = []
131132
verbose = False
132133
date = Date.GIT
133134

@@ -145,10 +146,12 @@ def main(argv):
145146
sys.exit()
146147

147148
elif opt == '-f' or opt == '--file':
148-
path = arg
149+
posts.append(arg)
150+
specific = True
149151

150152
elif opt == '-d' or opt == '--dir':
151-
path = os.path.join(arg, "*.md")
153+
posts = get_makrdown_files(arg)
154+
specific = True
152155

153156
elif opt == '-v' or opt == '--verbose':
154157
verbose = True
@@ -162,7 +165,10 @@ def main(argv):
162165
help()
163166
sys.exit(2)
164167

165-
update_lastmod(path, verbose, date)
168+
if not specific:
169+
posts = get_makrdown_files(POSTS_PATH)
170+
171+
update_lastmod(posts, verbose, date)
166172

167173

168174
main(sys.argv[1:])

_scripts/py/utils/common.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
'''
1212

1313
import sys
14+
import os
15+
import glob
1416

1517

1618
def get_yaml(path):
@@ -37,6 +39,16 @@ def get_yaml(path):
3739
return yaml, num
3840

3941

42+
def get_makrdown_files(path):
43+
MD_EXTENSIONS = ["md", "markdown", "markdn", "mdown"]
44+
ret_files = []
45+
46+
for extension in MD_EXTENSIONS:
47+
ret_files.extend(glob.glob(os.path.join(path, "*." + extension)))
48+
49+
return ret_files
50+
51+
4052
def check_py_version():
4153
if not sys.version_info.major == 3 and sys.version_info.minor >= 5:
4254
print("WARNING: This script requires Python 3.5 or higher, "

tools/init.sh

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,9 @@ LASTMOD=false
1515
WORK_DIR=$(dirname $(dirname $(realpath "$0")))
1616

1717
check_status() {
18-
if [[ ! -z $(git status -s) ]]; then
19-
echo "Warning: Commit the changes of the repository first."
20-
git status -s
18+
if [[ ! -z $(git status _posts -s) ]]; then
19+
echo "Warning: Commit the changes of the directory '_posts' first."
20+
git status -s | grep '_posts'
2121
exit 1
2222
fi
2323
}

0 commit comments

Comments
 (0)