Class: RI::TextFormatter
- Inherits:
-
Object
- Object
- RI::TextFormatter
- Defined in:
- lib/rdoc/ri/ri_formatter.rb,
lib/rdoc/ri/ri_formatter.rb
Overview
Finally, fill in the list of known formatters
Direct Known Subclasses
Constant Summary collapse
- FORMATTERS =
{ "ansi" => AnsiFormatter, "bs" => OverstrikeFormatter, "html" => HtmlFormatter, "plain" => TextFormatter, "simple" => SimpleFormatter, }
Instance Attribute Summary collapse
-
#indent ⇒ Object
readonly
Returns the value of attribute indent.
Class Method Summary collapse
Instance Method Summary collapse
-
#blankline ⇒ Object
####################################################################.
-
#bold_print(txt) ⇒ Object
####################################################################.
-
#break_to_newline ⇒ Object
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.
-
#conv_html(txt) ⇒ Object
convert HTML entities back to ASCII.
-
#conv_markup(txt) ⇒ Object
convert markup into display form.
- #display_flow(flow) ⇒ Object
-
#display_flow_item(item, prefix = @indent) ⇒ Object
####################################################################.
-
#display_heading(text, level, indent) ⇒ Object
####################################################################.
-
#display_list(list) ⇒ Object
####################################################################.
-
#display_verbatim_flow_item(item, prefix = @indent) ⇒ Object
####################################################################.
-
#draw_line(label = nil) ⇒ Object
####################################################################.
-
#initialize(options, indent) ⇒ TextFormatter
constructor
A new instance of TextFormatter.
-
#raw_print_line(txt) ⇒ Object
####################################################################.
- #strip_attributes(txt) ⇒ Object
-
#wrap(txt, prefix = @indent, linelen = @width) ⇒ Object
####################################################################.
Constructor Details
#initialize(options, indent) ⇒ TextFormatter
Returns a new instance of TextFormatter.
6 7 8 9 10 |
# File 'lib/rdoc/ri/ri_formatter.rb', line 6 def initialize(, indent) @options = @width = .width @indent = indent end |
Instance Attribute Details
#indent ⇒ Object (readonly)
Returns the value of attribute indent
4 5 6 |
# File 'lib/rdoc/ri/ri_formatter.rb', line 4 def indent @indent end |
Class Method Details
.for(name) ⇒ Object
664 665 666 |
# File 'lib/rdoc/ri/ri_formatter.rb', line 664 def TextFormatter.for(name) FORMATTERS[name.downcase] end |
.list ⇒ Object
660 661 662 |
# File 'lib/rdoc/ri/ri_formatter.rb', line 660 def TextFormatter.list FORMATTERS.keys.sort.join(", ") end |
Instance Method Details
#blankline ⇒ Object
####################################################################
51 52 53 |
# File 'lib/rdoc/ri/ri_formatter.rb', line 51 def blankline puts end |
#bold_print(txt) ⇒ Object
####################################################################
66 67 68 |
# File 'lib/rdoc/ri/ri_formatter.rb', line 66 def bold_print(txt) print txt end |
#break_to_newline ⇒ Object
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
61 62 |
# File 'lib/rdoc/ri/ri_formatter.rb', line 61 def break_to_newline end |
#conv_html(txt) ⇒ Object
convert HTML entities back to ASCII
79 80 81 82 83 84 85 86 |
# File 'lib/rdoc/ri/ri_formatter.rb', line 79 def conv_html(txt) txt. gsub(/>/, '>'). gsub(/</, '<'). gsub(/"/, '"'). gsub(/&/, '&') end |
#conv_markup(txt) ⇒ Object
convert markup into display form
89 90 91 92 93 94 95 |
# File 'lib/rdoc/ri/ri_formatter.rb', line 89 def conv_markup(txt) txt. gsub(%r{<tt>(.*?)</tt>}) { "+#$1+" } . gsub(%r{<code>(.*?)</code>}) { "+#$1+" } . gsub(%r{<b>(.*?)</b>}) { "*#$1*" } . gsub(%r{<em>(.*?)</em>}) { "_#$1_" } end |
#display_flow(flow) ⇒ Object
210 211 212 213 214 |
# File 'lib/rdoc/ri/ri_formatter.rb', line 210 def display_flow(flow) flow.each do |f| display_flow_item(f) end end |
#display_flow_item(item, prefix = @indent) ⇒ Object
####################################################################
154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 |
# File 'lib/rdoc/ri/ri_formatter.rb', line 154 def display_flow_item(item, prefix=@indent) case item when SM::Flow::P, SM::Flow::LI wrap(conv_html(item.body), prefix) blankline when SM::Flow::LIST display_list(item) when SM::Flow::VERB display_verbatim_flow_item(item, @indent) when SM::Flow::H display_heading(conv_html(item.text), item.level, @indent) when SM::Flow::RULE draw_line else fail "Unknown flow element: #{item.class}" end end |
#display_heading(text, level, indent) ⇒ Object
####################################################################
188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 |
# File 'lib/rdoc/ri/ri_formatter.rb', line 188 def display_heading(text, level, indent) text = strip_attributes(text) case level when 1 ul = "=" * text.length puts puts text.upcase puts ul # puts when 2 ul = "-" * text.length puts puts text puts ul # puts else print indent, text, "\n" end end |
#display_list(list) ⇒ Object
####################################################################
99 100 101 102 103 104 105 106 107 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 |
# File 'lib/rdoc/ri/ri_formatter.rb', line 99 def display_list(list) case list.type when SM::ListBase::BULLET prefixer = proc { |ignored| @indent + "* " } when SM::ListBase::NUMBER, SM::ListBase::UPPERALPHA, SM::ListBase::LOWERALPHA start = case list.type when SM::ListBase::NUMBER then 1 when SM::ListBase::UPPERALPHA then 'A' when SM::ListBase::LOWERALPHA then 'a' end prefixer = proc do |ignored| res = @indent + "#{start}.".ljust(4) start = start.succ res end when SM::ListBase::LABELED prefixer = proc do |li| li.label end when SM::ListBase::NOTE longest = 0 list.contents.each do |item| if item.kind_of?(SM::Flow::LI) && item.label.length > longest longest = item.label.length end end prefixer = proc do |li| @indent + li.label.ljust(longest+1) end else fail "unknown list type" end list.contents.each do |item| if item.kind_of? SM::Flow::LI 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
####################################################################
179 180 181 182 183 184 |
# File 'lib/rdoc/ri/ri_formatter.rb', line 179 def display_verbatim_flow_item(item, prefix=@indent) item.body.split(/\n/).each do |line| print @indent, conv_html(line), "\n" end blankline end |
#draw_line(label = nil) ⇒ Object
####################################################################
15 16 17 18 19 20 21 22 23 24 |
# File 'lib/rdoc/ri/ri_formatter.rb', line 15 def draw_line(label=nil) len = @width len -= (label.size+1) if label print "-"*len if label print(" ") bold_print(label) end puts end |
#raw_print_line(txt) ⇒ Object
####################################################################
72 73 74 |
# File 'lib/rdoc/ri/ri_formatter.rb', line 72 def raw_print_line(txt) puts txt end |
#strip_attributes(txt) ⇒ Object
216 217 218 219 220 221 222 223 224 225 226 227 228 229 |
# File 'lib/rdoc/ri/ri_formatter.rb', line 216 def strip_attributes(txt) tokens = txt.split(%r{(</?(?:b|code|em|i|tt)>)}) text = [] attributes = 0 tokens.each do |tok| case tok when %r{^</(\w+)>$}, %r{^<(\w+)>$} ; else text << tok end end text.join end |
#wrap(txt, prefix = @indent, linelen = @width) ⇒ Object
####################################################################
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/rdoc/ri/ri_formatter.rb', line 28 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? puts(prefix + res.join("\n" + next_prefix)) end |