1##
2# A List is a homogeneous set of ListItems.
3#
4# The supported list types include:
5#
6# :BULLET::
7#   An unordered list
8# :LABEL::
9#   An unordered definition list, but using an alternate RDoc::Markup syntax
10# :LALPHA::
11#   An ordered list using increasing lowercase English letters
12# :NOTE::
13#   An unordered definition list
14# :NUMBER::
15#   An ordered list using increasing Arabic numerals
16# :UALPHA::
17#   An ordered list using increasing uppercase English letters
18#
19# Definition lists behave like HTML definition lists.  Each list item can
20# describe multiple terms.  See RDoc::Markup::ListItem for how labels and
21# definition are stored as list items.
22
23class RDoc::Markup::List
24
25  ##
26  # The list's type
27
28  attr_accessor :type
29
30  ##
31  # Items in the list
32
33  attr_reader :items
34
35  ##
36  # Creates a new list of +type+ with +items+.  Valid list types are:
37  # +:BULLET+, +:LABEL+, +:LALPHA+, +:NOTE+, +:NUMBER+, +:UALPHA+
38
39  def initialize type = nil, *items
40    @type = type
41    @items = []
42    @items.concat items
43  end
44
45  ##
46  # Appends +item+ to the list
47
48  def << item
49    @items << item
50  end
51
52  def == other # :nodoc:
53    self.class == other.class and
54      @type == other.type and
55      @items == other.items
56  end
57
58  ##
59  # Runs this list and all its #items through +visitor+
60
61  def accept visitor
62    visitor.accept_list_start self
63
64    @items.each do |item|
65      item.accept visitor
66    end
67
68    visitor.accept_list_end self
69  end
70
71  ##
72  # Is the list empty?
73
74  def empty?
75    @items.empty?
76  end
77
78  ##
79  # Returns the last item in the list
80
81  def last
82    @items.last
83  end
84
85  def pretty_print q # :nodoc:
86    q.group 2, "[list: #{@type} ", ']' do
87      q.seplist @items do |item|
88        q.pp item
89      end
90    end
91  end
92
93  ##
94  # Appends +items+ to the list
95
96  def push *items
97    @items.concat items
98  end
99
100end
101
102