Skip to content

Commit

Permalink
Pretty shell format.
Browse files Browse the repository at this point in the history
  • Loading branch information
cotes2020 committed Sep 1, 2020
1 parent d669f0d commit f92a978
Show file tree
Hide file tree
Showing 7 changed files with 50 additions and 88 deletions.
38 changes: 14 additions & 24 deletions _scripts/sh/create_pages.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -61,52 +57,49 @@ 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"
echo "title: $_name" >> "$_filepath"
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:
# $1 - an array string
# $2 - type specified option
#########################################
create_pages() {
if [[ ! -z $1 ]]; then
if [[ -n $1 ]]; then
# split string to array
IFS_BAK=$IFS
IFS=','
Expand All @@ -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")

Expand Down
12 changes: 3 additions & 9 deletions _scripts/sh/dump_lastmod.sh
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ POST_DIR=_posts
OUTPUT_DIR=_data
OUTPUT_FILE=updates.yml


_init() {

if [[ ! -d "$OUTPUT_DIR" ]]; then
Expand All @@ -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))
Expand All @@ -45,7 +43,6 @@ _has_changed() {
return 1 # false
}


###################################
# Storage the posts' lastmod.
#
Expand All @@ -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
Expand All @@ -90,5 +85,4 @@ main() {
fi
}


main
9 changes: 4 additions & 5 deletions _scripts/sh/sync_monitor.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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
9 changes: 4 additions & 5 deletions tools/deploy.sh
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
# © 2020 Cotes Chung
# Published under MIT License


set -eu

PAGES_BRANCH="gh-pages"
Expand All @@ -28,11 +27,11 @@ backup() {
mv _site "$_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
if [[ -f CNAME ]]; then
if [[ -f CNAME ]]; then
mv CNAME "$_backup_dir"
fi
fi
}

flush() {
Expand All @@ -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
Expand Down
28 changes: 12 additions & 16 deletions tools/init.sh
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@

set -eu


ACTIONS_WORKFLOW=pages-deploy.yml

help() {
Expand All @@ -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 .
Expand All @@ -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

Expand Down
14 changes: 4 additions & 10 deletions tools/publish.sh
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ LASTMOD=false

WORK_DIR="$(dirname "$(dirname "$(realpath "$0")")")"


check_status() {
local _change=$(git status . -s)

Expand All @@ -26,13 +25,11 @@ check_status() {
fi
}


update_files() {
bash _scripts/sh/create_pages.sh
bash _scripts/sh/dump_lastmod.sh
}


commit() {
msg="Updated"

Expand All @@ -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"
Expand All @@ -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"
Expand All @@ -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
Expand All @@ -73,13 +70,11 @@ commit() {

}


push() {
git push origin master -q
echo "[INFO] Published successfully!"
}


main() {

cd "$WORK_DIR"
Expand All @@ -93,5 +88,4 @@ main() {
push
}


main
Loading

0 comments on commit f92a978

Please sign in to comment.