Class | RI::TextFormatter |
In: |
ri/ri_formatter.rb
|
Parent: | Object |
Finally, fill in the list of known formatters
FORMATTERS | = | { "ansi" => AnsiFormatter, "bs" => OverstrikeFormatter, "html" => HtmlFormatter, "plain" => TextFormatter, "simple" => SimpleFormatter, } |
indent | [R] |
# File ri/ri_formatter.rb, line 666 666: def TextFormatter.for(name) 667: FORMATTERS[name.downcase] 668: end
# File ri/ri_formatter.rb, line 662 662: def TextFormatter.list 663: FORMATTERS.keys.sort.join(", ") 664: end
# File ri/ri_formatter.rb, line 6 6: def initialize(options, indent) 7: @options = options 8: @width = options.width 9: @indent = indent 10: end
called when we want to ensure a nbew ‘wrap’ starts on a newline Only needed for HtmlFormatter, because the rest do their own line breaking
# File ri/ri_formatter.rb, line 61 61: def break_to_newline 62: end
convert HTML entities back to ASCII
# File ri/ri_formatter.rb, line 79 79: def conv_html(txt) 80: txt. 81: gsub(/>/, '>'). 82: gsub(/</, '<'). 83: gsub(/"/, '"'). 84: gsub(/&/, '&') 85: 86: end
convert markup into display form
# File ri/ri_formatter.rb, line 89 89: def conv_markup(txt) 90: txt. 91: gsub(%r{<tt>(.*?)</tt>}) { "+#$1+" } . 92: gsub(%r{<code>(.*?)</code>}) { "+#$1+" } . 93: gsub(%r{<b>(.*?)</b>}) { "*#$1*" } . 94: gsub(%r{<em>(.*?)</em>}) { "_#$1_" } 95: end
# File ri/ri_formatter.rb, line 210 210: def display_flow(flow) 211: flow.each do |f| 212: display_flow_item(f) 213: end 214: end
# File ri/ri_formatter.rb, line 154 154: def display_flow_item(item, prefix=@indent) 155: case item 156: when SM::Flow::P, SM::Flow::LI 157: wrap(conv_html(item.body), prefix) 158: blankline 159: 160: when SM::Flow::LIST 161: display_list(item) 162: 163: when SM::Flow::VERB 164: display_verbatim_flow_item(item, @indent) 165: 166: when SM::Flow::H 167: display_heading(conv_html(item.text), item.level, @indent) 168: 169: when SM::Flow::RULE 170: draw_line 171: 172: else 173: fail "Unknown flow element: #{item.class}" 174: end 175: end
# File ri/ri_formatter.rb, line 188 188: def display_heading(text, level, indent) 189: text = strip_attributes(text) 190: case level 191: when 1 192: ul = "=" * text.length 193: puts 194: puts text.upcase 195: puts ul 196: # puts 197: 198: when 2 199: ul = "-" * text.length 200: puts 201: puts text 202: puts ul 203: # puts 204: else 205: print indent, text, "\n" 206: end 207: end
# File ri/ri_formatter.rb, line 99 99: def display_list(list) 100: case list.type 101: 102: when SM::ListBase::BULLET 103: prefixer = proc { |ignored| @indent + "* " } 104: 105: when SM::ListBase::NUMBER, 106: SM::ListBase::UPPERALPHA, 107: SM::ListBase::LOWERALPHA 108: 109: start = case list.type 110: when SM::ListBase::NUMBER then 1 111: when SM::ListBase::UPPERALPHA then 'A' 112: when SM::ListBase::LOWERALPHA then 'a' 113: end 114: prefixer = proc do |ignored| 115: res = @indent + "#{start}.".ljust(4) 116: start = start.succ 117: res 118: end 119: 120: when SM::ListBase::LABELED 121: prefixer = proc do |li| 122: li.label 123: end 124: 125: when SM::ListBase::NOTE 126: longest = 0 127: list.contents.each do |item| 128: if item.kind_of?(SM::Flow::LI) && item.label.length > longest 129: longest = item.label.length 130: end 131: end 132: 133: prefixer = proc do |li| 134: @indent + li.label.ljust(longest+1) 135: end 136: 137: else 138: fail "unknown list type" 139: 140: end 141: 142: list.contents.each do |item| 143: if item.kind_of? SM::Flow::LI 144: prefix = prefixer.call(item) 145: display_flow_item(item, prefix) 146: else 147: display_flow_item(item) 148: end 149: end 150: end
# File ri/ri_formatter.rb, line 179 179: def display_verbatim_flow_item(item, prefix=@indent) 180: item.body.split(/\n/).each do |line| 181: print @indent, conv_html(line), "\n" 182: end 183: blankline 184: end
# File ri/ri_formatter.rb, line 15 15: def draw_line(label=nil) 16: len = @width 17: len -= (label.size+1) if label 18: print "-"*len 19: if label 20: print(" ") 21: bold_print(label) 22: end 23: puts 24: end
# File ri/ri_formatter.rb, line 216 216: def strip_attributes(txt) 217: tokens = txt.split(%r{(</?(?:b|code|em|i|tt)>)}) 218: text = [] 219: attributes = 0 220: tokens.each do |tok| 221: case tok 222: when %r{^</(\w+)>$}, %r{^<(\w+)>$} 223: ; 224: else 225: text << tok 226: end 227: end 228: text.join 229: end
# File ri/ri_formatter.rb, line 28 28: def wrap(txt, prefix=@indent, linelen=@width) 29: return unless txt && !txt.empty? 30: work = conv_markup(txt) 31: textLen = linelen - prefix.length 32: patt = Regexp.new("^(.{0,#{textLen}})[ \n]") 33: next_prefix = prefix.tr("^ ", " ") 34: 35: res = [] 36: 37: while work.length > textLen 38: if work =~ patt 39: res << $1 40: work.slice!(0, $&.length) 41: else 42: res << work.slice!(0, textLen) 43: end 44: end 45: res << work if work.length.nonzero? 46: puts(prefix + res.join("\n" + next_prefix)) 47: end