From 214480613eabecf4676fae4aff4b79374f7206a0 Mon Sep 17 00:00:00 2001 From: Cotes Chung <11371340+cotes2020@users.noreply.github.com> Date: Thu, 2 Apr 2020 23:25:19 +0800 Subject: [PATCH] Improved script tools. --- _scripts/py/pages_generator.py | 19 ++++++++++++++++--- _scripts/py/update_posts_lastmod.py | 20 +++++++++++++------- _scripts/py/utils/common.py | 12 ++++++++++++ tools/init.sh | 6 +++--- 4 files changed, 44 insertions(+), 13 deletions(-) diff --git a/_scripts/py/pages_generator.py b/_scripts/py/pages_generator.py index eca2832..e547d20 100755 --- a/_scripts/py/pages_generator.py +++ b/_scripts/py/pages_generator.py @@ -16,13 +16,13 @@ MIT License import os -import glob import shutil import sys import subprocess from ruamel.yaml import YAML from utils.common import get_yaml +from utils.common import get_makrdown_files from utils.common import check_py_version @@ -64,7 +64,10 @@ def get_categories(): for dir in POSTS_DIR: path = get_path(dir) - for file in glob.glob(os.path.join(path, '*.md')): + posts = get_makrdown_files(path) + + for file in posts: + meta = yaml.load(get_yaml(file)[0]) if 'category' in meta: @@ -98,6 +101,10 @@ def get_categories(): def generate_category_pages(is_verbose): categories = get_categories() + + if len(categories) <= 0: + return + path = get_path(CATEGORIES_DIR) if os.path.exists(path): @@ -129,7 +136,9 @@ def get_all_tags(): for dir in POSTS_DIR: path = get_path(dir) - for file in glob.glob(os.path.join(path, '*.md')): + posts = get_makrdown_files(path) + + for file in posts: meta = yaml.load(get_yaml(file)[0]) if 'tags' in meta: @@ -145,6 +154,10 @@ def get_all_tags(): def generate_tag_pages(is_verbose): all_tags = get_all_tags() + + if len(all_tags) <= 0: + return + tag_path = get_path(TAG_DIR) if os.path.exists(tag_path): diff --git a/_scripts/py/update_posts_lastmod.py b/_scripts/py/update_posts_lastmod.py index f3f2d41..2a24ab1 100755 --- a/_scripts/py/update_posts_lastmod.py +++ b/_scripts/py/update_posts_lastmod.py @@ -16,7 +16,6 @@ Licensed under MIT """ import sys -import glob import os import getopt import subprocess @@ -28,6 +27,7 @@ from enum import Enum from ruamel.yaml import YAML from utils.common import get_yaml +from utils.common import get_makrdown_files from utils.common import check_py_version @@ -48,11 +48,11 @@ def help(): "'git' for git-log, 'fs' for filesystem, default to 'git'.\n") -def update_lastmod(path, verbose, date): +def update_lastmod(posts, verbose, date): count = 0 yaml = YAML() - for post in glob.glob(path): + for post in posts: lastmod = '' @@ -127,7 +127,8 @@ def update_lastmod(path, verbose, date): def main(argv): check_py_version() - path = os.path.join(POSTS_PATH, "*.md") + specific = False + posts = [] verbose = False date = Date.GIT @@ -145,10 +146,12 @@ def main(argv): sys.exit() elif opt == '-f' or opt == '--file': - path = arg + posts.append(arg) + specific = True elif opt == '-d' or opt == '--dir': - path = os.path.join(arg, "*.md") + posts = get_makrdown_files(arg) + specific = True elif opt == '-v' or opt == '--verbose': verbose = True @@ -162,7 +165,10 @@ def main(argv): help() sys.exit(2) - update_lastmod(path, verbose, date) + if not specific: + posts = get_makrdown_files(POSTS_PATH) + + update_lastmod(posts, verbose, date) main(sys.argv[1:]) diff --git a/_scripts/py/utils/common.py b/_scripts/py/utils/common.py index 0f067be..7b8a924 100644 --- a/_scripts/py/utils/common.py +++ b/_scripts/py/utils/common.py @@ -11,6 +11,8 @@ MIT License ''' import sys +import os +import glob def get_yaml(path): @@ -37,6 +39,16 @@ def get_yaml(path): return yaml, num +def get_makrdown_files(path): + MD_EXTENSIONS = ["md", "markdown", "markdn", "mdown"] + ret_files = [] + + for extension in MD_EXTENSIONS: + ret_files.extend(glob.glob(os.path.join(path, "*." + extension))) + + return ret_files + + def check_py_version(): if not sys.version_info.major == 3 and sys.version_info.minor >= 5: print("WARNING: This script requires Python 3.5 or higher, " diff --git a/tools/init.sh b/tools/init.sh index 7821555..7db76a5 100755 --- a/tools/init.sh +++ b/tools/init.sh @@ -15,9 +15,9 @@ LASTMOD=false WORK_DIR=$(dirname $(dirname $(realpath "$0"))) check_status() { - if [[ ! -z $(git status -s) ]]; then - echo "Warning: Commit the changes of the repository first." - git status -s + if [[ ! -z $(git status _posts -s) ]]; then + echo "Warning: Commit the changes of the directory '_posts' first." + git status -s | grep '_posts' exit 1 fi }