Pretty shell format.

This commit is contained in:
Cotes Chung 2020-09-01 14:54:29 +08:00
parent 252e33b502
commit c473771af4
7 changed files with 50 additions and 88 deletions

View file

@ -18,32 +18,28 @@ TYPE_TAG=1
category_count=0 category_count=0
tag_count=0 tag_count=0
_read_yaml() { _read_yaml() {
local _endline="$(grep -n "\-\-\-" "$1" | cut -d: -f 1 | sed -n '2p')" local _endline="$(grep -n "\-\-\-" "$1" | cut -d: -f 1 | sed -n '2p')"
head -"$_endline" "$1" head -"$_endline" "$1"
} }
read_categories() { read_categories() {
local _yaml="$(_read_yaml "$1")" local _yaml="$(_read_yaml "$1")"
local _categories="$(echo "$_yaml" | grep "^categories *:")" local _categories="$(echo "$_yaml" | grep "^categories *:")"
local _category="$(echo "$_yaml" | grep "^category *:")" local _category="$(echo "$_yaml" | grep "^category *:")"
if [[ ! -z "$_categories" ]]; then if [[ -n $_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 elif [[ -n "_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 fi
} }
read_tags() { read_tags() {
local _yaml="$(_read_yaml "$1")" 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"
} }
init() { init() {
if [[ -d categories ]]; then if [[ -d categories ]]; then
@ -61,31 +57,29 @@ init() {
mkdir categories tags mkdir categories tags
} }
create_category() { create_category() {
if [[ ! -z $1 ]]; then if [[ -n $1 ]]; then
local _name=$1 local _name=$1
local _filepath="categories/$(echo "$_name" | sed 's/ /-/g' | awk '{print tolower($0)}').html" local _filepath="categories/$(echo "$_name" | sed 's/ /-/g' | awk '{print tolower($0)}').html"
if [[ ! -f "$_filepath" ]]; then if [[ ! -f $_filepath ]]; then
echo "---" > "$_filepath" echo "---" > "$_filepath"
echo "layout: category" >> "$_filepath" echo "layout: category" >> "$_filepath"
echo "title: $_name" >> "$_filepath" echo "title: $_name" >> "$_filepath"
echo "category: $_name" >> "$_filepath" echo "category: $_name" >> "$_filepath"
echo "---" >> "$_filepath" echo "---" >> "$_filepath"
((category_count=category_count+1)) ((category_count = category_count + 1))
fi fi
fi fi
} }
create_tag() { create_tag() {
if [[ ! -z $1 ]]; then if [[ -n $1 ]]; then
local _name=$1 local _name=$1
local _filepath="tags/$( echo "$_name" | sed "s/ /-/g;s/'//g" | awk '{print tolower($0)}' ).html" 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 "---" > "$_filepath"
echo "layout: tag" >> "$_filepath" echo "layout: tag" >> "$_filepath"
@ -93,12 +87,11 @@ create_tag() {
echo "tag: $_name" >> "$_filepath" echo "tag: $_name" >> "$_filepath"
echo "---" >> "$_filepath" echo "---" >> "$_filepath"
((tag_count=tag_count+1)) ((tag_count = tag_count + 1))
fi fi
fi fi
} }
######################################### #########################################
# Create HTML pages for Categories/Tags. # Create HTML pages for Categories/Tags.
# Arguments: # Arguments:
@ -106,7 +99,7 @@ create_tag() {
# $2 - type specified option # $2 - type specified option
######################################### #########################################
create_pages() { create_pages() {
if [[ ! -z $1 ]]; then if [[ -n $1 ]]; then
# split string to array # split string to array
IFS_BAK=$IFS IFS_BAK=$IFS
IFS=',' IFS=','
@ -126,23 +119,20 @@ create_pages() {
done done
;; ;;
*) *) ;;
;;
esac esac
IFS=$IFS_BAK IFS=$IFS_BAK
fi fi
} }
main() { main() {
init init
for _file in $(find "_posts" -type f \( -iname \*.md -o -iname \*.markdown \)) for _file in $(find "_posts" -type f \( -iname \*.md -o -iname \*.markdown \)); do
do
local _categories=$(read_categories "$_file") local _categories=$(read_categories "$_file")
local _tags=$(read_tags "$_file") local _tags=$(read_tags "$_file")

View file

@ -17,7 +17,6 @@ POST_DIR=_posts
OUTPUT_DIR=_data OUTPUT_DIR=_data
OUTPUT_FILE=updates.yml OUTPUT_FILE=updates.yml
_init() { _init() {
if [[ ! -d "$OUTPUT_DIR" ]]; then if [[ ! -d "$OUTPUT_DIR" ]]; then
@ -33,7 +32,6 @@ _init() {
fi fi
} }
_has_changed() { _has_changed() {
local _log_count="$(git log --pretty=%ad "$1" | wc -l | sed 's/ *//')" local _log_count="$(git log --pretty=%ad "$1" | wc -l | sed 's/ *//')"
_log_count=$((_log_count + 0)) _log_count=$((_log_count + 0))
@ -45,7 +43,6 @@ _has_changed() {
return 1 # false return 1 # false
} }
################################### ###################################
# Storage the posts' lastmod. # Storage the posts' lastmod.
# #
@ -67,20 +64,18 @@ _dump() {
echo " lastmod: '$_lasmod'" >> "$OUTPUT_DIR/$OUTPUT_FILE" echo " lastmod: '$_lasmod'" >> "$OUTPUT_DIR/$OUTPUT_FILE"
} }
main() { main() {
_init _init
local _count=0 local _count=0
for _file in $(find ${POST_DIR} -type f \( -iname \*.md -o -iname \*.markdown \)) for _file in $(find ${POST_DIR} -type f \( -iname \*.md -o -iname \*.markdown \)); do
do _filename="$(basename "$_file" | sed 's/-\-\+/-/;s/[[:digit:]]\([[:digit:]]*-\)//g;s/\..*//')" # remove date and extension
_filename="$(basename "$_file" | sed 's/-\-\+/-/;s/[[:digit:]]\([[:digit:]]*-\)//g;s/\..*//' )" # remove date and extension
if _has_changed "$_file"; then if _has_changed "$_file"; then
_dump "$_filename" "$_file" _dump "$_filename" "$_file"
((_count=_count+1)) ((_count = _count + 1))
fi fi
done done
@ -90,5 +85,4 @@ main() {
fi fi
} }
main main

View file

@ -11,7 +11,7 @@
# $3 -> the destination sync directory # $3 -> the destination sync directory
# Omit the system temp file # Omit the system temp file
if [[ ! -f "$1" ]]; then if [[ ! -f $1 ]]; then
exit 0 exit 0
fi fi
@ -21,18 +21,17 @@ dir_prefix="$(realpath "$2")/"
related_dir="${src_dir:${#dir_prefix}}" related_dir="${src_dir:${#dir_prefix}}"
dest="$(realpath "$3")/${related_dir}" dest="$(realpath "$3")/${related_dir}"
if [[ ! -d "$dest" ]]; then if [[ ! -d $dest ]]; then
mkdir -p "$dest" mkdir -p "$dest"
fi fi
if [[ -f "$1" ]]; then if [[ -f $1 ]]; then
cp "$1" "$dest" cp "$1" "$dest"
fi fi
if [[ "$related_dir" == "_posts" ]]; then if [[ $related_dir == "_posts" ]]; then
bash "$3"/_scripts/sh/create_pages.sh bash "$3"/_scripts/sh/create_pages.sh
bash "$3"/_scripts/sh/dump_lastmod.sh bash "$3"/_scripts/sh/dump_lastmod.sh
fi fi

View file

@ -7,7 +7,6 @@
# © 2020 Cotes Chung # © 2020 Cotes Chung
# Published under MIT License # Published under MIT License
set -eu set -eu
PAGES_BRANCH="gh-pages" PAGES_BRANCH="gh-pages"
@ -28,11 +27,11 @@ backup() {
mv _site "$_backup_dir" mv _site "$_backup_dir"
mv .git "$_backup_dir" mv .git "$_backup_dir"
# When adding custom domain from Github website, # When adding custom domain from Github website,
# the CANME only exist on `gh-pages` branch # the CANME only exist on `gh-pages` branch
if [[ -f CNAME ]]; then if [[ -f CNAME ]]; then
mv CNAME "$_backup_dir" mv CNAME "$_backup_dir"
fi fi
} }
flush() { flush() {
@ -51,7 +50,7 @@ deoply() {
git add -A git add -A
git commit -m "[Automation] Site update No.${GITHUB_RUN_NUMBER}" git commit -m "[Automation] Site update No.${GITHUB_RUN_NUMBER}"
if [[ $_no_branch = true ]]; then if $_no_branch; then
git push -u origin "$PAGES_BRANCH" git push -u origin "$PAGES_BRANCH"
else else
git push -f git push -f

View file

@ -9,7 +9,6 @@
set -eu set -eu
ACTIONS_WORKFLOW=pages-deploy.yml ACTIONS_WORKFLOW=pages-deploy.yml
help() { help() {
@ -26,24 +25,23 @@ check_init() {
local _has_inited=false local _has_inited=false
if [[ -d .github ]]; then if [[ -d .github ]]; then
if [[ -f .github/workflows/$ACTIONS_WORKFLOW if [[ -f .github/workflows/$ACTIONS_WORKFLOW && \
&& $(find .github/workflows/ -type f -name "*.yml" | wc -l) == 1 ]]; then $(find .github/workflows/ -type f -name "*.yml" | wc -l) == 1 ]]; then
_has_inited=true _has_inited=true
fi fi
else else
_has_inited=true _has_inited=true
fi fi
if [[ $_has_inited = true ]]; then if $_has_inited; then
echo "Already initialized." echo "Already initialized."
exit 0 exit 0
fi fi
} }
init_files() { init_files() {
if [[ $_no_gh = true ]]; then if $_no_gh; then
rm -rf .github rm -rf .github
else else
mv .github/workflows/$ACTIONS_WORKFLOW.hook . mv .github/workflows/$ACTIONS_WORKFLOW.hook .
@ -55,34 +53,32 @@ init_files() {
rm -f .travis.yml rm -f .travis.yml
rm -rf _posts/* docs rm -rf _posts/* docs
git add -A && git add .github -f git add -A && git add .github -f
git commit -m "[Automation] Initialize the environment." -q git commit -m "[Automation] Initialize the environment." -q
echo "[INFO] Initialization successful!" echo "[INFO] Initialization successful!"
} }
check_init check_init
_no_gh=false _no_gh=false
while (( $# )) while (($#)); do
do
opt="$1" opt="$1"
case $opt in case $opt in
--no-gh) --no-gh)
_no_gh=true _no_gh=true
shift shift
;; ;;
-h|--help) -h | --help)
help help
exit 0 exit 0
;; ;;
*) *)
# unknown option # unknown option
help help
exit 1 exit 1
;; ;;
esac esac
done done

View file

@ -15,7 +15,6 @@ LASTMOD=false
WORK_DIR="$(dirname "$(dirname "$(realpath "$0")")")" WORK_DIR="$(dirname "$(dirname "$(realpath "$0")")")"
check_status() { check_status() {
local _change=$(git status . -s) local _change=$(git status . -s)
@ -26,13 +25,11 @@ check_status() {
fi fi
} }
update_files() { update_files() {
bash _scripts/sh/create_pages.sh bash _scripts/sh/create_pages.sh
bash _scripts/sh/dump_lastmod.sh bash _scripts/sh/dump_lastmod.sh
} }
commit() { commit() {
msg="Updated" msg="Updated"
@ -44,7 +41,7 @@ commit() {
if [[ ! -z $(git status tags -s) ]]; then if [[ ! -z $(git status tags -s) ]]; then
git add tags/ git add tags/
if [[ $CATEGORIES = true ]]; then if $CATEGORIES; then
msg+="," msg+=","
else else
msg+=" the" msg+=" the"
@ -53,9 +50,9 @@ commit() {
TAGS=true TAGS=true
fi fi
if [[ ! -z $(git status _data -s) ]]; then if [[ -n $(git status _data -s) ]]; then
git add _data git add _data
if [[ $CATEGORIES = true || $TAGS = true ]]; then if $CATEGORIES || $TAGS; then
msg+="," msg+=","
else else
msg+=" the" msg+=" the"
@ -64,7 +61,7 @@ commit() {
LASTMOD=true LASTMOD=true
fi fi
if [[ $CATEGORIES = true || $TAGS = true || $LASTMOD = true ]]; then if $CATEGORIES || $TAGS || $LASTMOD; then
msg+=" for post(s)." msg+=" for post(s)."
git commit -m "[Automation] $msg" -q git commit -m "[Automation] $msg" -q
else else
@ -73,13 +70,11 @@ commit() {
} }
push() { push() {
git push origin master -q git push origin master -q
echo "[INFO] Published successfully!" echo "[INFO] Published successfully!"
} }
main() { main() {
cd "$WORK_DIR" cd "$WORK_DIR"
@ -93,5 +88,4 @@ main() {
push push
} }
main main

View file

@ -20,7 +20,6 @@ SYNC_TOOL=_scripts/sh/sync_monitor.sh
cmd="bundle exec jekyll s -l -o" cmd="bundle exec jekyll s -l -o"
realtime=false realtime=false
_help() { _help() {
echo "Usage:" echo "Usage:"
echo echo
@ -35,7 +34,6 @@ _help() {
echo " -r, --realtime Make the modified content updated in real time" echo " -r, --realtime Make the modified content updated in real time"
} }
_cleanup() { _cleanup() {
if [[ -d _site || -d .jekyll-cache ]]; then if [[ -d _site || -d .jekyll-cache ]]; then
jekyll clean jekyll clean
@ -45,7 +43,6 @@ _cleanup() {
ps aux | grep fswatch | awk '{print $2}' | xargs kill -9 > /dev/null 2>&1 ps aux | grep fswatch | awk '{print $2}' | xargs kill -9 > /dev/null 2>&1
} }
_init() { _init() {
if [[ -d "${WORK_DIR}/${CONTAINER}" ]]; then if [[ -d "${WORK_DIR}/${CONTAINER}" ]]; then
@ -60,7 +57,6 @@ _init() {
trap _cleanup INT trap _cleanup INT
} }
_check_unset() { _check_unset() {
if [[ -z ${1:+unset} ]]; then if [[ -z ${1:+unset} ]]; then
_help _help
@ -68,7 +64,6 @@ _check_unset() {
fi fi
} }
_check_command() { _check_command() {
if [[ -z $(command -v "$1") ]]; then if [[ -z $(command -v "$1") ]]; then
echo "Error: command '$1' not found !" echo "Error: command '$1' not found !"
@ -77,7 +72,6 @@ _check_command() {
fi fi
} }
main() { main() {
_init _init
@ -85,7 +79,7 @@ main() {
bash _scripts/sh/create_pages.sh bash _scripts/sh/create_pages.sh
bash _scripts/sh/dump_lastmod.sh bash _scripts/sh/dump_lastmod.sh
if [[ $realtime = true ]]; then if $realtime; then
fswatch -0 -e "/\..*" "$WORK_DIR" | xargs -0 -I {} bash "./${SYNC_TOOL}" {} "$WORK_DIR" . & fswatch -0 -e "/\..*" "$WORK_DIR" | xargs -0 -I {} bash "./${SYNC_TOOL}" {} "$WORK_DIR" . &
fi fi
@ -93,27 +87,24 @@ main() {
eval "$cmd" eval "$cmd"
} }
while (($#)); do
while (( $# ))
do
opt="$1" opt="$1"
case $opt in case $opt in
-H|--host) -H | --host)
_check_unset "$2" _check_unset "$2"
cmd+=" -H $2" cmd+=" -H $2"
shift # past argument shift # past argument
shift # past value shift # past value
;; ;;
-P|--port) -P | --port)
_check_unset "$2" _check_unset "$2"
cmd+=" -P $2" cmd+=" -P $2"
shift shift
shift shift
;; ;;
-b|--baseurl) -b | --baseurl)
_check_unset "$2" _check_unset "$2"
if [[ "$2" == \/* ]] if [[ "$2" == \/* ]]; then
then
cmd+=" -b $2" cmd+=" -b $2"
else else
_help _help
@ -122,16 +113,16 @@ do
shift shift
shift shift
;; ;;
-t|--trace) -t | --trace)
cmd+=" -t" cmd+=" -t"
shift shift
;; ;;
-r|--realtime) -r | --realtime)
_check_command fswatch "http://emcrisostomo.github.io/fswatch/" _check_command fswatch "http://emcrisostomo.github.io/fswatch/"
realtime=true realtime=true
shift shift
;; ;;
-h|--help) -h | --help)
_help _help
exit 0 exit 0
;; ;;
@ -143,5 +134,4 @@ do
esac esac
done done
main main