Class: ReVIEW::HTMLToc

Inherits:
Object show all
Defined in:
lib/review/htmltoc.rb

Instance Method Summary collapse

Constructor Details

#initialize(basedir) ⇒ HTMLToc

Returns a new instance of HTMLToc.



3
4
5
6
# File 'lib/review/htmltoc.rb', line 3

def initialize(basedir)
  @tochtmltxt = 'toc-html.txt'
  @basedir = basedir
end

Instance Method Details

#add_item(level, filename, title, args) ⇒ Object



8
9
10
11
12
# File 'lib/review/htmltoc.rb', line 8

def add_item(level, filename, title, args)
  args_str = encode_args(args)
  line = [level, filename, title, args_str].join("\t")
  File.open(tocfilename, 'a') { |f| f.write "#{line}\n" }
end

#decode_args(args_str) ⇒ Object



28
29
30
31
32
33
34
35
# File 'lib/review/htmltoc.rb', line 28

def decode_args(args_str)
  args = {}
  args_str.split(/,\s*/).each do |pair|
    key, val = pair.split('=')
    args[key.to_sym] = val
  end
  args
end

#each_itemObject



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

def each_item
  File.open(tocfilename) do |f|
    f.each_line do |line|
      level, file, title, args_str = line.chomp.split("\t")
      args = decode_args(args_str)
      yield level, file, title, args
    end
  end
end

#encode_args(args) ⇒ Object



37
38
39
# File 'lib/review/htmltoc.rb', line 37

def encode_args(args)
  args.delete_if { |_k, v| v.nil? }.map { |k, v| "#{k}=#{v}" }.join(',')
end

#tocfilenameObject



24
25
26
# File 'lib/review/htmltoc.rb', line 24

def tocfilename
  File.join(@basedir, @tochtmltxt)
end