Class: FoxTextFormatter
- Inherits:
-
Object
- Object
- FoxTextFormatter
- Defined in:
- lib/FoxTextFormatter.rb
Overview
This class is mostly copy & paste from ri_formatter.rb. except that it always returns a string and does not print anything.
Constant Summary collapse
- STYLE_NORMAL =
define all possible styles
:STYLE_NORMAL
- STYLE_BOLD =
:STYLE_BOLD
- STYLE_CLASS =
:STYLE_CLASS
- STYLE_H1 =
:STYLE_H1
- STYLE_H2 =
:STYLE_H2
- STYLE_H3 =
:STYLE_H3
- STYLE_TELETYPE =
:STYLE_TELETYPE
- STYLE_CODE =
:STYLE_CODE
- STYLE_EMPHASIS =
:STYLE_EMPHASIS
Instance Attribute Summary collapse
-
#indent ⇒ Object
readonly
Returns the value of attribute indent.
-
#width ⇒ Object
Returns the value of attribute width.
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, prefix, linelen) ⇒ 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_params(method) ⇒ Object
- #display_verbatim_flow_item(item, prefix = @indent) ⇒ Object
- #draw_line(label = nil) ⇒ Object
-
#initialize(width, indent, &proc) ⇒ FoxTextFormatter
constructor
whenever text should be printed/added/shown, proc is called with the text as the argument.
- #raw_print_line(txt) ⇒ Object
- #wrap(txt, prefix = @indent, linelen = @width) ⇒ Object
Constructor Details
#initialize(width, indent, &proc) ⇒ FoxTextFormatter
whenever text should be printed/added/shown, proc is called with the text as the argument.
19 20 21 22 23 |
# File 'lib/FoxTextFormatter.rb', line 19 def initialize(width, indent, &proc) @width = width @indent = indent @proc = proc end |
Instance Attribute Details
#indent ⇒ Object (readonly)
Returns the value of attribute indent.
15 16 17 |
# File 'lib/FoxTextFormatter.rb', line 15 def indent @indent end |
#width ⇒ Object
Returns the value of attribute width.
16 17 18 |
# File 'lib/FoxTextFormatter.rb', line 16 def width @width end |
Instance Method Details
#blankline ⇒ Object
80 81 82 |
# File 'lib/FoxTextFormatter.rb', line 80 def blankline @proc.call("\n") end |
#bold_print(txt) ⇒ Object
95 96 97 |
# File 'lib/FoxTextFormatter.rb', line 95 def bold_print(txt) @proc.call(txt, STYLE_BOLD) 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
90 91 |
# File 'lib/FoxTextFormatter.rb', line 90 def break_to_newline end |
#conv_html(txt) ⇒ Object
convert HTML entities back to ASCII
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 |
# File 'lib/FoxTextFormatter.rb', line 108 def conv_html(txt) case txt when Array txt.join. gsub(/>/, '>'). gsub(/</, '<'). gsub(/"/, '"'). gsub(/&/, '&') else # it's a String txt. gsub(/>/, '>'). gsub(/</, '<'). gsub(/"/, '"'). gsub(/&/, '&') end end |
#conv_markup(txt, prefix, linelen) ⇒ Object
convert markup into display form
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 |
# File 'lib/FoxTextFormatter.rb', line 126 def conv_markup(txt, prefix, linelen) # this code assumes that tags are not used inside tags pos = 0 old_pos = 0 style = STYLE_NORMAL current_indent = prefix.size while pos = txt.index(%r{(<tt>|<code>|<b>|<em>|</tt>|</code>|</b>|</em>)}, old_pos) new_part = txt[old_pos...pos] @proc.call(new_part, style) # get tag name old_pos = txt.index(">", pos) + 1 style = case txt[(pos+1)...(old_pos-1)] when "tt" STYLE_TELETYPE when "code" STYLE_CODE when "b" STYLE_BOLD when "em" STYLE_EMPHASIS else # closing or unknown tags STYLE_NORMAL end end @proc.call(txt[old_pos...txt.size], style) @proc.call("\n") end |
#display_flow(flow) ⇒ Object
270 271 272 273 274 |
# File 'lib/FoxTextFormatter.rb', line 270 def display_flow(flow) flow.each do |f| display_flow_item(f) end end |
#display_flow_item(item, prefix = @indent) ⇒ Object
212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 |
# File 'lib/FoxTextFormatter.rb', line 212 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 when String wrap(conv_html(item), prefix) else fail "Unknown flow element: #{item.class}" end end |
#display_heading(text, level, indent) ⇒ Object
252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 |
# File 'lib/FoxTextFormatter.rb', line 252 def display_heading(text, level, indent) case level when 1 @proc.call(text, STYLE_H1) @proc.call("\n") when 2 @proc.call(text, STYLE_H2) @proc.call("\n") else @proc.call(indent) @proc.call(text, STYLE_H3) @proc.call("\n") end end |
#display_list(list) ⇒ Object
159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 |
# File 'lib/FoxTextFormatter.rb', line 159 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_params(method) ⇒ Object
39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/FoxTextFormatter.rb', line 39 def display_params(method) params = method.params if params[0] == ?( if method.is_singleton params = method.full_name + params else params = method.name + params end end params.split(/\n/).each do |param| @proc.call(param+"\n", STYLE_BOLD) end end |
#display_verbatim_flow_item(item, prefix = @indent) ⇒ Object
241 242 243 244 245 246 247 248 |
# File 'lib/FoxTextFormatter.rb', line 241 def display_verbatim_flow_item(item, prefix=@indent) item.body.split(/\n/).each do |line| @proc.call(@indent) @proc.call(conv_html(line)) @proc.call("\n") end blankline end |
#draw_line(label = nil) ⇒ Object
27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/FoxTextFormatter.rb', line 27 def draw_line(label=nil) len = @width len -= (label.size+1) if label len = [0, len].max @proc.call("-"*len) if label @proc.call(" ") @proc.call(label, STYLE_CLASS) end @proc.call("\n") end |
#raw_print_line(txt) ⇒ Object
101 102 103 |
# File 'lib/FoxTextFormatter.rb', line 101 def raw_print_line(txt) @proc.call(txt) end |
#wrap(txt, prefix = @indent, linelen = @width) ⇒ Object
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/FoxTextFormatter.rb', line 54 def wrap(txt, prefix=@indent, linelen=@width) return if !txt || txt.empty? @proc.call(prefix, STYLE_EMPHASIS) conv_markup(txt, prefix, linelen) =begin 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? @proc.call(prefix + res.join("\n" + next_prefix) + "\n") =end end |