@@ -216,49 +216,49 @@ gh_toc_grab() {
216216 }
217217 print sprintf("%*s", level*3, " ") "* [" text "](" gh_url modified_href ")"
218218 '
219- if [ ` uname -s` == " OS/390" ]; then
220- grepcmd=" pcregrep -o"
221- echoargs=" "
222- awkscript=' {
219+ if [ ` uname -s` == " OS/390" ]; then
220+ grepcmd=" pcregrep -o"
221+ echoargs=" "
222+ awkscript=' {
223223 level = substr($0, length($0), 1)
224224 text = substr($0, match($0, /a>.*<\/h/)+2, RLENGTH-5)
225225 href = substr($0, match($0, "href=\"([^\"]+)?\"")+6, RLENGTH-7)
226226 ' " $common_awk_script " '
227227 }'
228- else
229- grepcmd=" grep -Eo"
230- echoargs=" -e"
231- awkscript=' {
228+ else
229+ grepcmd=" grep -Eo"
230+ echoargs=" -e"
231+ awkscript=' {
232232 level = substr($0, length($0), 1)
233233 text = substr($0, match($0, /a>.*<\/h/)+2, RLENGTH-5)
234234 href = substr($0, match($0, "href=\"[^\"]+?\"")+6, RLENGTH-7)
235235 ' " $common_awk_script " '
236236 }'
237- fi
238- href_regex=' href=\"[^\"]+?\"'
239-
240- # if closed <h[1-6]> is on the new line, then move it on the prev line
241- # for example:
242- # was: The command <code>foo1</code>
243- # </h1>
244- # became: The command <code>foo1</code></h1>
245- sed -e ' :a' -e ' N' -e ' $!ba' -e ' s/\n<\/h/<\/h/g' |
246-
247- # find strings that corresponds to template
248- $grepcmd ' <a.*id="user-content-[^"]*".*</h[1-6]' |
249-
250- # remove code tags
251- sed ' s/<code>//g' | sed ' s/<\/code>//g' |
252-
253- # remove g-emoji
254- sed ' s/<g-emoji[^>]*[^<]*<\/g-emoji> //g' |
255-
256- # now all rows are like:
257- # <a id="user-content-..." href="..."><span ...></span></a> ... </h1
258- # format result line
259- # * $0 - whole string
260- # * last element of each row: "</hN" where N in (1,2,3,...)
261- echo $echoargs " $( awk -v " gh_url=$1 " " $awkscript " ) "
237+ fi
238+ href_regex=' href=\"[^\"]+?\"'
239+
240+ # if closed <h[1-6]> is on the new line, then move it on the prev line
241+ # for example:
242+ # was: The command <code>foo1</code>
243+ # </h1>
244+ # became: The command <code>foo1</code></h1>
245+ sed -e ' :a' -e ' N' -e ' $!ba' -e ' s/\n<\/h/<\/h/g' |
246+
247+ # find strings that corresponds to template
248+ $grepcmd ' <a.*id="user-content-[^"]*".*</h[1-6]' |
249+
250+ # remove code tags
251+ sed ' s/<code>//g' | sed ' s/<\/code>//g' |
252+
253+ # remove g-emoji
254+ sed ' s/<g-emoji[^>]*[^<]*<\/g-emoji> //g' |
255+
256+ # now all rows are like:
257+ # <a id="user-content-..." href="..."><span ...></span></a> ... </h1
258+ # format result line
259+ # * $0 - whole string
260+ # * last element of each row: "</hN" where N in (1,2,3,...)
261+ echo $echoargs " $( awk -v " gh_url=$1 " " $awkscript " ) "
262262}
263263
264264# perl -lpE 's/(\[[^\]]*\]\()(.*?)(\))/my ($pre, $in, $post)=($1, $2, $3) ; $in =~ s{\+}{ }g; $in =~ s{%}{\\x}g; $pre.$in.$post/ems')"
@@ -267,80 +267,80 @@ echo $echoargs "$(awk -v "gh_url=$1" "$awkscript")"
267267# Returns filename only from full path or url
268268#
269269gh_toc_get_filename () {
270- echo " ${1##*/ } "
270+ echo " ${1##*/ } "
271271}
272272
273273#
274274# Options handlers
275275#
276276gh_toc_app () {
277- local need_replace=" no"
277+ local need_replace=" no"
278278
279- if [ " $1 " = ' --help' ] || [ $# -eq 0 ] ; then
280- local app_name=$( basename " $0 " )
281- echo " GitHub TOC generator ($app_name ): $gh_toc_version "
282- echo " "
283- echo " Usage:"
284- echo " $app_name [--insert] src [src] Create TOC for a README file (url or local path)"
285- echo " $app_name [--no-backup] src [src] Create TOC without backup, requires <!--ts--> / <!--te--> placeholders"
286- echo " $app_name - Create TOC for markdown from STDIN"
287- echo " $app_name --help Show help"
288- echo " $app_name --version Show version"
289- return
290- fi
291-
292- if [ " $1 " = ' --version' ]; then
293- echo " $gh_toc_version "
294- echo
295- echo " os: ` lsb_release -d | cut -f 2` "
296- echo " kernel: ` cat /proc/version` "
297- echo " shell: ` $SHELL --version` "
298- echo
299- for tool in curl wget grep awk sed; do
300- printf " %-5s: " $tool
301- echo ` $tool --version | head -n 1`
302- done
303- return
304- fi
305-
306- if [ " $1 " = " -" ]; then
307- if [ -z " $TMPDIR " ]; then
308- TMPDIR=" /tmp"
309- elif [ -n " $TMPDIR " -a ! -d " $TMPDIR " ]; then
310- mkdir -p " $TMPDIR "
279+ if [ " $1 " = ' --help' ] || [ $# -eq 0 ] ; then
280+ local app_name=$( basename " $0 " )
281+ echo " GitHub TOC generator ($app_name ): $gh_toc_version "
282+ echo " "
283+ echo " Usage:"
284+ echo " $app_name [--insert] src [src] Create TOC for a README file (url or local path)"
285+ echo " $app_name [--no-backup] src [src] Create TOC without backup, requires <!--ts--> / <!--te--> placeholders"
286+ echo " $app_name - Create TOC for markdown from STDIN"
287+ echo " $app_name --help Show help"
288+ echo " $app_name --version Show version"
289+ return
311290 fi
312- local gh_tmp_md
313- if [ ` uname -s` == " OS/390" ]; then
314- local timestamp=$( date +%m%d%Y%H%M%S)
315- gh_tmp_md=" $TMPDIR /tmp.$timestamp "
316- else
317- gh_tmp_md=$( mktemp $TMPDIR /tmp.XXXXXX)
291+
292+ if [ " $1 " = ' --version' ]; then
293+ echo " $gh_toc_version "
294+ echo
295+ echo " os: ` lsb_release -d | cut -f 2` "
296+ echo " kernel: ` cat /proc/version` "
297+ echo " shell: ` $SHELL --version` "
298+ echo
299+ for tool in curl wget grep awk sed; do
300+ printf " %-5s: " $tool
301+ echo ` $tool --version | head -n 1`
302+ done
303+ return
304+ fi
305+
306+ if [ " $1 " = " -" ]; then
307+ if [ -z " $TMPDIR " ]; then
308+ TMPDIR=" /tmp"
309+ elif [ -n " $TMPDIR " -a ! -d " $TMPDIR " ]; then
310+ mkdir -p " $TMPDIR "
311+ fi
312+ local gh_tmp_md
313+ if [ ` uname -s` == " OS/390" ]; then
314+ local timestamp=$( date +%m%d%Y%H%M%S)
315+ gh_tmp_md=" $TMPDIR /tmp.$timestamp "
316+ else
317+ gh_tmp_md=$( mktemp $TMPDIR /tmp.XXXXXX)
318+ fi
319+ while read input; do
320+ echo " $input " >> " $gh_tmp_md "
321+ done
322+ gh_toc_md2html " $gh_tmp_md " | gh_toc_grab " "
323+ return
324+ fi
325+
326+ if [ " $1 " = ' --insert' ]; then
327+ need_replace=" yes"
328+ shift
318329 fi
319- while read input; do
320- echo " $input " >> " $gh_tmp_md "
330+
331+ if [ " $1 " = ' --no-backup' ]; then
332+ need_replace=" yes"
333+ no_backup=" yes"
334+ shift
335+ fi
336+ for md in " $@ "
337+ do
338+ echo " "
339+ gh_toc " $md " " $# " " $need_replace " " $no_backup "
321340 done
322- gh_toc_md2html " $gh_tmp_md " | gh_toc_grab " "
323- return
324- fi
325-
326- if [ " $1 " = ' --insert' ]; then
327- need_replace=" yes"
328- shift
329- fi
330-
331- if [ " $1 " = ' --no-backup' ]; then
332- need_replace=" yes"
333- no_backup=" yes"
334- shift
335- fi
336- for md in " $@ "
337- do
338- echo " "
339- gh_toc " $md " " $# " " $need_replace " " $no_backup "
340- done
341341
342- echo " "
343- echo " Created by [gh-md-toc](https://github.com/ekalinin/github-markdown-toc)"
342+ echo " "
343+ echo " Created by [gh-md-toc](https://github.com/ekalinin/github-markdown-toc)"
344344}
345345
346346#
0 commit comments