Skip to content

Commit df36e23

Browse files
authored
Use yq to process YAML content (cotes2020#130)
Use yq to process YAML content.
1 parent 9919a64 commit df36e23

File tree

3 files changed

+28
-9
lines changed

3 files changed

+28
-9
lines changed

.github/workflows/ci.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,10 @@ jobs:
2929
with:
3030
ruby-version: '2.6.x'
3131

32+
- name: install tooling
33+
run: |
34+
brew install yq
35+
3236
- name: Checkout
3337
uses: actions/checkout@v2
3438
with:

.github/workflows/pages-deploy.yml.hook

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@ jobs:
1717
with:
1818
ruby-version: '2.6.x'
1919

20+
- name: install tooling
21+
run: |
22+
brew install yq
23+
2024
- name: Checkout
2125
uses: actions/checkout@v2
2226
with:
@@ -62,6 +66,10 @@ jobs:
6266
with:
6367
ruby-version: '2.6.x'
6468

69+
- name: install tooling
70+
run: |
71+
brew install yq
72+
6573
- name: Checkout
6674
uses: actions/checkout@v2
6775
with:

_scripts/sh/create_pages.sh

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#!/usr/local/bin/bash
1+
#!/usr/bin/env bash
22
#
33
# Create HTML pages for Categories and Tags in posts.
44
#
@@ -25,19 +25,26 @@ _read_yaml() {
2525

2626
read_categories() {
2727
local _yaml="$(_read_yaml "$1")"
28-
local _categories="$(echo "$_yaml" | grep "^categories *:")"
29-
local _category="$(echo "$_yaml" | grep "^category *:")"
28+
local _categories="$(echo "$_yaml" | yq r - "categories.*")"
29+
local _category="$(echo "$_yaml" | yq r - "category")"
3030

3131
if [[ -n $_categories ]]; then
32-
echo "$_categories" | sed "s/categories *: *//;s/\[//;s/\].*//;s/, */,/g;s/\"//g;s/'//g"
32+
echo "$_categories"
3333
elif [[ -n $_category ]]; then
34-
echo "$_category" | sed "s/category *: *//;s/\[//;s/\].*//;s/, */,/g;s/\"//g;s/'//g"
34+
echo "$_category"
3535
fi
3636
}
3737

3838
read_tags() {
3939
local _yaml="$(_read_yaml "$1")"
40-
echo "$_yaml" | grep "^tags *:" | sed "s/tags *: *//;s/\[//;s/\].*//;s/, */,/g;s/\"//g;s/'//g"
40+
local _tags="$(echo "$_yaml" | yq r - "tags.*")"
41+
local _tag="$(echo "$_yaml" | yq r - "tag")"
42+
43+
if [[ -n $_tags ]]; then
44+
echo "$_tags"
45+
elif [[ -n $_tag ]]; then
46+
echo "$_tag"
47+
fi
4148
}
4249

4350
init() {
@@ -102,19 +109,19 @@ create_pages() {
102109
if [[ -n $1 ]]; then
103110
# split string to array
104111
IFS_BAK=$IFS
105-
IFS=','
112+
IFS=$'\n'
106113
local _string=$1
107114

108115
case $2 in
109116

110117
$TYPE_CATEGORY)
111-
for i in ${_string#,}; do
118+
for i in $_string; do
112119
create_category "$i"
113120
done
114121
;;
115122

116123
$TYPE_TAG)
117-
for i in ${_string#,}; do
124+
for i in $_string; do
118125
create_tag "$i"
119126
done
120127
;;

0 commit comments

Comments
 (0)