forked from drphilmarshall/GettingStarted
-
Notifications
You must be signed in to change notification settings - Fork 109
/
make_toc.csh
executable file
·114 lines (92 loc) · 2.15 KB
/
make_toc.csh
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
#!/bin/tcsh
#=============================================================================
#+
# NAME:
# make_toc.csh
#
# PURPOSE:
# Output a markdown table of contents based on the content of the
# README.md file.
#
# COMMENTS:
#
# INPUTS:
#
# OPTIONAL INPUTS:
# -f filename Input file [README.md].
# -h --help
#
# OUTPUTS:
# stdout
#
# EXAMPLES:
#
# make_toc.csh -f README.md
#
# DEPENDENCIES:
#
# BUGS:
#
# REVISION HISTORY:
# 2015-07-09 started Marshall (KIPACF)
#-
#=======================================================================
unset noclobber
# Set defaults:
set help = 0
set file = README.md
# Parse command line:
while ( $#argv > 0 )
switch ($argv[1])
case -h: # print help
set help = 1
shift argv
breaksw
case --{help}:
set help = 1
shift argv
breaksw
case -f: # alternative input file
shift argv
set file = $argv[1]
shift argv
breaksw
case *:
shift argv
breaksw
endsw
end
#-----------------------------------------------------------------------
if ($help) then
more `which $0`
goto FINISH
endif
#-----------------------------------------------------------------------
# Get list of FAQ, marked with '####':
set toclist = /tmp/toclist
\rm -f $toclist
grep '####' $file > $toclist
# For each of these headings, extract the anchor name and the title text:
set nlines = `cat $toclist | wc -l`
foreach k ( `seq $nlines`)
set line = `tail -n +$k $toclist | head -1`
# Is an anchor defined? If not, use a blank string:
set gotanchor = `echo "$line" | grep 'name=' | wc -l`
if ($gotanchor) then
set anchor = `echo "$line" | cut -d'"' -f2`
else
set anchor = ''
endif
# Pull out title text:
if ($gotanchor) then
set text = `echo "${line}" | sed s%'</a>'%'+'%g | cut -d'+' -f2`
else
set text = `echo "${line}" | sed s%'####'%'+'%g | cut -d'+' -f2`
endif
# Print formatted markdown line to stdout:
echo '* ['"${text}"'](#'"${anchor}"')'
end
#-----------------------------------------------------------------------
# Clean up:
\rm -f $toclist
finish: