Use yq to process YAML content (#130)

Use yq to process YAML content.
This commit is contained in:
Honnix 2020-09-16 19:20:04 +02:00 committed by GitHub
parent 092ab5bbdb
commit 643c8177be
3 changed files with 28 additions and 9 deletions

View File

@ -29,6 +29,10 @@ jobs:
with: with:
ruby-version: '2.6.x' ruby-version: '2.6.x'
- name: install tooling
run: |
brew install yq
- name: Checkout - name: Checkout
uses: actions/checkout@v2 uses: actions/checkout@v2
with: with:

View File

@ -17,6 +17,10 @@ jobs:
with: with:
ruby-version: '2.6.x' ruby-version: '2.6.x'
- name: install tooling
run: |
brew install yq
- name: Checkout - name: Checkout
uses: actions/checkout@v2 uses: actions/checkout@v2
with: with:
@ -62,6 +66,10 @@ jobs:
with: with:
ruby-version: '2.6.x' ruby-version: '2.6.x'
- name: install tooling
run: |
brew install yq
- name: Checkout - name: Checkout
uses: actions/checkout@v2 uses: actions/checkout@v2
with: with:

View File

@ -1,4 +1,4 @@
#!/usr/local/bin/bash #!/usr/bin/env bash
# #
# Create HTML pages for Categories and Tags in posts. # Create HTML pages for Categories and Tags in posts.
# #
@ -25,19 +25,26 @@ _read_yaml() {
read_categories() { read_categories() {
local _yaml="$(_read_yaml "$1")" local _yaml="$(_read_yaml "$1")"
local _categories="$(echo "$_yaml" | grep "^categories *:")" local _categories="$(echo "$_yaml" | yq r - "categories.*")"
local _category="$(echo "$_yaml" | grep "^category *:")" local _category="$(echo "$_yaml" | yq r - "category")"
if [[ -n $_categories ]]; then if [[ -n $_categories ]]; then
echo "$_categories" | sed "s/categories *: *//;s/\[//;s/\].*//;s/, */,/g;s/\"//g;s/'//g" echo "$_categories"
elif [[ -n $_category ]]; then elif [[ -n $_category ]]; then
echo "$_category" | sed "s/category *: *//;s/\[//;s/\].*//;s/, */,/g;s/\"//g;s/'//g" echo "$_category"
fi fi
} }
read_tags() { 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" local _tags="$(echo "$_yaml" | yq r - "tags.*")"
local _tag="$(echo "$_yaml" | yq r - "tag")"
if [[ -n $_tags ]]; then
echo "$_tags"
elif [[ -n $_tag ]]; then
echo "$_tag"
fi
} }
init() { init() {
@ -102,19 +109,19 @@ create_pages() {
if [[ -n $1 ]]; then if [[ -n $1 ]]; then
# split string to array # split string to array
IFS_BAK=$IFS IFS_BAK=$IFS
IFS=',' IFS=$'\n'
local _string=$1 local _string=$1
case $2 in case $2 in
$TYPE_CATEGORY) $TYPE_CATEGORY)
for i in ${_string#,}; do for i in $_string; do
create_category "$i" create_category "$i"
done done
;; ;;
$TYPE_TAG) $TYPE_TAG)
for i in ${_string#,}; do for i in $_string; do
create_tag "$i" create_tag "$i"
done done
;; ;;