Skip to content

Commit c9ec631

Browse files
committed
added support for multi input; fixes #2
1 parent 18991e2 commit c9ec631

File tree

1 file changed

+35
-6
lines changed

1 file changed

+35
-6
lines changed

gh-md-toc

Lines changed: 35 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
# substr($4, 7)
2020
#
2121

22-
gh_toc_version="0.1.1"
22+
gh_toc_version="0.2.0"
2323

2424
#
2525
# Download rendered into html README.md by it's url.
@@ -45,16 +45,40 @@ gh_toc_load() {
4545
gh_toc(){
4646
local gh_url=$1
4747
local gh_user_agent=${2:-"gh-md-toc"}
48+
local gh_count=$3
4849

4950
if [ "$gh_url" = "" ]; then
5051
echo "Please, enter URL for a README.md"
5152
exit 1
5253
fi
5354

54-
echo "Table of Contents"
55-
echo "================="
56-
gh_toc_load "$gh_url" "$gh_user_agent" | \
57-
awk '/user-content-/ {print sprintf("%*s", substr($NF, length($NF)-1, 1)*2, " ") "* [" substr($0, match($0, /a>.*<\/h/)+2, RLENGTH-5)"](" substr($4, 7, length($4)-7) ")"}'
55+
if [ "$gh_count" = "1" ]; then
56+
57+
echo "Table of Contents"
58+
echo "================="
59+
echo ""
60+
61+
gh_toc_load "$gh_url" "$gh_user_agent" | gh_toc_grab_single
62+
else
63+
gh_toc_load "$gh_url" "$gh_user_agent" | gh_toc_grab_multi
64+
fi
65+
}
66+
67+
gh_toc_grab_single() {
68+
awk -v "gh_url=$gh_url" '/user-content-/ {
69+
print sprintf("%*s", substr($NF, length($NF)-1, 1)*2, " ") "* [" substr($0, match($0, /a>.*<\/h/)+2, RLENGTH-5)"](" substr($4, 7, length($4)-7) ")"}'
70+
}
71+
72+
gh_toc_grab_multi() {
73+
awk -v "gh_url=$gh_url" '/user-content-/ {
74+
print sprintf("%*s", substr($NF, length($NF)-1, 1)*2, " ") "* [" substr($0, match($0, /a>.*<\/h/)+2, RLENGTH-5)"](" gh_url substr($4, 7, length($4)-7) ")"}'
75+
}
76+
77+
#
78+
# Returns filename only from full path or url
79+
#
80+
gh_toc_get_filename() {
81+
echo "${1##*/}"
5882
}
5983

6084
#
@@ -78,7 +102,12 @@ gh_toc_app() {
78102
return
79103
fi
80104

81-
gh_toc "$1" "$app_name"
105+
for md in "$@"
106+
do
107+
echo ""
108+
gh_toc "$md" "$app_name" "$#"
109+
done
110+
82111
}
83112

84113
#

0 commit comments

Comments
 (0)