From 5b0aaa54036f455f52c7ec1d860c383976868c75 Mon Sep 17 00:00:00 2001 From: Cotes Chung <11371340+cotes2020@users.noreply.github.com> Date: Tue, 14 Jul 2020 20:55:43 +0800 Subject: [PATCH] Improved script tools. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Allows posts to be placed in subdirectories (#41, #87). - Identify posts file with the suffix ‘.markdown’. **Page creator** - Pass the posts without any categories or tags. - Omit the YAML comments for categories/tags. **Lastmod** - compatible with one-digit month or day post files. --- _scripts/sh/create_pages.sh | 108 ++++++++++++++++++------------------ _scripts/sh/dump_lastmod.sh | 10 ++-- 2 files changed, 59 insertions(+), 59 deletions(-) diff --git a/_scripts/sh/create_pages.sh b/_scripts/sh/create_pages.sh index 6e152d1..12e8068 100755 --- a/_scripts/sh/create_pages.sh +++ b/_scripts/sh/create_pages.sh @@ -31,16 +31,16 @@ read_categories() { local _category=$(echo "$_yaml" | grep "^category:") if [[ ! -z "$_categories" ]]; then - echo "$_categories" | sed "s/categories: *//;s/\[//;s/\]//;s/, */,/g;s/\"//g;s/'//g" + echo "$_categories" | sed "s/categories: *//;s/\[//;s/\].*//;s/, */,/g;s/\"//g;s/'//g" elif [[ ! -z "_category" ]]; then - echo "$_category" | sed "s/category: *//;s/\[//;s/\]//;s/, */,/g;s/\"//g;s/'//g" + echo "$_category" | sed "s/category: *//;s/\[//;s/\].*//;s/, */,/g;s/\"//g;s/'//g" fi } read_tags() { local _yaml=$(_read_yaml $1) - echo "$_yaml" | grep "^tags:" | sed "s/tags: *//;s/\[//;s/\]//;s/, */,/g;s/\"//g;s/'//g" + echo "$_yaml" | grep "^tags:" | sed "s/tags: *//;s/\[//;s/\].*//;s/, */,/g;s/\"//g;s/'//g" } @@ -63,34 +63,38 @@ init() { create_category() { - local _name=$1 - local _filepath="categories/$(echo $_name | sed 's/ /-/g' | awk '{print tolower($0)}').html" + if [[ ! -z $1 ]]; then + local _name=$1 + local _filepath="categories/$(echo $_name | sed 's/ /-/g' | awk '{print tolower($0)}').html" - if [[ ! -f $_filepath ]]; then - echo "---" > $_filepath - echo "layout: category" >> $_filepath - echo "title: $_name" >> $_filepath - echo "category: $_name" >> $_filepath - echo "---" >> $_filepath + if [[ ! -f $_filepath ]]; then + echo "---" > $_filepath + echo "layout: category" >> $_filepath + echo "title: $_name" >> $_filepath + echo "category: $_name" >> $_filepath + echo "---" >> $_filepath - ((category_count=category_count+1)) + ((category_count=category_count+1)) + fi fi } create_tag() { - local _name=$1 - local _filepath="tags/$( echo $_name | sed "s/ /-/g;s/'//g" | awk '{print tolower($0)}' ).html" + if [[ ! -z $1 ]]; then + local _name=$1 + local _filepath="tags/$( echo $_name | sed "s/ /-/g;s/'//g" | awk '{print tolower($0)}' ).html" - if [[ ! -f $_filepath ]]; then + if [[ ! -f $_filepath ]]; then - echo "---" > $_filepath - echo "layout: tag" >> $_filepath - echo "title: $_name" >> $_filepath - echo "tag: $_name" >> $_filepath - echo "---" >> $_filepath + echo "---" > $_filepath + echo "layout: tag" >> $_filepath + echo "title: $_name" >> $_filepath + echo "tag: $_name" >> $_filepath + echo "---" >> $_filepath - ((tag_count=tag_count+1)) + ((tag_count=tag_count+1)) + fi fi } @@ -102,47 +106,45 @@ create_tag() { # $2 - type specified option ######################################### create_pages() { - if [[ $1 == '' ]]; then - exit 0 + if [[ ! -z $1 ]]; then + # split string to array + IFS_BAK=$IFS + IFS=',' + local _string=$1 + + case $2 in + + $TYPE_CATEGORY) + for i in ${_string#,}; do + create_category $i + done + ;; + + $TYPE_TAG) + for i in ${_string#,}; do + create_tag $i + done + ;; + + *) + ;; + + esac + + IFS=$IFS_BAK fi - # split string to array - IFS_BAK=$IFS - IFS=',' - local _string=$1 - - case $2 in - - $TYPE_CATEGORY) - for i in ${_string#,}; do - create_category $i - done - ;; - - $TYPE_TAG) - for i in ${_string#,}; do - create_tag $i - done - ;; - - *) - ;; - - esac - - IFS=$IFS_BAK - } +} main() { init - for _file in $(ls "_posts") + for _file in $(find "_posts" -type f \( -iname \*.md -o -iname \*.markdown \)) do - local _path="_posts/$_file" - local _categories=$(read_categories "$_path") - local _tags=$(read_tags "$_path") + local _categories=$(read_categories "$_file") + local _tags=$(read_tags "$_file") create_pages "$_categories" $TYPE_CATEGORY create_pages "$_tags" $TYPE_TAG diff --git a/_scripts/sh/dump_lastmod.sh b/_scripts/sh/dump_lastmod.sh index 1190e60..eeed57e 100755 --- a/_scripts/sh/dump_lastmod.sh +++ b/_scripts/sh/dump_lastmod.sh @@ -74,14 +74,12 @@ main() { local _count=0 - for _file in $(ls -r "$POST_DIR") + for _file in $(find ${POST_DIR} -type f \( -iname \*.md -o -iname \*.markdown \)) do - _filepath="$POST_DIR/$_file" - _filename="${_file%.*}" # jekyll cannot read the extension of a file, so omit it. - _filename=${_filename:11} # remove the date + _filename=$(basename $_file | sed 's/[0-9]\([0-9]*-\)//g;s/\..*//' ) # remove date and extension - if _has_changed "$_filepath"; then - _dump "$_filename" "$_filepath" + if _has_changed "$_file"; then + _dump "$_filename" "$_file" ((_count=_count+1)) fi