~ubuntu-branches/ubuntu/quantal/ruby1.9.1/quantal

« back to all changes in this revision

Viewing changes to lib/rdoc/markup/to_rdoc.rb

  • Committer: Bazaar Package Importer
  • Author(s): Lucas Nussbaum
  • Date: 2011-09-24 19:16:17 UTC
  • mfrom: (1.1.8 upstream) (13.1.7 experimental)
  • Revision ID: james.westby@ubuntu.com-20110924191617-o1qz4rcmqjot8zuy
Tags: 1.9.3~rc1-1
* New upstream release: 1.9.3 RC1.
  + Includes load.c fixes. Closes: #639959.
* Upload to unstable.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
require 'rdoc/markup/formatter'
1
2
require 'rdoc/markup/inline'
2
3
 
3
4
##
5
6
 
6
7
class RDoc::Markup::ToRdoc < RDoc::Markup::Formatter
7
8
 
 
9
  ##
 
10
  # Current indent amount for output in characters
 
11
 
8
12
  attr_accessor :indent
 
13
 
 
14
  ##
 
15
  # Output width in characters
 
16
 
 
17
  attr_accessor :width
 
18
 
 
19
  ##
 
20
  # Stack of current list indexes for alphabetic and numeric lists
 
21
 
9
22
  attr_reader :list_index
 
23
 
 
24
  ##
 
25
  # Stack of list types
 
26
 
10
27
  attr_reader :list_type
 
28
 
 
29
  ##
 
30
  # Stack of list widths for indentation
 
31
 
11
32
  attr_reader :list_width
 
33
 
 
34
  ##
 
35
  # Prefix for the next list item.  See #use_prefix
 
36
 
12
37
  attr_reader :prefix
 
38
 
 
39
  ##
 
40
  # Output accumulator
 
41
 
13
42
  attr_reader :res
14
43
 
15
 
  def initialize
 
44
  ##
 
45
  # Creates a new formatter that will output (mostly) \RDoc markup
 
46
 
 
47
  def initialize markup = nil
16
48
    super
17
49
 
18
 
    @markup.add_special(/\\[^\s]/, :SUPPRESSED_CROSSREF)
19
 
 
 
50
    @markup.add_special(/\\\S/, :SUPPRESSED_CROSSREF)
20
51
    @width = 78
21
 
    @prefix = ''
22
 
 
23
52
    init_tags
24
53
 
25
54
    @headings = {}
34
63
  end
35
64
 
36
65
  ##
37
 
  # Maps attributes to ANSI sequences
 
66
  # Maps attributes to HTML sequences
38
67
 
39
68
  def init_tags
40
69
    add_tag :BOLD, "<b>", "</b>"
42
71
    add_tag :EM,   "<em>", "</em>"
43
72
  end
44
73
 
 
74
  ##
 
75
  # Adds +blank_line+ to the output
 
76
 
45
77
  def accept_blank_line blank_line
46
78
    @res << "\n"
47
79
  end
48
80
 
 
81
  ##
 
82
  # Adds +heading+ to the output
 
83
 
49
84
  def accept_heading heading
50
85
    use_prefix or @res << ' ' * @indent
51
86
    @res << @headings[heading.level][0]
54
89
    @res << "\n"
55
90
  end
56
91
 
 
92
  ##
 
93
  # Finishes consumption of +list+
 
94
 
57
95
  def accept_list_end list
58
96
    @list_index.pop
59
97
    @list_type.pop
60
98
    @list_width.pop
61
99
  end
62
100
 
 
101
  ##
 
102
  # Finishes consumption of +list_item+
 
103
 
63
104
  def accept_list_item_end list_item
64
105
    width = case @list_type.last
65
106
            when :BULLET then
76
117
    @indent -= width
77
118
  end
78
119
 
 
120
  ##
 
121
  # Prepares the visitor for consuming +list_item+
 
122
 
79
123
  def accept_list_item_start list_item
80
 
    bullet = case @list_type.last
81
 
             when :BULLET then
82
 
               '*'
83
 
             when :NOTE, :LABEL then
84
 
               attributes(list_item.label) + ":\n"
85
 
             else
86
 
               @list_index.last.to_s + '.'
87
 
             end
 
124
    type = @list_type.last
88
125
 
89
 
    case @list_type.last
 
126
    case type
90
127
    when :NOTE, :LABEL then
 
128
      bullet = attributes(list_item.label) + ":\n"
 
129
      @prefix = ' ' * @indent
91
130
      @indent += 2
92
 
      @prefix = bullet + (' ' * @indent)
 
131
      @prefix << bullet + (' ' * @indent)
