1##
2# Joins the parts of an RDoc::Markup::Paragraph into a single String.
3#
4# This allows for easier maintenance and testing of Markdown support.
5#
6# This formatter only works on Paragraph instances.  Attempting to process
7# other markup syntax items will not work.
8
9class RDoc::Markup::ToJoinedParagraph < RDoc::Markup::Formatter
10
11  def initialize # :nodoc:
12    super nil
13  end
14
15  def start_accepting # :nodoc:
16  end
17
18  def end_accepting # :nodoc:
19  end
20
21  ##
22  # Converts the parts of +paragraph+ to a single entry.
23
24  def accept_paragraph paragraph
25    parts = []
26    string = false
27
28    paragraph.parts.each do |part|
29      if String === part then
30        if string then
31          string << part
32        else
33          parts << part
34          string = part
35        end
36      else
37        parts << part
38        string = false
39      end
40    end
41
42    parts = parts.map do |part|
43      if String === part then
44        part.rstrip
45      else
46        part
47      end
48    end
49
50    # TODO use Enumerable#chunk when ruby 1.8 support is dropped
51    #parts = paragraph.parts.chunk do |part|
52    #  String === part
53    #end.map do |string, chunk|
54    #  string ? chunk.join.rstrip : chunk
55    #end.flatten
56
57    paragraph.parts.replace parts
58  end
59
60  alias accept_block_quote     ignore
61  alias accept_heading         ignore
62  alias accept_list_end        ignore
63  alias accept_list_item_end   ignore
64  alias accept_list_item_start ignore
65  alias accept_list_start      ignore
66  alias accept_raw             ignore
67  alias accept_rule            ignore
68  alias accept_verbatim        ignore
69
70end
71
72