1##
2# An item within a List that contains paragraphs, headings, etc.
3#
4# For BULLET, NUMBER, LALPHA and UALPHA lists, the label will always be nil.
5# For NOTE and LABEL lists, the list label may contain:
6#
7# * a single String for a single label
8# * an Array of Strings for a list item with multiple terms
9# * nil for an extra description attached to a previously labeled list item
10
11class RDoc::Markup::ListItem
12
13  ##
14  # The label for the ListItem
15
16  attr_accessor :label
17
18  ##
19  # Parts of the ListItem
20
21  attr_reader :parts
22
23  ##
24  # Creates a new ListItem with an optional +label+ containing +parts+
25
26  def initialize label = nil, *parts
27    @label = label
28    @parts = []
29    @parts.concat parts
30  end
31
32  ##
33  # Appends +part+ to the ListItem
34
35  def << part
36    @parts << part
37  end
38
39  def == other # :nodoc:
40    self.class == other.class and
41      @label == other.label and
42      @parts == other.parts
43  end
44
45  ##
46  # Runs this list item and all its #parts through +visitor+
47
48  def accept visitor
49    visitor.accept_list_item_start self
50
51    @parts.each do |part|
52      part.accept visitor
53    end
54
55    visitor.accept_list_item_end self
56  end
57
58  ##
59  # Is the ListItem empty?
60
61  def empty?
62    @parts.empty?
63  end
64
65  ##
66  # Length of parts in the ListItem
67
68  def length
69    @parts.length
70  end
71
72  def pretty_print q # :nodoc:
73    q.group 2, '[item: ', ']' do
74      case @label
75      when Array then
76        q.pp @label
77        q.text ';'
78        q.breakable
79      when String then
80        q.pp @label
81        q.text ';'
82        q.breakable
83      end
84
85      q.seplist @parts do |part|
86        q.pp part
87      end
88    end
89  end
90
91  ##
92  # Adds +parts+ to the ListItem
93
94  def push *parts
95    @parts.concat parts
96  end
97
98end
99
100