Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle bad credentials #151

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
124 changes: 69 additions & 55 deletions gh-md-toc
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -70,9 +71,9 @@ gh_toc_md2html() {
local gh_tmp_file_md=$gh_file_md
if [ "$skip_header" = "yes" ]; then
if grep -Fxq "<!--te-->" "$gh_src"; then
# cut everything before the toc
gh_tmp_file_md=$gh_file_md~~
sed '1,/<!--te-->/d' "$gh_file_md" > "$gh_tmp_file_md"
# cut everything before the toc
gh_tmp_file_md=$gh_file_md~~
sed '1,/<!--te-->/d' "$gh_file_md" >"$gh_tmp_file_md"
fi
fi

Expand All @@ -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
Expand All @@ -127,7 +131,6 @@ gh_toc(){
exit 1
fi


# Show "TOC" string only if working with one document
if [ "$gh_ttl_docs" = "1" ]; then

Expand Down Expand Up @@ -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 "<!--ts-->" "$gh_src" && grep -Fxq "<!--te-->" "$gh_src"; then
Expand All @@ -184,18 +200,18 @@ gh_toc(){
local toc_path="${gh_src}.toc.${dt}"
local toc_createdby="<!-- Created by https://github.com/ekalinin/github-markdown-toc -->"
local toc_footer
toc_footer="<!-- Added by: `whoami`, at: `date` -->"
toc_footer="<!-- Added by: $(whoami), at: $(date) -->"
# 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"
Expand All @@ -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
Expand Down Expand Up @@ -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='{
Expand All @@ -270,27 +286,27 @@ gh_toc_grab() {
# became: The command <code>foo1</code></h1>
sed -e ':a' -e 'N' -e '$!ba' -e 's/\n<\/h/<\/h/g' |

# Sometimes a line can start with <span>. Fix that.
sed -e ':a' -e 'N' -e '$!ba' -e 's/\n<span/<span/g' |
# Sometimes a line can start with <span>. Fix that.
sed -e ':a' -e 'N' -e '$!ba' -e 's/\n<span/<span/g' |

# find strings that corresponds to template
$grepcmd '<h.*class="heading-element".*</a' |
# find strings that corresponds to template
$grepcmd '<h.*class="heading-element".*</a' |

# remove code tags
sed 's/<code>//g' | sed 's/<\/code>//g' |
# remove code tags
sed 's/<code>//g' | sed 's/<\/code>//g' |

# remove g-emoji
sed 's/<g-emoji[^>]*[^<]*<\/g-emoji> //g' |
# remove g-emoji
sed 's/<g-emoji[^>]*[^<]*<\/g-emoji> //g' |

# now all rows are like:
# <h1 class="heading-element">title</h1><a href="..."><span>..</span></a>
# format result line
# * $0 - whole string
# * last element of each row: "</hN" where N in (1,2,3,...)
echo $echoargs "$(awk -v "gh_url=$1" "$awkscript")"
# now all rows are like:
# <h1 class="heading-element">title</h1><a href="..."><span>..</span></a>
# format result line
# * $0 - whole string
# * last element of each row: "</hN" where N in (1,2,3,...)
echo $echoargs "$(awk -v "gh_url=$1" "$awkscript")"
}

# perl -lpE 's/(\[[^\]]*\]\()(.*?)(\))/my ($pre, $in, $post)=($1, $2, $3) ; $in =~ s{\+}{ }g; $in =~ s{%}{\\x}g; $pre.$in.$post/ems')"
# perl -lpE 's/(\[[^\]]*\]\()(.*?)(\))/my ($pre, $in, $post)=($1, $2, $3) ; $in =~ s{\+}{ }g; $in =~ s{%}{\\x}g; $pre.$in.$post/ems')"

#
# Returns filename only from full path or url
Expand All @@ -302,10 +318,10 @@ gh_toc_get_filename() {
show_version() {
echo "$gh_toc_version"
echo
echo "os: `uname -s`"
echo "arch: `uname -m`"
echo "kernel: `uname -r`"
echo "shell: `$SHELL --version`"
echo "os: $(uname -s)"
echo "arch: $(uname -m)"
echo "kernel: $(uname -r)"
echo "shell: $($SHELL --version)"
echo
for tool in curl wget grep awk sed; do
printf "%-5s: " $tool
Expand Down Expand Up @@ -346,7 +362,7 @@ gh_toc_app() {
local need_replace="no"
local indent=3

if [ "$1" = '--help' ] || [ $# -eq 0 ] ; then
if [ "$1" = '--help' ] || [ $# -eq 0 ]; then
show_help
return
fi
Expand All @@ -368,15 +384,15 @@ gh_toc_app() {
mkdir -p "$TMPDIR"
fi
local gh_tmp_md
if [ "`uname -s`" == "OS/390" ]; then
if [ "$(uname -s)" == "OS/390" ]; then
local timestamp
timestamp=$(date +%m%d%Y%H%M%S)
gh_tmp_md="$TMPDIR/tmp.$timestamp"
else
gh_tmp_md=$(mktemp "$TMPDIR/tmp.XXXXXX")
fi
while read -r input; do
echo "$input" >> "$gh_tmp_md"
echo "$input" >>"$gh_tmp_md"
done
gh_toc_md2html "$gh_tmp_md" | gh_toc_grab "" "$indent"
return
Expand Down Expand Up @@ -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
Expand Down