Class: RDocF95::RI::Formatter

Inherits:
Object
  • Object
show all
Defined in:
lib/rdoc-f95/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
# File 'lib/rdoc-f95/ri/formatter.rb', line 19

def initialize(output, width, indent)
  @output = output
  @width  = width
  @indent = indent
end

Instance Attribute Details

#indentObject (readonly)

Returns the value of attribute indent


6
7
8
# File 'lib/rdoc-f95/ri/formatter.rb', line 6

def indent
  @indent
end

#outputObject

Returns the value of attribute output


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

def output
  @output
end

Class Method Details

.for(name) ⇒ Object


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

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

.listObject


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

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

Instance Method Details

#blanklineObject


67
68
69
# File 'lib/rdoc-f95/ri/formatter.rb', line 67

def blankline
  @output.puts
end

#bold_print(txt) ⇒ Object


78
79
80
# File 'lib/rdoc-f95/ri/formatter.rb', line 78

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.


75
76
# File 'lib/rdoc-f95/ri/formatter.rb', line 75

def break_to_newline
end

#conv_html(txt) ⇒ Object

Convert HTML entities back to ASCII


89
90
91
92
93
94
95
# File 'lib/rdoc-f95/ri/formatter.rb', line 89

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


100
101
102
103
104
105
106
# File 'lib/rdoc-f95/ri/formatter.rb', line 100

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


205
206
207
208
209
# File 'lib/rdoc-f95/ri/formatter.rb', line 205

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

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


153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
# File 'lib/rdoc-f95/ri/formatter.rb', line 153

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

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

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

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

  when RDocF95::Markup::Flow::RULE
    draw_line

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

#display_heading(text, level, indent) ⇒ Object


183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
# File 'lib/rdoc-f95/ri/formatter.rb', line 183

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


108
109
110
111
112
113
114
115
116
117
118
119
120
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
# File 'lib/rdoc-f95/ri/formatter.rb', line 108

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 RDocF95::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 RDocF95::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


176
177
178
179
180
181
# File 'lib/rdoc-f95/ri/formatter.rb', line 176

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


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

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


82
83
84
# File 'lib/rdoc-f95/ri/formatter.rb', line 82

def raw_print_line(txt)
  @output.puts txt
end

#strip_attributes(text) ⇒ Object


211
212
213
# File 'lib/rdoc-f95/ri/formatter.rb', line 211

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

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


45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/rdoc-f95/ri/formatter.rb', line 45

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