Improved script tools.

This commit is contained in:
Cotes Chung 2020-04-02 23:25:19 +08:00
parent ad992db75f
commit 214480613e
4 changed files with 44 additions and 13 deletions

View file

@ -16,13 +16,13 @@ MIT License
import os import os
import glob
import shutil import shutil
import sys import sys
import subprocess import subprocess
from ruamel.yaml import YAML from ruamel.yaml import YAML
from utils.common import get_yaml from utils.common import get_yaml
from utils.common import get_makrdown_files
from utils.common import check_py_version from utils.common import check_py_version
@ -64,7 +64,10 @@ def get_categories():
for dir in POSTS_DIR: for dir in POSTS_DIR:
path = get_path(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]) meta = yaml.load(get_yaml(file)[0])
if 'category' in meta: if 'category' in meta:
@ -98,6 +101,10 @@ def get_categories():
def generate_category_pages(is_verbose): def generate_category_pages(is_verbose):
categories = get_categories() categories = get_categories()
if len(categories) <= 0:
return
path = get_path(CATEGORIES_DIR) path = get_path(CATEGORIES_DIR)
if os.path.exists(path): if os.path.exists(path):
@ -129,7 +136,9 @@ def get_all_tags():
for dir in POSTS_DIR: for dir in POSTS_DIR:
path = get_path(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]) meta = yaml.load(get_yaml(file)[0])
if 'tags' in meta: if 'tags' in meta:
@ -145,6 +154,10 @@ def get_all_tags():
def generate_tag_pages(is_verbose): def generate_tag_pages(is_verbose):
all_tags = get_all_tags() all_tags = get_all_tags()
if len(all_tags) <= 0:
return
tag_path = get_path(TAG_DIR) tag_path = get_path(TAG_DIR)
if os.path.exists(tag_path): if os.path.exists(tag_path):

View file

@ -16,7 +16,6 @@ Licensed under MIT
""" """
import sys import sys
import glob
import os import os
import getopt import getopt
import subprocess import subprocess
@ -28,6 +27,7 @@ from enum import Enum
from ruamel.yaml import YAML from ruamel.yaml import YAML
from utils.common import get_yaml from utils.common import get_yaml
from utils.common import get_makrdown_files
from utils.common import check_py_version from utils.common import check_py_version
@ -48,11 +48,11 @@ def help():
"'git' for git-log, 'fs' for filesystem, default to 'git'.\n") "'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 count = 0
yaml = YAML() yaml = YAML()
for post in glob.glob(path): for post in posts:
lastmod = '' lastmod = ''
@ -127,7 +127,8 @@ def update_lastmod(path, verbose, date):
def main(argv): def main(argv):
check_py_version() check_py_version()
path = os.path.join(POSTS_PATH, "*.md") specific = False
posts = []
verbose = False verbose = False
date = Date.GIT date = Date.GIT
@ -145,10 +146,12 @@ def main(argv):
sys.exit() sys.exit()
elif opt == '-f' or opt == '--file': elif opt == '-f' or opt == '--file':
path = arg posts.append(arg)
specific = True
elif opt == '-d' or opt == '--dir': 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': elif opt == '-v' or opt == '--verbose':
verbose = True verbose = True
@ -162,7 +165,10 @@ def main(argv):
help() help()
sys.exit(2) 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:]) main(sys.argv[1:])

View file

@ -11,6 +11,8 @@ MIT License
''' '''
import sys import sys
import os
import glob
def get_yaml(path): def get_yaml(path):
@ -37,6 +39,16 @@ def get_yaml(path):
return yaml, num 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(): def check_py_version():
if not sys.version_info.major == 3 and sys.version_info.minor >= 5: if not sys.version_info.major == 3 and sys.version_info.minor >= 5:
print("WARNING: This script requires Python 3.5 or higher, " print("WARNING: This script requires Python 3.5 or higher, "

View file

@ -15,9 +15,9 @@ LASTMOD=false
WORK_DIR=$(dirname $(dirname $(realpath "$0"))) WORK_DIR=$(dirname $(dirname $(realpath "$0")))
check_status() { check_status() {
if [[ ! -z $(git status -s) ]]; then if [[ ! -z $(git status _posts -s) ]]; then
echo "Warning: Commit the changes of the repository first." echo "Warning: Commit the changes of the directory '_posts' first."
git status -s git status -s | grep '_posts'
exit 1 exit 1
fi fi
} }