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

View file

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

View file

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

View file

@ -7,7 +7,6 @@
# © 2020 Cotes Chung
# Published under MIT License
set -eu
PAGES_BRANCH="gh-pages"
@ -51,7 +50,7 @@ deoply() {
git add -A
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"
else
git push -f

View file

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

View file

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

View file

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