1919# substr($4, 7)
2020#
2121
22- gh_toc_version=" 0.2.1 "
22+ gh_toc_version=" 0.3.0 "
2323
2424#
2525# Download rendered into html README.md by it's url.
@@ -39,31 +39,68 @@ gh_toc_load() {
3939 fi
4040}
4141
42+ #
43+ # Converts local md file into html by GitHub
44+ #
45+ # ➥ curl -X POST --data '{"text": "Hello world github/linguist#1 **cool**, and #1!"}' https://api.github.com/markdown
46+ # <p>Hello world github/linguist#1 <strong>cool</strong>, and #1!</p>'"
47+ gh_toc_md2html () {
48+ local gh_file_md=$1
49+ local gh_user_agent=$2
50+ curl -s --user-agent " $gh_user_agent " \
51+ --data-binary @$gh_file_md -H " Content-Type:text/plain" \
52+ https://api.github.com/markdown/raw
53+ }
54+
55+ #
56+ # Is passed string url
57+ #
58+ gh_is_url () {
59+ if [[ $1 == https* || $1 == http* ]]; then
60+ echo " yes"
61+ else
62+ echo " no"
63+ fi
64+ }
65+
4266#
4367# TOC generator
4468#
4569gh_toc (){
46- local gh_url=$1
70+ local gh_src=$1
71+ local gh_src_copy=$1
4772 local gh_user_agent=${2:- " gh-md-toc" }
48- local gh_count =$3
73+ local gh_ttl_docs =$3
4974
50- if [ " $gh_url " = " " ]; then
51- echo " Please, enter URL for a README.md"
75+ if [ " $gh_src " = " " ]; then
76+ echo " Please, enter URL or local path for a README.md"
5277 exit 1
5378 fi
5479
55- if [ " $gh_count " = " 1" ]; then
80+
81+ # Show "TOC" string only if working with one document
82+ if [ " $gh_ttl_docs " = " 1" ]; then
5683
5784 echo " Table of Contents"
5885 echo " ================="
5986 echo " "
87+ gh_src_copy=" "
88+
89+ fi
6090
61- gh_toc_load " $gh_url " " $gh_user_agent " | gh_toc_grab " "
91+ if [ " $( gh_is_url $gh_src ) " == " yes" ]; then
92+ gh_toc_load " $gh_src " " $gh_user_agent " | gh_toc_grab " $gh_src_copy "
6293 else
63- gh_toc_load " $gh_url " " $gh_user_agent " | gh_toc_grab " $gh_url "
94+ gh_toc_md2html " $gh_src " " $gh_user_agent " | gh_toc_grab " $gh_src_copy "
6495 fi
6596}
6697
98+ #
99+ # Grabber of the TOC from rendered html
100+ #
101+ # $1 — a source url of document.
102+ # It's need if TOC is generated for multiple documents.
103+ #
67104gh_toc_grab () {
68105 awk -v " gh_url=$1 " ' /user-content-/ {
69106 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) ")"}'
@@ -86,7 +123,7 @@ gh_toc_app() {
86123 echo " GitHub TOC generator ($app_name ): $gh_toc_version "
87124 echo " "
88125 echo " Usage:"
89- echo " $app_name <url> Create TOC for passed url of a README file"
126+ echo " $app_name src [src] Create TOC for a README file (url or local path) "
90127 echo " $app_name --help Show help"
91128 echo " $app_name --version Show help"
92129 return
0 commit comments