summaryrefslogtreecommitdiff
path: root/sysutils/gh-md-toc/files/patch-gh-md-toc
blob: c2c5213ec7639f849648f04c49aedd73d553b0cb (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
--- gh-md-toc.orig	2024-03-03 10:54:38 UTC
+++ gh-md-toc
@@ -1,4 +1,4 @@
-#!/usr/bin/env bash
+#!/bin/sh
 
 #
 # Steps:
@@ -34,12 +34,10 @@ gh_toc_load() {
 gh_toc_load() {
     local gh_url=$1
 
-    if type curl &>/dev/null; then
-        curl --user-agent "$gh_user_agent" -s "$gh_url"
-    elif type wget &>/dev/null; then
-        wget --user-agent="$gh_user_agent" -qO- "$gh_url"
+    if type curl > /dev/null 2>&1; then
+        curl --location --fail --user-agent "$gh_user_agent" -s "$gh_url"
     else
-        echo "Please, install 'curl' or 'wget' and try again."
+        echo "Please, install 'curl' and try again."
         exit 1
     fi
 }
@@ -58,7 +56,7 @@ gh_toc_md2html() {
     if [ ! -z "$GH_TOC_TOKEN" ]; then
         TOKEN=$GH_TOC_TOKEN
     else
-        TOKEN_FILE="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)/token.txt"
+        TOKEN_FILE="$(cd "$(dirname "$0")" && pwd)/token.txt"
         if [ -f "$TOKEN_FILE" ]; then
             TOKEN="$(cat $TOKEN_FILE)"
         fi
@@ -109,6 +107,15 @@ gh_is_url() {
     esac
 }
 
+gh_is_invalid_url() {
+    local gh_hostname="github.com"
+
+    case $1 in
+        https://${gh_hostname}/* | http://${gh_hostname}/* ) return 0 ;;
+        *) return 1 ;;
+    esac
+}
+
 #
 # TOC generator
 #
@@ -139,75 +146,112 @@ gh_toc(){
     fi
 
     if [ "$(gh_is_url "$gh_src")" == "yes" ]; then
-        gh_toc_load "$gh_src" | gh_toc_grab "$gh_src_copy" "$indent"
-        if [ "${PIPESTATUS[0]}" != "0" ]; then
+        if ! which -s "jq"; then
+            echo "Please, install 'jq' and try again."
+            exit 1
+        fi
+
+        if ! gh_is_invalid_url "${gh_src}"; then
+            echo "It looks like an invalid URL."
+            echo "Note that valid URLs are, for example, \"https://github.com/<account>/<repo>\"."
+            exit 1
+        fi
+
+        local account repo
+
+        account=$(echo -n "${gh_src}" | sed -Ee 's#https?://github\.com/([^/]+).+#\1#')
+        repo=$(echo -n "${gh_src}" | sed -Ee 's#https?://github\.com/[^/]+/([^/]+)/?.*#\1#')
+
+        local gh_content
+        gh_content=$(gh_toc_load "${gh_src}")
+        if [ $? -ne 0 ]; then
             echo "Could not load remote document."
             echo "Please check your url or network connectivity"
             exit 1
         fi
+
         if [ "$need_replace" = "yes" ]; then
             echo
             echo "!! '$gh_src' is not a local file"
             echo "!! Can't insert the TOC into it."
             echo
+            exit 1
         fi
-    else
-        local 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
+
+        local json_file
+        json_file=$(mktemp -t gh-md-toc)
+        printf "%s\n" "${gh_content}" | grep defaultBranch | sed -Ee 's#<script [^>]+>(.+)</script>#\1#' > "${json_file}"
+
+        local refName path
+
+        refName=$(cat "${json_file}" | jq --raw-output '.props.initialPayload.overview.overviewFiles.[] | select(.preferredFileType == "readme") | .refName' 2> /dev/null)
+        path=$(cat "${json_file}" | jq --raw-output '.props.initialPayload.overview.overviewFiles.[] | select(.preferredFileType == "readme") | .path' 2> /dev/null)
+
+        gh_src=$(mktemp -t gh-md-toc)
+
+        gh_content=`gh_toc_load "https://raw.githubusercontent.com/${account}/${repo}/${refName}/${path}"`
+        if [ $? -ne 0 ]; then
+            echo "Could not load remote document."
+            echo "Please check your url or 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
-        fi
-        local 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
-                echo "Found markers"
-            else
-                echo "You don't have <!--ts--> or <!--te--> in your file...exiting"
-                exit 1
-            fi
-            local ts="<\!--ts-->"
-            local te="<\!--te-->"
-            local dt=`date +'%F_%H%M%S'`
-            local ext=".orig.${dt}"
-            local toc_path="${gh_src}.toc.${dt}"
-            local toc_createdby="<!-- Created by https://github.com/ekalinin/github-markdown-toc -->"
-            local 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}"
-            if [ "${no_footer}" != "yes" ]; then
-                echo -e "\n${toc_createdby}\n${toc_footer}\n" >> "$toc_path"
-            fi
 
-            # insert toc file
-            if ! sed --version > /dev/null 2>&1; then
-                sed -i "" "/${ts}/r ${toc_path}" "$gh_src"
-            else
-                sed -i "/${ts}/r ${toc_path}" "$gh_src"
-            fi
-            echo
-            if [ "${no_backup}" = "yes" ]; then
-                rm "$toc_path" "$gh_src$ext"
-            fi
-            echo "!! TOC was added into: '$gh_src'"
-            if [ -z "${no_backup}" ]; then
-                echo "!! Origin version of the file: '${gh_src}${ext}'"
-                echo "!! TOC added into a separate file: '${toc_path}'"
+        printf "%s\n" "${gh_content}" > "${gh_src}"
+
+        rm -f "${json_file}"
+    fi
+
+    local 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 "$0}")" && pwd)/token.txt"
+         echo "or place GitHub auth token here: ${TOKEN_FILE}"
+         exit 1
+    fi
+    local 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
+            echo "Found markers"
+        else
+            echo "You don't have <!--ts--> or <!--te--> in your file...exiting"
+            exit 1
         fi
-            echo
+        local ts="<\!--ts-->"
+        local te="<\!--te-->"
+        local dt=`date +'%F_%H%M%S'`
+        local ext=".orig.${dt}"
+        local toc_path="${gh_src}.toc.${dt}"
+        local toc_createdby="<!-- Created by https://github.com/ekalinin/github-markdown-toc -->"
+        local 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}"
+        if [ "${no_footer}" != "yes" ]; then
+            echo -e "\n${toc_createdby}\n${toc_footer}\n" >> "$toc_path"
         fi
