From 643c8177be0a188a430d29ff1077ee2ee42209cf Mon Sep 17 00:00:00 2001 From: Honnix Date: Wed, 16 Sep 2020 19:20:04 +0200 Subject: [PATCH] Use yq to process YAML content (#130) Use yq to process YAML content. --- .github/workflows/ci.yml | 4 ++++ .github/workflows/pages-deploy.yml.hook | 8 ++++++++ _scripts/sh/create_pages.sh | 25 ++++++++++++++++--------- 3 files changed, 28 insertions(+), 9 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 49e3309..f1acb42 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -29,6 +29,10 @@ jobs: with: ruby-version: '2.6.x' + - name: install tooling + run: | + brew install yq + - name: Checkout uses: actions/checkout@v2 with: diff --git a/.github/workflows/pages-deploy.yml.hook b/.github/workflows/pages-deploy.yml.hook index 6d3ac74..1733a17 100644 --- a/.github/workflows/pages-deploy.yml.hook +++ b/.github/workflows/pages-deploy.yml.hook @@ -17,6 +17,10 @@ jobs: with: ruby-version: '2.6.x' + - name: install tooling + run: | + brew install yq + - name: Checkout uses: actions/checkout@v2 with: @@ -62,6 +66,10 @@ jobs: with: ruby-version: '2.6.x' + - name: install tooling + run: | + brew install yq + - name: Checkout uses: actions/checkout@v2 with: diff --git a/_scripts/sh/create_pages.sh b/_scripts/sh/create_pages.sh index 0f2e39d..c2cf292 100755 --- a/_scripts/sh/create_pages.sh +++ b/_scripts/sh/create_pages.sh @@ -1,4 +1,4 @@ -#!/usr/local/bin/bash +#!/usr/bin/env bash # # Create HTML pages for Categories and Tags in posts. # @@ -25,19 +25,26 @@ _read_yaml() { read_categories() { local _yaml="$(_read_yaml "$1")" - local _categories="$(echo "$_yaml" | grep "^categories *:")" - local _category="$(echo "$_yaml" | grep "^category *:")" + local _categories="$(echo "$_yaml" | yq r - "categories.*")" + local _category="$(echo "$_yaml" | yq r - "category")" if [[ -n $_categories ]]; then - echo "$_categories" | sed "s/categories *: *//;s/\[//;s/\].*//;s/, */,/g;s/\"//g;s/'//g" + echo "$_categories" elif [[ -n $_category ]]; then - echo "$_category" | sed "s/category *: *//;s/\[//;s/\].*//;s/, */,/g;s/\"//g;s/'//g" + echo "$_category" fi } read_tags() { 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() { @@ -102,19 +109,19 @@ create_pages() { if [[ -n $1 ]]; then # split string to array IFS_BAK=$IFS - IFS=',' + IFS=$'\n' local _string=$1 case $2 in $TYPE_CATEGORY) - for i in ${_string#,}; do + for i in $_string; do create_category "$i" done ;; $TYPE_TAG) - for i in ${_string#,}; do + for i in $_string; do create_tag "$i" done ;;