1
0
mirror of https://github.com/cotes2020/jekyll-theme-chirpy.git synced 2025-12-18 21:53:26 +00:00

Improve the Shell code style.

This commit is contained in:
Cotes Chung
2020-08-19 17:05:58 +08:00
parent 038bccb3ca
commit 10ab9d3364
8 changed files with 86 additions and 85 deletions

View File

@@ -20,15 +20,15 @@ tag_count=0
_read_yaml() {
local _endline=$(grep -n "\-\-\-" $1 | cut -d: -f 1 | sed -n '2p')
head -$_endline $1
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:")
local _yaml="$(_read_yaml "$1")"
local _categories="$(echo "$_yaml" | grep "^categories:")"
local _category="$(echo "$_yaml" | grep "^category:")"
if [[ ! -z "$_categories" ]]; then
echo "$_categories" | sed "s/categories: *//;s/\[//;s/\].*//;s/, */,/g;s/\"//g;s/'//g"
@@ -39,7 +39,7 @@ read_categories() {
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"
}
@@ -65,14 +65,14 @@ init() {
create_category() {
if [[ ! -z $1 ]]; then
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
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))
fi
@@ -83,15 +83,15 @@ create_category() {
create_tag() {
if [[ ! -z $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
echo "---" > "$_filepath"
echo "layout: tag" >> "$_filepath"
echo "title: $_name" >> "$_filepath"
echo "tag: $_name" >> "$_filepath"
echo "---" >> "$_filepath"
((tag_count=tag_count+1))
fi
@@ -116,13 +116,13 @@ create_pages() {
$TYPE_CATEGORY)
for i in ${_string#,}; do
create_category $i
create_category "$i"
done
;;
$TYPE_TAG)
for i in ${_string#,}; do
create_tag $i
create_tag "$i"
done
;;

View File

@@ -35,8 +35,8 @@ _init() {
_has_changed() {
local _log_count=`git log --pretty=%ad $1 | wc -l | sed 's/ *//'`
_log_count=$(($_log_count + 0))
local _log_count="$(git log --pretty=%ad "$1" | wc -l | sed 's/ *//')"
_log_count=$((_log_count + 0))
if [[ $_log_count > 1 ]]; then
return 0 # true
@@ -56,7 +56,7 @@ _has_changed() {
# the file '_data/updates.yml'
###################################
_dump() {
local _lasmod="`git log -1 --pretty=%ad --date=iso $2`"
local _lasmod="$(git log -1 --pretty=%ad --date=iso "$2")"
if [[ ! -f "$OUTPUT_DIR/$OUTPUT_FILE" ]]; then
touch "$OUTPUT_DIR/$OUTPUT_FILE"
@@ -76,7 +76,7 @@ main() {
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
_filename="$(basename "$_file" | sed 's/-\-\+/-/;s/[[:digit:]]\([[:digit:]]*-\)//g;s/\..*//' )" # remove date and extension
if _has_changed "$_file"; then
_dump "$_filename" "$_file"

View File

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