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")