diff --git a/gh-md-toc b/gh-md-toc
index 35239bf..d8dc492 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
URL=https://api.github.com/markdown/raw
@@ -70,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
@@ -83,36 +84,39 @@ gh_toc_md2html() {
-H "Content-Type:text/plain" \
-H "$AUTHORIZATION" \
"$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
}
-
#
# 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
@@ -127,7 +131,6 @@ gh_toc(){
exit 1
fi
-
# Show "TOC" string only if working with one document
if [ "$gh_ttl_docs" = "1" ]; then
@@ -155,19 +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
- fi
- if [ "$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}"
- 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 "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
- 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
@@ -184,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"
@@ -208,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
@@ -243,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='{
@@ -270,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
@@ -404,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