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.



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

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

Instance Method Details

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



9
10
11
12
13
14
15
# File 'lib/review/htmltoc.rb', line 9

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

#decode_args(args_str) ⇒ Object



31
32
33
34
35
36
37
38
# File 'lib/review/htmltoc.rb', line 31

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

#each_itemObject



17
18
19
20
21
22
23
24
25
# File 'lib/review/htmltoc.rb', line 17

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



40
41
42
# File 'lib/review/htmltoc.rb', line 40

def encode_args(args)
  args.map{|k,v| "#{k}=#{v}"}.join(",")
end

#tocfilenameObject



27
28
29
# File 'lib/review/htmltoc.rb', line 27

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