| Class | RI::AttributeFormatter |
| In: |
ri/ri_formatter.rb
|
| Parent: | TextFormatter |
Handle text with attributes. We‘re a base class: there are different presentation classes (one, for example, uses overstrikes to handle bold and underlining, while another using ANSI escape sequences
| BOLD | = | 1 |
| ITALIC | = | 2 |
| CODE | = | 4 |
| ATTR_MAP | = | { "b" => BOLD, "code" => CODE, "em" => ITALIC, "i" => ITALIC, "tt" => CODE |
# File ri/ri_formatter.rb, line 347
347: def add_attributes_to(txt)
348: tokens = txt.split(%r{(</?(?:b|code|em|i|tt)>)})
349: text = AttributeString.new
350: attributes = 0
351: tokens.each do |tok|
352: case tok
353: when %r{^</(\w+)>$} then attributes &= ~(ATTR_MAP[$1]||0)
354: when %r{^<(\w+)>$} then attributes |= (ATTR_MAP[$1]||0)
355: else
356: tok.split(//).each {|ch| text << AttrChar.new(ch, attributes)}
357: end
358: end
359: text
360: end
again, overridden
# File ri/ri_formatter.rb, line 341
341: def bold_print(txt)
342: print txt
343: end
overrides base class. Looks for … etc sequences and generates an array of AttrChars. This array is then used as the basis for the split
# File ri/ri_formatter.rb, line 305
305: def wrap(txt, prefix=@indent, linelen=@width)
306: return unless txt && !txt.empty?
307:
308: txt = add_attributes_to(txt)
309: next_prefix = prefix.tr("^ ", " ")
310: linelen -= prefix.size
311:
312: line = []
313:
314: until txt.empty?
315: word = txt.next_word
316: if word.size + line.size > linelen
317: write_attribute_text(prefix, line)
318: prefix = next_prefix
319: line = []
320: end
321: line.concat(word)
322: end
323:
324: write_attribute_text(prefix, line) if line.length > 0
325: end