1##
2# Extracts just the RDoc::Markup::Heading elements from a
3# RDoc::Markup::Document to help build a table of contents
4
5class RDoc::Markup::ToTableOfContents < RDoc::Markup::Formatter
6
7  @to_toc = nil
8
9  ##
10  # Singleton for table-of-contents generation
11
12  def self.to_toc
13    @to_toc ||= new
14  end
15
16  ##
17  # Output accumulator
18
19  attr_reader :res
20
21  ##
22  # Omits headings with a level less than the given level.
23
24  attr_accessor :omit_headings_below
25
26  def initialize # :nodoc:
27    super nil
28
29    @omit_headings_below = nil
30  end
31
32  ##
33  # Adds +document+ to the output, using its heading cutoff if present
34
35  def accept_document document
36    @omit_headings_below = document.omit_headings_below
37
38    super
39  end
40
41  ##
42  # Adds +heading+ to the table of contents
43
44  def accept_heading heading
45    @res << heading unless suppressed? heading
46  end
47
48  ##
49  # Returns the table of contents
50
51  def end_accepting
52    @res
53  end
54
55  ##
56  # Prepares the visitor for text generation
57
58  def start_accepting
59    @omit_headings_below = nil
60    @res = []
61  end
62
63  ##
64  # Returns true if +heading+ is below the display threshold
65
66  def suppressed? heading
67    return false unless @omit_headings_below
68
69    heading.level > @omit_headings_below
70  end
71
72  # :stopdoc:
73  alias accept_block_quote     ignore
74  alias accept_raw             ignore
75  alias accept_rule            ignore
76  alias accept_blank_line      ignore
77  alias accept_paragraph       ignore
78  alias accept_verbatim        ignore
79  alias accept_list_end        ignore
80  alias accept_list_item_start ignore
81  alias accept_list_item_end   ignore
82  alias accept_list_end_bullet ignore
83  alias accept_list_start      ignore
84  # :startdoc:
85
86end
87
88