+
+        # insert toc file
+        sed -i "" "/${ts}/r ${toc_path}" "$gh_src"
+        echo
+        if [ "${no_backup}" = "yes" ]; then
+            rm "$toc_path" "$gh_src$ext"
+        fi
+        echo "!! TOC was added into: '$gh_src'"
+        if [ -z "${no_backup}" ]; then
+            echo "!! Origin version of the file: '${gh_src}${ext}'"
+            echo "!! TOC added into a separate file: '${toc_path}'"
     fi
+        echo
+    fi
 }
 
 #
@@ -218,7 +262,6 @@ gh_toc_grab() {
 # $2 - number of spaces used to indent.
 #
 gh_toc_grab() {
-
     href_regex="/href=\"[^\"]+?\"/"
     common_awk_script='
                      modified_href = ""
@@ -298,19 +341,21 @@ show_version() {
 show_version() {
     echo "$gh_toc_version"
     echo
-    echo "os:     `uname -s`"
+    echo "os:     `uname -rs`"
     echo "arch:   `uname -m`"
-    echo "kernel: `uname -r`"
-    echo "shell:  `$SHELL --version`"
+    echo "kernel: `uname -i`"
+    echo "shell:  $SHELL"
     echo
-    for tool in curl wget grep awk sed; do
+    for tool in curl grep awk jq; do
         printf "%-5s: " $tool
-        if `type $tool &>/dev/null`; then
+        if type $tool > /dev/null 2>&1; then
             echo `$tool --version | head -n 1`
         else
             echo "not installed"
         fi
     done
+    printf "%-5s: " sed
+    echo "sed (BSD sed)"
 }
 
 show_help() {