fxri 0.3.3 → 0.3.4

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,275 +1,275 @@
1
- # This class is mostly copy & paste from ri_formatter.rb.
2
- # except that it always returns a string and does not print anything.
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")
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
- 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
1
+ # This class is mostly copy & paste from ri_formatter.rb.
2
+ # except that it always returns a string and does not print anything.
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")
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
+ 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
275
275
  end