2020-04-09 22:15:51 +03:00
|
|
|
#!/usr/local/bin/bash
|
|
|
|
#
|
|
|
|
# Create HTML pages for Categories and Tags in posts.
|
|
|
|
#
|
|
|
|
# Usage:
|
|
|
|
# Call from the '_posts' sibling directory.
|
|
|
|
#
|
|
|
|
# v2.2
|
|
|
|
# https://github.com/cotes2020/jekyll-theme-chirpy
|
|
|
|
# © 2020 Cotes Chung
|
|
|
|
# Published under MIT License
|
|
|
|
|
|
|
|
set -eu
|
|
|
|
|
|
|
|
TYPE_CATEGORY=0
|
|
|
|
TYPE_TAG=1
|
|
|
|
|
|
|
|
category_count=0
|
|
|
|
tag_count=0
|
|
|
|
|
2020-05-01 09:09:15 +03:00
|
|
|
_read_yaml() {
|
2020-08-19 12:05:58 +03:00
|
|
|
local _endline="$(grep -n "\-\-\-" "$1" | cut -d: -f 1 | sed -n '2p')"
|
|
|
|
head -"$_endline" "$1"
|
2020-05-01 09:09:15 +03:00
|
|
|
}
|
|
|
|
|
2020-04-09 22:15:51 +03:00
|
|
|
read_categories() {
|
2020-08-19 12:05:58 +03:00
|
|
|
local _yaml="$(_read_yaml "$1")"
|
2020-08-31 20:05:23 +03:00
|
|
|
local _categories="$(echo "$_yaml" | grep "^categories *:")"
|
|
|
|
local _category="$(echo "$_yaml" | grep "^category *:")"
|
2020-05-01 09:09:15 +03:00
|
|
|
|
2020-09-01 09:54:29 +03:00
|
|
|
if [[ -n $_categories ]]; then
|
2020-08-31 20:05:23 +03:00
|
|
|
echo "$_categories" | sed "s/categories *: *//;s/\[//;s/\].*//;s/, */,/g;s/\"//g;s/'//g"
|
2020-09-01 16:37:08 +03:00
|
|
|
elif [[ -n $_category ]]; then
|
2020-08-31 20:05:23 +03:00
|
|
|
echo "$_category" | sed "s/category *: *//;s/\[//;s/\].*//;s/, */,/g;s/\"//g;s/'//g"
|
2020-04-09 22:15:51 +03:00
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
|
|
|
read_tags() {
|
2020-08-19 12:05:58 +03:00
|
|
|
local _yaml="$(_read_yaml "$1")"
|
2020-08-31 20:05:23 +03:00
|
|
|
echo "$_yaml" | grep "^tags *:" | sed "s/tags *: *//;s/\[//;s/\].*//;s/, */,/g;s/\"//g;s/'//g"
|
2020-04-09 22:15:51 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
init() {
|
2020-07-13 06:20:07 +03:00
|
|
|
|
2020-04-09 22:15:51 +03:00
|
|
|
if [[ -d categories ]]; then
|
|
|
|
rm -rf categories
|
|
|
|
fi
|
|
|
|
|
|
|
|
if [[ -d tags ]]; then
|
|
|
|
rm -rf tags
|
|
|
|
fi
|
|
|
|
|
2020-07-13 06:20:07 +03:00
|
|
|
if [[ ! -d _posts ]]; then
|
|
|
|
exit 0
|
|
|
|
fi
|
|
|
|
|
2020-04-09 22:15:51 +03:00
|
|
|
mkdir categories tags
|
|
|
|
}
|
|
|
|
|
|
|
|
create_category() {
|
2020-09-01 09:54:29 +03:00
|
|
|
if [[ -n $1 ]]; then
|
2020-07-14 15:55:43 +03:00
|
|
|
local _name=$1
|
2020-08-19 12:05:58 +03:00
|
|
|
local _filepath="categories/$(echo "$_name" | sed 's/ /-/g' | awk '{print tolower($0)}').html"
|
2020-07-14 15:55:43 +03:00
|
|
|
|
2020-09-01 09:54:29 +03:00
|
|
|
if [[ ! -f $_filepath ]]; then
|
2020-08-19 12:05:58 +03:00
|
|
|
echo "---" > "$_filepath"
|
|
|
|
echo "layout: category" >> "$_filepath"
|
|
|
|
echo "title: $_name" >> "$_filepath"
|
|
|
|
echo "category: $_name" >> "$_filepath"
|
|
|
|
echo "---" >> "$_filepath"
|
2020-07-14 15:55:43 +03:00
|
|
|
|
2020-09-01 09:54:29 +03:00
|
|
|
((category_count = category_count + 1))
|
2020-07-14 15:55:43 +03:00
|
|
|
fi
|
2020-04-09 22:15:51 +03:00
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
|
|
|
create_tag() {
|
2020-09-01 09:54:29 +03:00
|
|
|
if [[ -n $1 ]]; then
|
2020-07-14 15:55:43 +03:00
|
|
|
local _name=$1
|
2020-09-01 09:54:29 +03:00
|
|
|
local _filepath="tags/$(echo "$_name" | sed "s/ /-/g;s/'//g" | awk '{print tolower($0)}').html"
|
2020-04-09 22:15:51 +03:00
|
|
|
|
2020-09-01 09:54:29 +03:00
|
|
|
if [[ ! -f $_filepath ]]; then
|
2020-04-09 22:15:51 +03:00
|
|
|
|
2020-08-19 12:05:58 +03:00
|
|
|
echo "---" > "$_filepath"
|
|
|
|
echo "layout: tag" >> "$_filepath"
|
|
|
|
echo "title: $_name" >> "$_filepath"
|
|
|
|
echo "tag: $_name" >> "$_filepath"
|
|
|
|
echo "---" >> "$_filepath"
|
2020-04-09 22:15:51 +03:00
|
|
|
|
2020-09-01 09:54:29 +03:00
|
|
|
((tag_count = tag_count + 1))
|
2020-07-14 15:55:43 +03:00
|
|
|
fi
|
2020-04-09 22:15:51 +03:00
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
|
|
|
#########################################
|
|
|
|
# Create HTML pages for Categories/Tags.
|
|
|
|
# Arguments:
|
|
|
|
# $1 - an array string
|
|
|
|
# $2 - type specified option
|
|
|
|
#########################################
|
|
|
|
create_pages() {
|
2020-09-01 09:54:29 +03:00
|
|
|
if [[ -n $1 ]]; then
|
2020-07-14 15:55:43 +03:00
|
|
|
# split string to array
|
|
|
|
IFS_BAK=$IFS
|
|
|
|
IFS=','
|
|
|
|
local _string=$1
|
2020-04-09 22:15:51 +03:00
|
|
|
|
2020-07-14 15:55:43 +03:00
|
|
|
case $2 in
|
2020-04-09 22:15:51 +03:00
|
|
|
|
2020-07-14 15:55:43 +03:00
|
|
|
$TYPE_CATEGORY)
|
|
|
|
for i in ${_string#,}; do
|
2020-08-19 12:05:58 +03:00
|
|
|
create_category "$i"
|
2020-07-14 15:55:43 +03:00
|
|
|
done
|
|
|
|
;;
|
2020-04-09 22:15:51 +03:00
|
|
|
|
2020-07-14 15:55:43 +03:00
|
|
|
$TYPE_TAG)
|
|
|
|
for i in ${_string#,}; do
|
2020-08-19 12:05:58 +03:00
|
|
|
create_tag "$i"
|
2020-07-14 15:55:43 +03:00
|
|
|
done
|
|
|
|
;;
|
2020-04-09 22:15:51 +03:00
|
|
|
|
2020-09-01 09:54:29 +03:00
|
|
|
*) ;;
|
2020-04-09 22:15:51 +03:00
|
|
|
|
2020-09-01 09:54:29 +03:00
|
|
|
esac
|
2020-04-09 22:15:51 +03:00
|
|
|
|
2020-09-01 09:54:29 +03:00
|
|
|
IFS=$IFS_BAK
|
2020-07-14 15:55:43 +03:00
|
|
|
fi
|
2020-04-09 22:15:51 +03:00
|
|
|
|
2020-07-14 15:55:43 +03:00
|
|
|
}
|
2020-04-09 22:15:51 +03:00
|
|
|
|
|
|
|
main() {
|
2020-07-13 06:20:07 +03:00
|
|
|
|
2020-04-09 22:15:51 +03:00
|
|
|
init
|
|
|
|
|
2020-09-01 09:54:29 +03:00
|
|
|
for _file in $(find "_posts" -type f \( -iname \*.md -o -iname \*.markdown \)); do
|
2020-07-14 15:55:43 +03:00
|
|
|
local _categories=$(read_categories "$_file")
|
|
|
|
local _tags=$(read_tags "$_file")
|
2020-04-09 22:15:51 +03:00
|
|
|
|
|
|
|
create_pages "$_categories" $TYPE_CATEGORY
|
|
|
|
create_pages "$_tags" $TYPE_TAG
|
|
|
|
done
|
|
|
|
|
|
|
|
if [[ $category_count -gt 0 ]]; then
|
|
|
|
echo "[INFO] Succeed! $category_count category-pages created."
|
|
|
|
fi
|
|
|
|
|
|
|
|
if [[ $tag_count -gt 0 ]]; then
|
|
|
|
echo "[INFO] Succeed! $tag_count tag-pages created."
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
|
|
|
main
|