From cfa3fda6ee02af6f3474e9f53a32aa12a9232c04 Mon Sep 17 00:00:00 2001 From: elijahr Date: Wed, 5 Jun 2024 16:24:41 -0500 Subject: [PATCH 1/3] Handle bad credentials MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Also fixes a bug where XXNetworkErrorXX wouldn’t be caught due to value of $? changing after rm call. --- gh-md-toc | 31 +++++++++++++++++++++++-------- 1 file changed, 23 insertions(+), 8 deletions(-) diff --git a/gh-md-toc b/gh-md-toc index 35239bf..f707a0c 100755 --- a/gh-md-toc +++ b/gh-md-toc @@ -52,6 +52,7 @@ gh_toc_load() { gh_toc_md2html() { local gh_file_md=$1 local skip_header=$2 + local status=0 URL=https://api.github.com/markdown/raw @@ -82,15 +83,16 @@ gh_toc_md2html() { --data-binary @"$gh_tmp_file_md" \ -H "Content-Type:text/plain" \ -H "$AUTHORIZATION" \ - "$URL") + "$URL" || status=$?) rm -f "${gh_file_md}~~" - if [ "$?" != "0" ]; then + if [ "$status" != "0" ]; then echo "XXNetworkErrorXX" - fi - if [ "$(echo "${OUTPUT}" | awk '/API rate limit exceeded/')" != "" ]; then + elif [ "$(echo "${OUTPUT}" | awk '/API rate limit exceeded/')" != "" ]; then echo "XXRateLimitXX" + elif [ "$(echo "${OUTPUT}" | awk '/Bad credentials/')" != "" ]; then + echo "XXBadCredentialsXX" else echo "${OUTPUT}" fi @@ -158,12 +160,25 @@ gh_toc(){ echo "Parsing local markdown file requires access to github API" echo "Please make sure curl is installed and check your network connectivity" exit 1 - fi - if [ "$rawhtml" == "XXRateLimitXX" ]; then + elif [ "$rawhtml" == "XXRateLimitXX" ]; then echo "Parsing local markdown file requires access to github API" echo "Error: You exceeded the hourly limit. See: https://developer.github.com/v3/#rate-limiting" - TOKEN_FILE="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)/token.txt" - echo "or place GitHub auth token here: ${TOKEN_FILE}" + if [ -n "$GH_TOC_TOKEN" ]; then + echo "Verify correct GitHub auth token in GH_TOC_TOKEN environment variable" + else + TOKEN_FILE="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)/token.txt" + echo "Verify correct GitHub auth token here: ${TOKEN_FILE}" + fi + exit 1 + elif [ "$rawhtml" == "XXBadCredentialsXX" ]; then + echo "Parsing local markdown file requires access to github API" + echo "Error: You provided bad credentials. See: https://developer.github.com/v3/#rate-limiting" + if [ -n "$GH_TOC_TOKEN" ]; then + echo "Verify correct GitHub auth token in GH_TOC_TOKEN environment variable" + else + TOKEN_FILE="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)/token.txt" + echo "Verify correct GitHub auth token here: ${TOKEN_FILE}" + fi exit 1 fi local toc From 2ea20242979c9cbb3bcb68a12d256d8686863b1f Mon Sep 17 00:00:00 2001 From: elijahr Date: Wed, 5 Jun 2024 16:34:34 -0500 Subject: [PATCH 2/3] Fix status check --- gh-md-toc | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/gh-md-toc b/gh-md-toc index f707a0c..27e2a05 100755 --- a/gh-md-toc +++ b/gh-md-toc @@ -52,7 +52,7 @@ gh_toc_load() { gh_toc_md2html() { local gh_file_md=$1 local skip_header=$2 - local status=0 + local status URL=https://api.github.com/markdown/raw @@ -83,7 +83,8 @@ gh_toc_md2html() { --data-binary @"$gh_tmp_file_md" \ -H "Content-Type:text/plain" \ -H "$AUTHORIZATION" \ - "$URL" || status=$?) + "$URL") + status=$? rm -f "${gh_file_md}~~" From d9f75fa9edca452835b6e8b6f6d076ff4db73c85 Mon Sep 17 00:00:00 2001 From: elijahr Date: Wed, 5 Jun 2024 16:34:50 -0500 Subject: [PATCH 3/3] Run through shfmt --- gh-md-toc | 112 +++++++++++++++++++++++++++--------------------------- 1 file changed, 55 insertions(+), 57 deletions(-) diff --git a/gh-md-toc b/gh-md-toc index 27e2a05..d8dc492 100755 --- a/gh-md-toc +++ b/gh-md-toc @@ -71,9 +71,9 @@ gh_toc_md2html() { local gh_tmp_file_md=$gh_file_md if [ "$skip_header" = "yes" ]; then if grep -Fxq "" "$gh_src"; then - # cut everything before the toc - gh_tmp_file_md=$gh_file_md~~ - sed '1,//d' "$gh_file_md" > "$gh_tmp_file_md" + # cut everything before the toc + gh_tmp_file_md=$gh_file_md~~ + sed '1,//d' "$gh_file_md" >"$gh_tmp_file_md" fi fi @@ -99,23 +99,24 @@ gh_toc_md2html() { fi } - # # Is passed string url # gh_is_url() { case $1 in - https* | http*) - echo "yes";; - *) - echo "no";; + https* | http*) + echo "yes" + ;; + *) + echo "no" + ;; esac } # # TOC generator # -gh_toc(){ +gh_toc() { local gh_src=$1 local gh_src_copy=$1 local gh_ttl_docs=$2 @@ -130,7 +131,6 @@ gh_toc(){ exit 1 fi - # Show "TOC" string only if working with one document if [ "$gh_ttl_docs" = "1" ]; then @@ -158,32 +158,32 @@ gh_toc(){ local rawhtml rawhtml=$(gh_toc_md2html "$gh_src" "$skip_header") if [ "$rawhtml" == "XXNetworkErrorXX" ]; then - echo "Parsing local markdown file requires access to github API" - echo "Please make sure curl is installed and check your network connectivity" - exit 1 + echo "Parsing local markdown file requires access to github API" + echo "Please make sure curl is installed and check your network connectivity" + exit 1 elif [ "$rawhtml" == "XXRateLimitXX" ]; then - echo "Parsing local markdown file requires access to github API" - echo "Error: You exceeded the hourly limit. See: https://developer.github.com/v3/#rate-limiting" - if [ -n "$GH_TOC_TOKEN" ]; then + echo "Parsing local markdown file requires access to github API" + echo "Error: You exceeded the hourly limit. See: https://developer.github.com/v3/#rate-limiting" + if [ -n "$GH_TOC_TOKEN" ]; then echo "Verify correct GitHub auth token in GH_TOC_TOKEN environment variable" - else + else TOKEN_FILE="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)/token.txt" echo "Verify correct GitHub auth token here: ${TOKEN_FILE}" - fi - exit 1 + fi + exit 1 elif [ "$rawhtml" == "XXBadCredentialsXX" ]; then - echo "Parsing local markdown file requires access to github API" - echo "Error: You provided bad credentials. See: https://developer.github.com/v3/#rate-limiting" - if [ -n "$GH_TOC_TOKEN" ]; then + echo "Parsing local markdown file requires access to github API" + echo "Error: You provided bad credentials. See: https://developer.github.com/v3/#rate-limiting" + if [ -n "$GH_TOC_TOKEN" ]; then echo "Verify correct GitHub auth token in GH_TOC_TOKEN environment variable" - else + else TOKEN_FILE="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)/token.txt" echo "Verify correct GitHub auth token here: ${TOKEN_FILE}" - fi - exit 1 + fi + exit 1 fi local toc - toc=`echo "$rawhtml" | gh_toc_grab "$gh_src_copy" "$indent"` + toc=$(echo "$rawhtml" | gh_toc_grab "$gh_src_copy" "$indent") echo "$toc" if [ "$need_replace" = "yes" ]; then if grep -Fxq "" "$gh_src" && grep -Fxq "" "$gh_src"; then @@ -200,18 +200,18 @@ gh_toc(){ local toc_path="${gh_src}.toc.${dt}" local toc_createdby="" local toc_footer - toc_footer="" + toc_footer="" # http://fahdshariff.blogspot.ru/2012/12/sed-mutli-line-replacement-between-two.html # clear old TOC sed -i"${ext}" "/${ts}/,/${te}/{//!d;}" "$gh_src" # create toc file - echo "${toc}" > "${toc_path}" + echo "${toc}" >"${toc_path}" if [ "${no_footer}" != "yes" ]; then - echo -e "\n${toc_createdby}\n${toc_footer}\n" >> "$toc_path" + echo -e "\n${toc_createdby}\n${toc_footer}\n" >>"$toc_path" fi # insert toc file - if ! sed --version > /dev/null 2>&1; then + if ! sed --version || true >/dev/null 2>&1; then sed -i "" "/${ts}/r ${toc_path}" "$gh_src" else sed -i "/${ts}/r ${toc_path}" "$gh_src" @@ -224,7 +224,7 @@ gh_toc(){ if [ -z "${no_backup}" ]; then echo "!! Origin version of the file: '${gh_src}${ext}'" echo "!! TOC added into a separate file: '${toc_path}'" - fi + fi echo fi fi @@ -259,7 +259,7 @@ gh_toc_grab() { } print sprintf("%*s", (level-1)*'"$2"', "") "* [" text "](" gh_url modified_href ")" ' - if [ "`uname -s`" == "OS/390" ]; then + if [ "$(uname -s)" == "OS/390" ]; then grepcmd="pcregrep -o" echoargs="" awkscript='{ @@ -286,27 +286,27 @@ gh_toc_grab() { # became: The command foo1 sed -e ':a' -e 'N' -e '$!ba' -e 's/\n<\/h/<\/h/g' | - # Sometimes a line can start with . Fix that. - sed -e ':a' -e 'N' -e '$!ba' -e 's/\n. Fix that. + sed -e ':a' -e 'N' -e '$!ba' -e 's/\n//g' | sed 's/<\/code>//g' | + # remove code tags + sed 's///g' | sed 's/<\/code>//g' | - # remove g-emoji - sed 's/]*[^<]*<\/g-emoji> //g' | + # remove g-emoji + sed 's/]*[^<]*<\/g-emoji> //g' | - # now all rows are like: - #

title

.. - # format result line - # * $0 - whole string - # * last element of each row: "title.. + # format result line + # * $0 - whole string + # * last element of each row: "> "$gh_tmp_md" + echo "$input" >>"$gh_tmp_md" done gh_toc_md2html "$gh_tmp_md" | gh_toc_grab "" "$indent" return @@ -420,9 +420,7 @@ gh_toc_app() { shift fi - - for md in "$@" - do + for md in "$@"; do echo "" gh_toc "$md" "$#" "$need_replace" "$no_backup" "$no_footer" "$indent" "$skip_header" done