Class: TOCBot

Inherits:
Object
  • Object
show all
Defined in:
lib/TOCBot.rb,
lib/TOCBot/version.rb

Overview

To follow

Constant Summary collapse

VERSION =
'0.2.0'.freeze

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ TOCBot

Returns a new instance of TOCBot.



9
10
11
12
13
14
15
16
17
# File 'lib/TOCBot.rb', line 9

def initialize(options = {})
    @separator = '<!--TOCBot-->'
    @level = 1
    @depth = 5

    @separator = options[:separator] unless options[:separator].nil?
    @level = options[:level].to_i unless options[:level].nil?
    @depth = options[:depth].to_i unless options[:depth].nil?
end

Instance Method Details

#load_file(filename) ⇒ Object



19
20
21
22
23
24
25
26
27
28
# File 'lib/TOCBot.rb', line 19

def load_file(filename)
    lines = []

    begin
        lines = File.readlines(filename).each(&:chomp!)
    rescue SystemCallError
        raise StandardError.new("Failed to open file #{filename} for reading")
    end
    return lines
end

#process_file(filename) ⇒ Object



112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/TOCBot.rb', line 112

def process_file(filename)
    spinners = TTY::Spinner::Multi.new("[:spinner] TOCBot is generating your Table of Contents (TOC) (Start Level: #{@level}, Depth: #{@depth})")

    sp1 = spinners.register '[:spinner] Loading File'
    sp2 = spinners.register '[:spinner] Processing File'
    sp3 = spinners.register '[:spinner] Writing File'

    sp1.auto_spin
    sp2.auto_spin
    sp3.auto_spin

    lines = load_file(filename)
    sp1.success

    processed, toc = process_lines(lines)
    sp2.success

    write_file(filename, processed, toc)
    sp3.success
end

#process_lines(lines) ⇒ Object



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
# File 'lib/TOCBot.rb', line 52

def process_lines(lines)
    processed = []
    toc = []

    skip_code_block = false
    strip_existing_toc = false
    lines.each do |line|
        line = line.strip

        #
        # Identify code blocks and ignore the contents
        #
        skip_code_block = !skip_code_block if line.start_with?('```')

        if skip_code_block
            processed << line
            next
        end

        #
        # Identify open/closing <!--TOC--> tags
        #
        strip_existing_toc = !strip_existing_toc if line == @separator

        #
        # Skip over effectively removing the existing TOC
        #
        next if strip_existing_toc && line.downcase != @separator.downcase

        #
        # Strip out relative links
        #
        next if line.start_with?('<a name="')

        if line.scan(/^\#{1,}.*/m).size.positive?

            m = line.match(/^(\#{1,})(.*)/)
            hashes = m[1].length
            text = m[2].strip

            if hashes >= @level
                hashes = hashes - @level + 1

                if hashes <= @depth
                    link = text.gsub(/ /, '-').downcase

                    toc << '     ' * (hashes - 1) + "* [#{text}](##{link})"
                    processed << "<a name=\"#{link}\"></a>"
                end
            end
        end

        #
        # Add the line to array - but also process it to see if it needs to go into the TOC
        #
        processed << line
    end
    return processed, toc
end

#write_file(filename, processed, toc_list, permissions = 0o0644) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/TOCBot.rb', line 30

def write_file(filename, processed, toc_list, permissions = 0o0644)
    toc_done = false

    begin
        File.open(filename, 'w') do |f|
            processed.each do |line|
                f.puts line

                next unless line == @separator && !toc_done

                toc_list.each do |toc|
                    f.puts toc
                end
                toc_done = true
            end
            f.chmod(permissions)
        end
    rescue SystemCallError
        raise StandardError.new("Failed to open file #{filename} for writing")
    end
end