fxri 0.3.2 → 0.3.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,265 +1,275 @@
1
1
  # This class is mostly copy & paste from ri_formatter.rb.
2
2
  # except that it always returns a string and does not print anything.
3
3
  class FoxTextFormatter
4
- # define all possible styles
5
- STYLE_NORMAL = :STYLE_NORMAL
6
- STYLE_BOLD = :STYLE_BOLD
7
- STYLE_CLASS = :STYLE_CLASS
8
- STYLE_H1 = :STYLE_H1
9
- STYLE_H2 = :STYLE_H2
10
- STYLE_H3 = :STYLE_H3
11
- STYLE_TELETYPE = :STYLE_TELETYPE
12
- STYLE_CODE = :STYLE_CODE
13
- STYLE_EMPHASIS = :STYLE_EMPHASIS
14
-
15
- attr_reader :indent
16
- attr_accessor :width
17
-
18
- # whenever text should be printed/added/shown, proc is called with the text as the argument.
19
- def initialize(width, indent, &proc)
20
- @width = width
21
- @indent = indent
22
- @proc = proc
23
- end
24
-
25
- ######################################################################
26
-
27
- def draw_line(label=nil)
28
- len = @width
29
- len -= (label.size+1) if label
30
- len = [0, len].max
31
- @proc.call("-"*len)
32
- if label
33
- @proc.call(" ")
34
- @proc.call(label, STYLE_CLASS)
35
- end
36
- @proc.call("\n")
37
- end
38
-
39
- def display_params(method)
40
- params = method.params
41
-
42
- if params[0] == ?(
43
- if method.is_singleton
44
- params = method.full_name + params
45
- else
46
- params = method.name + params
47
- end
48
- end
49
- params.split(/\n/).each do |param|
50
- @proc.call(param+"\n", STYLE_BOLD)
51
- end
52
- end
53
-
54
- def wrap(txt, prefix=@indent, linelen=@width)
55
- return if !txt || txt.empty?
56
- @proc.call(prefix, STYLE_EMPHASIS)
57
- conv_markup(txt, prefix, linelen)
58
- =begin
59
- textLen = linelen - prefix.length
60
- patt = Regexp.new("^(.{0,#{textLen}})[ \n]")
61
- next_prefix = prefix.tr("^ ", " ")
62
-
63
- res = []
64
-
65
- while work.length > textLen
66
- if work =~ patt
67
- res << $1
68
- work.slice!(0, $&.length)
69
- else
70
- res << work.slice!(0, textLen)
71
- end
72
- end
73
- res << work if work.length.nonzero?
74
- @proc.call(prefix + res.join("\n" + next_prefix) + "\n")
4
+ # define all possible styles
5
+ STYLE_NORMAL = :STYLE_NORMAL
6
+ STYLE_BOLD = :STYLE_BOLD
7
+ STYLE_CLASS = :STYLE_CLASS
8
+ STYLE_H1 = :STYLE_H1
9
+ STYLE_H2 = :STYLE_H2
10
+ STYLE_H3 = :STYLE_H3
11
+ STYLE_TELETYPE = :STYLE_TELETYPE
12
+ STYLE_CODE = :STYLE_CODE
13
+ STYLE_EMPHASIS = :STYLE_EMPHASIS
14
+
15
+ attr_reader :indent
16
+ attr_accessor :width
17
+
18
+ # whenever text should be printed/added/shown, proc is called with the text as the argument.
19
+ def initialize(width, indent, &proc)
20
+ @width = width
21
+ @indent = indent
22
+ @proc = proc
23
+ end
24
+
25
+ ######################################################################
26
+
27
+ def draw_line(label=nil)
28
+ len = @width
29
+ len -= (label.size+1) if label
30
+ len = [0, len].max
31
+ @proc.call("-"*len)
32
+ if label
33
+ @proc.call(" ")
34
+ @proc.call(label, STYLE_CLASS)
35
+ end
36
+ @proc.call("\n")
37
+ end
38
+
39
+ def display_params(method)
40
+ params = method.params
41
+
42
+ if params[0] == ?(
43
+ if method.is_singleton
44
+ params = method.full_name + params
45
+ else
46
+ params = method.name + params
47
+ end
48
+ end
49
+ params.split(/\n/).each do |param|
50
+ @proc.call(param+"\n", STYLE_BOLD)
51
+ end
52
+ end
53
+
54
+ def wrap(txt, prefix=@indent, linelen=@width)
55
+ return if !txt || txt.empty?
56
+ @proc.call(prefix, STYLE_EMPHASIS)
57
+ conv_markup(txt, prefix, linelen)
58
+ =begin
59
+ textLen = linelen - prefix.length
60
+ patt = Regexp.new("^(.{0,#{textLen}})[ \n]")
61
+ next_prefix = prefix.tr("^ ", " ")
62
+
63
+ res = []
64
+
65
+ while work.length > textLen
66
+ if work =~ patt
67
+ res << $1
68
+ work.slice!(0, $&.length)
69
+ else
70
+ res << work.slice!(0, textLen)
71
+ end
72
+ end
73
+ res << work if work.length.nonzero?
74
+ @proc.call(prefix + res.join("\n" + next_prefix) + "\n")
75
75
  =end
76
- end
77
-
78
- ######################################################################
79
-
80
- def blankline
81
- @proc.call("\n")
82
- end
83
-
84
- ######################################################################
85
-
86
- # called when we want to ensure a nbew 'wrap' starts on a newline
87
- # Only needed for HtmlFormatter, because the rest do their
88
- # own line breaking
89
-
90
- def break_to_newline
91
- end
92
-
93
- ######################################################################
94
-
95
- def bold_print(txt)
96
- @proc.call(txt, STYLE_BOLD)
97
- end
98
-
99
- ######################################################################
100
-
101
- def raw_print_line(txt)
102
- @proc.call(txt)
103
- end
104
-
105
- ######################################################################
106
-
107
- # convert HTML entities back to ASCII
108
- def conv_html(txt)
109
- txt.
110
- gsub(/&gt;/, '>').
111
- gsub(/&lt;/, '<').
112
- gsub(/&quot;/, '"').
113
- gsub(/&amp;/, '&')
114
- end
115
-
116
- # convert markup into display form
117
- def conv_markup(txt, prefix, linelen)
118
-
119
- # this code assumes that tags are not used inside tags
120
- pos = 0
121
- old_pos = 0
122
- style = STYLE_NORMAL
123
- current_indent = prefix.size
124
- while pos = txt.index(%r{(<tt>|<code>|<b>|<em>|</tt>|</code>|</b>|</em>)}, old_pos)
125
- new_part = txt[old_pos...pos]
126
- @proc.call(new_part, style)
127
-
128
- # get tag name
129
- old_pos = txt.index(">", pos) + 1
130
- style = case txt[(pos+1)...(old_pos-1)]
131
- when "tt"
132
- STYLE_TELETYPE
133
- when "code"
134
- STYLE_CODE
135
- when "b"
136
- STYLE_BOLD
137
- when "em"
138
- STYLE_EMPHASIS
139
- else
140
- # closing or unknown tags
141
- STYLE_NORMAL
142
- end
143
- end
144
- @proc.call(txt[old_pos...txt.size], style)
145
- @proc.call("\n")
146
- end
147
-
148
- ######################################################################
149
-
150
- def display_list(list)
151
- case list.type
152
- when SM::ListBase::BULLET
153
- prefixer = proc { |ignored| @indent + "* " }
154
-
155
- when SM::ListBase::NUMBER,
156
- SM::ListBase::UPPERALPHA,
157
- SM::ListBase::LOWERALPHA
158
-
159
- start = case list.type
160
- when SM::ListBase::NUMBER then 1
161
- when SM::ListBase::UPPERALPHA then 'A'
162
- when SM::ListBase::LOWERALPHA then 'a'
163
- end
164
- prefixer = proc do |ignored|
165
- res = @indent + "#{start}.".ljust(4)
166
- start = start.succ
167
- res
168
- end
169
-
170
- when SM::ListBase::LABELED
171
- prefixer = proc do |li|
172
- li.label
173
- end
174
-
175
- when SM::ListBase::NOTE
176
- longest = 0
177
- list.contents.each do |item|
178
- if item.kind_of?(SM::Flow::LI) && item.label.length > longest
179
- longest = item.label.length
180
- end
181
- end
182
-
183
- prefixer = proc do |li|
184
- @indent + li.label.ljust(longest+1)
185
- end
186
-
187
- else
188
- fail "unknown list type"
189
- end
190
-
191
- list.contents.each do |item|
192
- if item.kind_of? SM::Flow::LI
193
- prefix = prefixer.call(item)
194
- display_flow_item(item, prefix)
195
- else
196
- display_flow_item(item)
197
- end
198
- end
199
- end
200
-
201
- ######################################################################
202
-
203
- def display_flow_item(item, prefix=@indent)
204
- case item
205
- when SM::Flow::P, SM::Flow::LI
206
- wrap(conv_html(item.body), prefix)
207
- blankline
208
-
209
- when SM::Flow::LIST
210
- display_list(item)
211
-
212
- when SM::Flow::VERB
213
- display_verbatim_flow_item(item, @indent)
214
-
215
- when SM::Flow::H
216
- display_heading(conv_html(item.text.join), item.level, @indent)
217
-
218
- when SM::Flow::RULE
219
- draw_line
220
-
221
- when String
222
- wrap(conv_html(item), prefix)
223
-
224
- else
225
- fail "Unknown flow element: #{item.class}"
226
- end
227
- end
228
-
229
- ######################################################################
230
-
231
- def display_verbatim_flow_item(item, prefix=@indent)
232
- item.body.split(/\n/).each do |line|
233
- @proc.call(@indent)
234
- @proc.call(conv_html(line))
235
- @proc.call("\n")
236
- end
237
- blankline
238
- end
239
-
240
- ######################################################################
241
-
242
- def display_heading(text, level, indent)
243
- case level
244
- when 1
245
- @proc.call(text, STYLE_H1)
246
- @proc.call("\n")
247
-
248
- when 2
249
- @proc.call(text, STYLE_H2)
250
- @proc.call("\n")
251
-
252
- else
253
- @proc.call(indent)
254
- @proc.call(text, STYLE_H3)
255
- @proc.call("\n")
256
- end
257
- end
258
-
259
-
260
- def display_flow(flow)
261
- flow.each do |f|
262
- display_flow_item(f)
263
- end
264
- end
76
+ end
77
+
78
+ ######################################################################
79
+
80
+ def blankline
81
+ @proc.call("\n")
82
+ end
83
+
84
+ ######################################################################
85
+
86
+ # called when we want to ensure a nbew 'wrap' starts on a newline
87
+ # Only needed for HtmlFormatter, because the rest do their
88
+ # own line breaking
89
+
90
+ def break_to_newline
91
+ end
92
+
93
+ ######################################################################
94
+
95
+ def bold_print(txt)
96
+ @proc.call(txt, STYLE_BOLD)
97
+ end
98
+
99
+ ######################################################################
100
+
101
+ def raw_print_line(txt)
102
+ @proc.call(txt)
103
+ end
104
+
105
+ ######################################################################
106
+
107
+ # convert HTML entities back to ASCII
108
+ def conv_html(txt)
109
+ case txt
110
+ when Array
111
+ txt.join.
112
+ gsub(/&gt;/, '>').
113
+ gsub(/&lt;/, '<').
114
+ gsub(/&quot;/, '"').
115
+ gsub(/&amp;/, '&')
116
+ else # it's a String
117
+ txt.
118
+ gsub(/&gt;/, '>').
119
+ gsub(/&lt;/, '<').
120
+ gsub(/&quot;/, '"').
121
+ gsub(/&amp;/, '&')
122
+ end
123
+ end
124
+
125
+ # convert markup into display form
126
+ def conv_markup(txt, prefix, linelen)
127
+
128
+ # this code assumes that tags are not used inside tags
129
+ pos = 0
130
+ old_pos = 0
131
+ style = STYLE_NORMAL
132
+ current_indent = prefix.size
133
+ while pos = txt.index(%r{(<tt>|<code>|<b>|<em>|</tt>|</code>|</b>|</em>)}, old_pos)
134
+ new_part = txt[old_pos...pos]
135
+ @proc.call(new_part, style)
136
+
137
+ # get tag name
138
+ old_pos = txt.index(">", pos) + 1
139
+ style = case txt[(pos+1)...(old_pos-1)]
140
+ when "tt"
141
+ STYLE_TELETYPE
142
+ when "code"
143
+ STYLE_CODE
144
+ when "b"
145
+ STYLE_BOLD
146
+ when "em"
147
+ STYLE_EMPHASIS
148
+ else
149
+ # closing or unknown tags
150
+ STYLE_NORMAL
151
+ end
152
+ end
153
+ @proc.call(txt[old_pos...txt.size], style)
154
+ @proc.call("\n")
155
+ end
156
+
157
+ ######################################################################
158
+
159
+ def display_list(list)
160
+ case list.type
161
+ when SM::ListBase::BULLET
162
+ prefixer = proc { |ignored| @indent + "* " }
163
+
164
+ when SM::ListBase::NUMBER,
165
+ SM::ListBase::UPPERALPHA,
166
+ SM::ListBase::LOWERALPHA
167
+
168
+ start = case list.type
169
+ when SM::ListBase::NUMBER then 1
170
+ when SM::ListBase::UPPERALPHA then 'A'
171
+ when SM::ListBase::LOWERALPHA then 'a'
172
+ end
173
+ prefixer = proc do |ignored|
174
+ res = @indent + "#{start}.".ljust(4)
175
+ start = start.succ
176
+ res
177
+ end
178
+
179
+ when SM::ListBase::LABELED
180
+ prefixer = proc do |li|
181
+ li.label
182
+ end
183
+
184
+ when SM::ListBase::NOTE
185
+ longest = 0
186
+ list.contents.each do |item|
187
+ if item.kind_of?(SM::Flow::LI) && item.label.length > longest
188
+ longest = item.label.length
189
+ end
190
+ end
191
+
192
+ prefixer = proc do |li|
193
+ @indent + li.label.ljust(longest+1)
194
+ end
195
+
196
+ else
197
+ fail "unknown list type"
198
+ end
199
+
200
+ list.contents.each do |item|
201
+ if item.kind_of? SM::Flow::LI
202
+ prefix = prefixer.call(item)
203
+ display_flow_item(item, prefix)
204
+ else
205
+ display_flow_item(item)
206
+ end
207
+ end
208
+ end
209
+
210
+ ######################################################################
211
+
212
+ def display_flow_item(item, prefix=@indent)
213
+
214
+ case item
215
+ when SM::Flow::P, SM::Flow::LI
216
+ wrap(conv_html(item.body), prefix)
217
+ blankline
218
+
219
+ when SM::Flow::LIST
220
+ display_list(item)
221
+
222
+ when SM::Flow::VERB
223
+ display_verbatim_flow_item(item, @indent)
224
+
225
+ when SM::Flow::H
226
+ display_heading(conv_html(item.text), item.level, @indent)
227
+
228
+ when SM::Flow::RULE
229
+ draw_line
230
+
231
+ when String
232
+ wrap(conv_html(item), prefix)
233
+
234
+ else
235
+ fail "Unknown flow element: #{item.class}"
236
+ end
237
+ end
238
+
239
+ ######################################################################
240
+
241
+ def display_verbatim_flow_item(item, prefix=@indent)
242
+ item.body.split(/\n/).each do |line|
243
+ @proc.call(@indent)
244
+ @proc.call(conv_html(line))
245
+ @proc.call("\n")
246
+ end
247
+ blankline
248
+ end
249
+
250
+ ######################################################################
251
+
252
+ def display_heading(text, level, indent)
253
+ case level
254
+ when 1
255
+ @proc.call(text, STYLE_H1)
256
+ @proc.call("\n")
257
+
258
+ when 2
259
+ @proc.call(text, STYLE_H2)
260
+ @proc.call("\n")
261
+
262
+ else
263
+ @proc.call(indent)
264
+ @proc.call(text, STYLE_H3)
265
+ @proc.call("\n")
266
+ end
267
+ end
268
+
269
+
270
+ def display_flow(flow)
271
+ flow.each do |f|
272
+ display_flow_item(f)
273
+ end
274
+ end
265
275
  end