Class: TOCBot

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

Overview

To follow

Constant Summary collapse

VERSION =
'0.1.0'.freeze

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ TOCBot

Returns a new instance of TOCBot.



7
8
9
10
11
# File 'lib/TOCBot.rb', line 7

def initialize(options = {})
    @separator = '<!--TOC-->'

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

Instance Method Details

#load_file(filename) ⇒ Object



13
14
15
16
17
18
19
20
21
22
# File 'lib/TOCBot.rb', line 13

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



99
100
101
102
103
104
105
# File 'lib/TOCBot.rb', line 99

def process_file(filename)
    lines = load_file(filename)

    processed, toc = process_lines(lines)

    write_file(filename, processed, toc)
end

#process_lines(lines) ⇒ Object



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

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

            link = text.gsub(/ /, '-').downcase

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



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/TOCBot.rb', line 24

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