93
132
    else
 
133
      bullet = type == :BULLET ? '*' :  @list_index.last.to_s + '.'
94
134
      @prefix = (' ' * @indent) + bullet.ljust(bullet.length + 1)
95
 
 
96
135
      width = bullet.length + 1
97
 
 
98
136
      @indent += width
99
137
    end
100
138
  end
101
139
 
 
140
  ##
 
141
  # Prepares the visitor for consuming +list+
 
142
 
102
143
  def accept_list_start list
103
144
    case list.type
104
145
    when :BULLET then
123
164
    @list_type << list.type
124
165
  end
125
166
 
 
167
  ##
 
168
  # Adds +paragraph+ to the output
 
169
 
126
170
  def accept_paragraph paragraph
127
171
    wrap attributes(paragraph.text)
128
172
  end
129
173
 
 
174
  ##
 
175
  # Adds +paragraph+ to the output
 
176
 
 
177
  def accept_indented_paragraph paragraph
 
178
    @indent += paragraph.indent
 
179
    wrap attributes(paragraph.text)
 
180
    @indent -= paragraph.indent
 
181
  end
 
182
 
 
183
  ##
 
184
  # Adds +raw+ to the output
 
185
 
130
186
  def accept_raw raw
131
187
    @res << raw.parts.join("\n")
132
188
  end
133
189
 
 
190
  ##
 
191
  # Adds +rule+ to the output
 
192
 
134
193
  def accept_rule rule
135
194
    use_prefix or @res << ' ' * @indent
136
195
    @res << '-' * (@width - @indent)
138
197
  end
139
198
 
140
199
  ##
141
 
  # Outputs +verbatim+ flush left and indented 2 columns
 
200
  # Outputs +verbatim+ indented 2 columns
142
201
 
143
202
  def accept_verbatim verbatim
144
203
    indent = ' ' * (@indent + 2)
145
204
 
146
 
    lines = []
147
 
    current_line = []
148
 
 
149
 
    # split into lines
150
205
    verbatim.parts.each do |part|
151
 
      current_line << part
152
 
 
153
 
      if part == "\n" then
154
 
        lines << current_line
155
 
        current_line = []
156
 
      end
157
 
    end
158
 
 
159
 
    lines << current_line unless current_line.empty?
160
 
 
161
 
    # calculate margin
162
 
    indented = lines.select { |line| line != ["\n"] }
163
 
    margin = indented.map { |line| line.first.length }.min
164
 
 
165
 
    # flush left
166
 
    indented.each { |line| line[0][0...margin] = '' }
167
 
 
168
 
    # output
169
 
    use_prefix or @res << indent # verbatim is unlikely to have prefix
170
 
    @res << lines.shift.join
171
 
 
172
 
    lines.each do |line|
173
 
      @res << indent unless line == ["\n"]
174
 
      @res << line.join
175
 
    end
176
 
 
177
 
    @res << "\n"
 
206
      @res << indent unless part == "\n"
 
207
      @res << part
 
208
    end
 
209
 
 
210
    @res << "\n" unless @res =~ /\n\z/
178
211
  end
179
212
 
 
213
  ##
 
214
  # Applies attribute-specific markup to +text+ using RDoc::AttributeManager
 
215
 
180
216
  def attributes text
181
217
    flow = @am.flow text.dup
182
218
    convert_flow flow
183
219
  end
184
220
 
 
221
  ##
 
222
  # Returns the generated output
 
223
 
185
224
  def end_accepting
186
225
    @res.join
187
226
  end
188
227
 
 
228
  ##
 
229
  # Removes preceding \\ from the suppressed crossref +special+
 
230
 
189
231
  def handle_special_SUPPRESSED_CROSSREF special
190
 
    special.text.sub(/\\/, '')
 
232
    text = special.text
 
233
    text = text.sub('\\', '') unless in_tt?
 
234
    text
191
235
  end
192
236
 
 
237
  ##
 
238
  # Prepares the visitor for text generation
 
239
 
193
240
  def start_accepting
194
241
    @res = [""]
195
242
    @indent = 0
200
247
    @list_width = []
201
248
  end
202
249
 
 
250
  ##
 
251
  # Adds the stored #prefix to the output and clears it.  Lists generate a
 
252
  # prefix for later consumption.
 
253
 
203
254
  def use_prefix
204
255
    prefix = @prefix
205
256
    @prefix = nil
208
259
    prefix
209
260
  end
210
261
 
 
262
  ##
 
263
  # Wraps +text+ to #width
 
264
 
211
265
  def wrap text
212
266
    return unless text && !text.empty?
213
267