Class: RDoc::RI::Formatter

Inherits:
Object
  • Object
show all
Defined in:
lib/rdoc/ri/formatter.rb

Direct Known Subclasses

AttributeFormatter, SimpleFormatter

Constant Summary collapse

FORMATTERS =
{ }

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(output, width, indent) ⇒ Formatter

Returns a new instance of Formatter.



19
20
21
22
23
24
# File 'lib/rdoc/ri/formatter.rb', line 19

def initialize(output, width, indent)
  @output = output
  @width  = width
  @indent = indent
  @original_indent = indent.dup
end

Instance Attribute Details

#indentObject



46
47
48
49
50
51
52
53
54
55
56
# File 'lib/rdoc/ri/formatter.rb', line 46

def indent
  return @indent unless block_given?

  begin
    indent = @indent.dup
    @indent += @original_indent
    yield
  ensure
    @indent = indent
  end
end

#outputObject

Returns the value of attribute output.



7
8
9
# File 'lib/rdoc/ri/formatter.rb', line 7

def output
  @output
end

Class Method Details

.for(name) ⇒ Object



11
12
13
# File 'lib/rdoc/ri/formatter.rb', line 11

def self.for(name)
  FORMATTERS[name.downcase]
end

.listObject



15
16
17
# File 'lib/rdoc/ri/formatter.rb', line 15

def self.list
  FORMATTERS.keys.sort.join ", "
end

Instance Method Details

#blanklineObject



80
81
82
# File 'lib/rdoc/ri/formatter.rb', line 80

def blankline
  @output.puts
end

#bold_print(txt) ⇒ Object



91
92
93
# File 'lib/rdoc/ri/formatter.rb', line 91

def bold_print(txt)
  @output.print txt
end

#break_to_newlineObject

Called when we want to ensure a new ‘wrap’ starts on a newline. Only needed for HtmlFormatter, because the rest do their own line breaking.



88
89
# File 'lib/rdoc/ri/formatter.rb', line 88

def break_to_newline
end

#conv_html(txt) ⇒ Object

Convert HTML entities back to ASCII



102
103
104
105
106
107
108
# File 'lib/rdoc/ri/formatter.rb', line 102

def conv_html(txt)
  txt = txt.gsub(/>/, '>')
  txt.gsub!(/&lt;/, '<')
  txt.gsub!(/&quot;/, '"')
  txt.gsub!(/&amp;/, '&')
  txt
end

#conv_markup(txt) ⇒ Object

Convert markup into display form



113
114
115
116
117
118
119
# File 'lib/rdoc/ri/formatter.rb', line 113

def conv_markup(txt)
  txt = txt.gsub(%r{<tt>(.*?)</tt>}, '+\1+')
  txt.gsub!(%r{<code>(.*?)</code>}, '+\1+')
  txt.gsub!(%r{<b>(.*?)</b>}, '*\1*')
  txt.gsub!(%r{<em>(.*?)</em>}, '_\1_') 
  txt
end

#display_flow(flow) ⇒ Object



218
219
220
221
222
# File 'lib/rdoc/ri/formatter.rb', line 218

def display_flow(flow)
  flow.each do |f|
    display_flow_item(f)
  end
end

#display_flow_item(item, prefix = @indent) ⇒ Object



166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
# File 'lib/rdoc/ri/formatter.rb', line 166

def display_flow_item(item, prefix = @indent)
  case item
  when RDoc::Markup::Flow::P, RDoc::Markup::Flow::LI
    wrap(conv_html(item.body), prefix)
    blankline

  when RDoc::Markup::Flow::LIST
    display_list(item)

  when RDoc::Markup::Flow::VERB
    display_verbatim_flow_item(item, @indent)

  when RDoc::Markup::Flow::H
    display_heading(conv_html(item.text), item.level, @indent)

  when RDoc::Markup::Flow::RULE
    draw_line

  else
    raise RDoc::Error, "Unknown flow element: #{item.class}"
  end
end

#display_heading(text, level, indent) ⇒ Object



196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
# File 'lib/rdoc/ri/formatter.rb', line 196

def display_heading(text, level, indent)
  text = strip_attributes text

  case level
  when 1 then
    ul = "=" * text.length
    @output.puts
    @output.puts text.upcase
    @output.puts ul

  when 2 then
    ul = "-" * text.length
    @output.puts
    @output.puts text
    @output.puts ul
  else
    @output.print indent, text, "\n"
  end

  @output.puts
end

#display_list(list) ⇒ Object



121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
# File 'lib/rdoc/ri/formatter.rb', line 121

def display_list(list)
  case list.type
  when :BULLET
    prefixer = proc { |ignored| @indent + "*   " }

  when :NUMBER, :UPPERALPHA, :LOWERALPHA then
    start = case list.type
            when :NUMBER     then 1
            when :UPPERALPHA then 'A'
            when :LOWERALPHA then 'a'
            end

    prefixer = proc do |ignored|
      res = @indent + "#{start}.".ljust(4)
      start = start.succ
      res
    end

  when :LABELED, :NOTE then
    longest = 0

    list.contents.each do |item|
      if RDoc::Markup::Flow::LI === item and item.label.length > longest then
        longest = item.label.length
      end
    end

    longest += 1

    prefixer = proc { |li| @indent + li.label.ljust(longest) }

  else
    raise ArgumentError, "unknown list type #{list.type}"
  end

  list.contents.each do |item|
    if RDoc::Markup::Flow::LI === item then
      prefix = prefixer.call item
      display_flow_item item, prefix
    else
      display_flow_item item
    end
  end
end

#display_verbatim_flow_item(item, prefix = @indent) ⇒ Object



189
190
191
192
193
194
# File 'lib/rdoc/ri/formatter.rb', line 189

def display_verbatim_flow_item(item, prefix=@indent)
  item.body.split(/\n/).each do |line|
    @output.print @indent, conv_html(line), "\n"
  end
  blankline
end

#draw_line(label = nil) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/rdoc/ri/formatter.rb', line 26

def draw_line(label=nil)
  len = @width
  len -= (label.size + 1) if label

  if len > 0 then
    @output.print '-' * len
    if label
      @output.print ' '
      bold_print label
    end

    @output.puts
  else
    @output.print '-' * @width
    @output.puts

    @output.puts label
  end
end

#raw_print_line(txt) ⇒ Object



95
96
97
# File 'lib/rdoc/ri/formatter.rb', line 95

def raw_print_line(txt)
  @output.puts txt
end

#strip_attributes(text) ⇒ Object



224
225
226
# File 'lib/rdoc/ri/formatter.rb', line 224

def strip_attributes(text)
  text.gsub(/(<\/?(?:b|code|em|i|tt)>)/, '')
end

#wrap(txt, prefix = @indent, linelen = @width) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/rdoc/ri/formatter.rb', line 58

def wrap(txt, prefix=@indent, linelen=@width)
  return unless txt && !txt.empty?

  work = conv_markup(txt)
  textLen = linelen - prefix.length
  patt = Regexp.new("^(.{0,#{textLen}})[ \n]")
  next_prefix = prefix.tr("^ ", " ")

  res = []

  while work.length > textLen
    if work =~ patt
      res << $1
      work.slice!(0, $&.length)
    else
      res << work.slice!(0, textLen)
    end
  end
  res << work if work.length.nonzero?
  @output.puts(prefix + res.join("\n" + next_prefix))
end