1414# substr($0, length($0), 1)
1515#
1616# 3.2 Get level from 3.1 and insert corresponding number of spaces before '*':
17- # sprintf("%*s", substr($0, length($0), 1)*3 , " ")
17+ # sprintf("%*s", (level- 1)*'"$nb_spaces"' , "")
1818#
1919# 4. Find head's text and insert it inside "* [ ... ]":
2020# substr($0, match($0, /a>.*<\/h/)+2, RLENGTH-5)
@@ -119,7 +119,8 @@ gh_toc(){
119119 local need_replace=$3
120120 local no_backup=$4
121121 local no_footer=$5
122- local skip_header=$6
122+ local indent=$6
123+ local skip_header=$7
123124
124125 if [ " $gh_src " = " " ]; then
125126 echo " Please, enter URL or local path for a README.md"
@@ -138,7 +139,7 @@ gh_toc(){
138139 fi
139140
140141 if [ " $( gh_is_url " $gh_src " ) " == " yes" ]; then
141- gh_toc_load " $gh_src " | gh_toc_grab " $gh_src_copy "
142+ gh_toc_load " $gh_src " | gh_toc_grab " $gh_src_copy " " $indent "
142143 if [ " ${PIPESTATUS[0]} " != " 0" ]; then
143144 echo " Could not load remote document."
144145 echo " Please check your url or network connectivity"
@@ -164,7 +165,7 @@ gh_toc(){
164165 echo " or place GitHub auth token here: ${TOKEN_FILE} "
165166 exit 1
166167 fi
167- local toc=` echo " $rawhtml " | gh_toc_grab " $gh_src_copy " `
168+ local toc=` echo " $rawhtml " | gh_toc_grab " $gh_src_copy " " $indent " `
168169 echo " $toc "
169170 if [ " $need_replace " = " yes" ]; then
170171 if grep -Fxq " <!--ts-->" $gh_src && grep -Fxq " <!--te-->" $gh_src ; then
@@ -178,18 +179,19 @@ gh_toc(){
178179 local dt=` date +' %F_%H%M%S' `
179180 local ext=" .orig.${dt} "
180181 local toc_path=" ${gh_src} .toc.${dt} "
182+ local toc_createdby=" <!-- Created by https://github.com/ekalinin/github-markdown-toc -->"
181183 local toc_footer=" <!-- Added by: ` whoami` , at: ` date` -->"
182184 # http://fahdshariff.blogspot.ru/2012/12/sed-mutli-line-replacement-between-two.html
183185 # clear old TOC
184186 sed -i${ext} " /${ts} /,/${te} /{//!d;}" " $gh_src "
185187 # create toc file
186188 echo " ${toc} " > " ${toc_path} "
187189 if [ " ${no_footer} " != " yes" ]; then
188- echo -e " \n${toc_footer} \n" >> " $toc_path "
190+ echo -e " \n${toc_createdby} \n ${ toc_footer} \n" >> " $toc_path "
189191 fi
190192
191193 # insert toc file
192- if [[ " ` uname ` " == " Darwin " ]] ; then
194+ if ! sed --version > /dev/null 2>&1 ; then
193195 sed -i " " " /${ts} /r ${toc_path} " " $gh_src "
194196 else
195197 sed -i " /${ts} /r ${toc_path} " " $gh_src "
@@ -212,7 +214,8 @@ gh_toc(){
212214# Grabber of the TOC from rendered html
213215#
214216# $1 - a source url of document.
215- # It's need if TOC is generated for multiple documents.
217+ # It's need if TOC is generated for multiple documents.
218+ # $2 - number of spaces used to indent.
216219#
217220gh_toc_grab () {
218221 common_awk_script='
@@ -232,7 +235,7 @@ gh_toc_grab() {
232235 }
233236 modified_href = modified_href res
234237 }
235- print sprintf("%*s", (level-1)*3 , "") "* [" text "](" gh_url modified_href ")"
238+ print sprintf("%*s", (level-1)*' " $2 " ' , "") "* [" text "](" gh_url modified_href ")"
236239 '
237240 if [ ` uname -s` == " OS/390" ]; then
238241 grepcmd=" pcregrep -o"
@@ -293,14 +296,15 @@ gh_toc_get_filename() {
293296#
294297gh_toc_app () {
295298 local need_replace=" no"
299+ local indent=3
296300
297301 if [ " $1 " = ' --help' ] || [ $# -eq 0 ] ; then
298302 local app_name=$( basename " $0 " )
299303 echo " GitHub TOC generator ($app_name ): $gh_toc_version "
300304 echo " "
301305 echo " Usage:"
302- echo " $app_name [--insert] [--hide-footer] [--skip-header] src [src] Create TOC for a README file (url or local path)"
303- echo " $app_name [--no-backup] [--hide-footer] [--skip-header] src [src] Create TOC without backup, requires <!--ts--> / <!--te--> placeholders"
306+ echo " $app_name [--insert] [--hide-footer] [--skip-header] [--indent #spaces] src [src] Create TOC for a README file (url or local path)"
307+ echo " $app_name [--no-backup] [--hide-footer] [--skip-header] [--indent #spaces] src [src] Create TOC without backup, requires <!--ts--> / <!--te--> placeholders"
304308 echo " $app_name - Create TOC for markdown from STDIN"
305309 echo " $app_name --help Show help"
306310 echo " $app_name --version Show version"
@@ -321,6 +325,11 @@ gh_toc_app() {
321325 return
322326 fi
323327
328+ if [ " $1 " = ' --indent' ]; then
329+ indent=" $2 "
330+ shift
331+ fi
332+
324333 if [ " $1 " = " -" ]; then
325334 if [ -z " $TMPDIR " ]; then
326335 TMPDIR=" /tmp"
@@ -337,7 +346,7 @@ gh_toc_app() {
337346 while read input; do
338347 echo " $input " >> " $gh_tmp_md "
339348 done
340- gh_toc_md2html " $gh_tmp_md " | gh_toc_grab " "
349+ gh_toc_md2html " $gh_tmp_md " | gh_toc_grab " " " $indent "
341350 return
342351 fi
343352
@@ -367,11 +376,11 @@ gh_toc_app() {
367376 for md in " $@ "
368377 do
369378 echo " "
370- gh_toc " $md " " $# " " $need_replace " " $no_backup " " $no_footer " " $skip_header "
379+ gh_toc " $md " " $# " " $need_replace " " $no_backup " " $no_footer " " $indent " " $ skip_header"
371380 done
372381
373382 echo " "
374- echo " Created by [gh-md-toc]( https://github.com/ekalinin/github-markdown-toc) "
383+ echo " <!-- Created by https://github.com/ekalinin/github-markdown-toc --> "
375384}
376385
377386#
0 commit comments