Skip to content

Commit

Permalink
Improved script tools.
Browse files Browse the repository at this point in the history
  • Loading branch information
cotes2020 committed Apr 3, 2020
1 parent a2d089b commit 031415e
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 13 deletions.
19 changes: 16 additions & 3 deletions _scripts/py/pages_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,13 @@


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


Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -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:
Expand All @@ -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):
Expand Down
20 changes: 13 additions & 7 deletions _scripts/py/update_posts_lastmod.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
"""

import sys
import glob
import os
import getopt
import subprocess
Expand All @@ -28,6 +27,7 @@
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


Expand All @@ -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 = ''

Expand Down Expand Up @@ -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

Expand All @@ -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
Expand All @@ -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:])
12 changes: 12 additions & 0 deletions _scripts/py/utils/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
'''

import sys
import os
import glob


def get_yaml(path):
Expand All @@ -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, "
Expand Down
6 changes: 3 additions & 3 deletions tools/init.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down

0 comments on commit 031415e

Please sign in to comment.