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 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):

View File

@ -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:])

View File

@ -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, "

View File

@ -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
}