forked from alex-harvey-z3q/puppet-firewall_multi
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmdtoc.rb
More file actions
90 lines (75 loc) · 1.67 KB
/
mdtoc.rb
File metadata and controls
90 lines (75 loc) · 1.67 KB
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
#!/usr/bin/env ruby
TOP = 2; MAX = 4
def usage
puts <<-EOF
Usage: #{$0} FILE.md [TOP [MAX]] [-h]
FILE.md Markdown source document
TOP top-level Markdown heading level (default #{TOP})
MAX maximum heading level (default #{MAX})
EOF
exit 1
end
class ToCWriter
def initialize(source_file, top=TOP, max=MAX)
@source_file = source_file
@top = top.to_i
@max = max.to_i
@count = 1
@level = ""
@header = ""
@start = ""
@anchor = ""
@seen = []
end
def write
puts "#### Table of contents\n\n"
File.open(@source_file).each_line do |line|
next unless line.match(/^#/)
@level, @header = line.match(/^(#+) *(.*) *$/).captures
next if ignore_this_header?
set_anchor
set_start
puts "#{@start} [#{@header}](##{@anchor})"
end
end
private
def ignore_this_header?
@header == "Table of contents" || \
@level.length < @top || \
@level.length > @max
end
def set_anchor
@anchor = @header
.downcase
.gsub(/[^a-z\d_\- ]+/, "")
.gsub(/ /, "-")
update_if_seen
end
def update_if_seen
inc = 2
while @seen.include?(@anchor)
if inc == 2
@anchor += "-" + inc.to_s
else
@anchor.sub!(/-\d+$/, "-" + inc.to_s)
end
inc += 1
end
@seen << @anchor
end
def set_start
len = @level.length
if len == @top
@start = "#{@count}."
@count += 1
else
bullet = len % 2 == 0 ? "-" : "*"
@start = " " * (len - @top) + bullet
end
end
end
usage if ARGV.length == 0
usage if ARGV[0] == "-h"
if $0 == __FILE__
ToCWriter.new(*ARGV).write
end