Skip to content

Commit 0960259

Browse files
authored
Merge pull request #652 from aycabta/rename-special-with-regexp-handling
Rename the name "special" with "regexp handling"
2 parents 4259db4 + c047201 commit 0960259

22 files changed

+235
-236
lines changed

lib/rdoc/cross_reference.rb

+2-2
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@ class RDoc::CrossReference
2323

2424
##
2525
# Regular expressions matching text that should potentially have
26-
# cross-reference links generated are passed to add_special. Note that
27-
# these expressions are meant to pick up text for which cross-references
26+
# cross-reference links generated are passed to add_regexp_handling. Note
27+
# that these expressions are meant to pick up text for which cross-references
2828
# have been suppressed, since the suppression characters are removed by the
2929
# code that is triggered.
3030

lib/rdoc/markup.rb

+10-12
Original file line numberDiff line numberDiff line change
@@ -65,25 +65,24 @@
6565
# puts h.convert(input_string)
6666
#
6767
# You can extend the RDoc::Markup parser to recognize new markup
68-
# sequences, and to add special processing for text that matches a
69-
# regular expression. Here we make WikiWords significant to the parser,
70-
# and also make the sequences {word} and \<no>text...</no> signify
68+
# sequences, and to add regexp handling. Here we make WikiWords significant to
69+
# the parser, and also make the sequences {word} and \<no>text...</no> signify
7170
# strike-through text. We then subclass the HTML output class to deal
7271
# with these:
7372
#
7473
# require 'rdoc'
7574
#
7675
# class WikiHtml < RDoc::Markup::ToHtml
77-
# def handle_special_WIKIWORD(special)
78-
# "<font color=red>" + special.text + "</font>"
76+
# def handle_regexp_WIKIWORD(target)
77+
# "<font color=red>" + target.text + "</font>"
7978
# end
8079
# end
8180
#
8281
# markup = RDoc::Markup.new
8382
# markup.add_word_pair("{", "}", :STRIKE)
8483
# markup.add_html("no", :STRIKE)
8584
#
86-
# markup.add_special(/\b([A-Z][a-z]+[A-Z]\w+)/, :WIKIWORD)
85+
# markup.add_regexp_handling(/\b([A-Z][a-z]+[A-Z]\w+)/, :WIKIWORD)
8786
#
8887
# wh = WikiHtml.new RDoc::Options.new, markup
8988
# wh.add_tag(:STRIKE, "<strike>", "</strike>")
@@ -800,13 +799,12 @@ def add_html(tag, name)
800799
# Add to other inline sequences. For example, we could add WikiWords using
801800
# something like:
802801
#
803-
# parser.add_special(/\b([A-Z][a-z]+[A-Z]\w+)/, :WIKIWORD)
802+
# parser.add_regexp_handling(/\b([A-Z][a-z]+[A-Z]\w+)/, :WIKIWORD)
804803
#
805-
# Each wiki word will be presented to the output formatter via the
806-
# accept_special method.
804+
# Each wiki word will be presented to the output formatter.
807805

808-
def add_special(pattern, name)
809-
@attribute_manager.add_special(pattern, name)
806+
def add_regexp_handling(pattern, name)
807+
@attribute_manager.add_regexp_handling(pattern, name)
810808
end
811809

812810
##
@@ -832,7 +830,7 @@ def convert input, formatter
832830
autoload :AttrSpan, 'rdoc/markup/attr_span'
833831
autoload :Attributes, 'rdoc/markup/attributes'
834832
autoload :AttributeManager, 'rdoc/markup/attribute_manager'
835-
autoload :Special, 'rdoc/markup/special'
833+
autoload :RegexpHandling, 'rdoc/markup/regexp_handling'
836834

837835
# RDoc::Markup AST
838836
autoload :BlankLine, 'rdoc/markup/blank_line'

lib/rdoc/markup/attribute_manager.rb

+22-22
Original file line numberDiff line numberDiff line change
@@ -53,10 +53,10 @@ class RDoc::Markup::AttributeManager
5353
attr_reader :protectable
5454

5555
##
56-
# And this maps _special_ sequences to a name. A special sequence is
57-
# something like a WikiWord
56+
# And this maps _regexp handling_ sequences to a name. A regexp handling
57+
# sequence is something like a WikiWord
5858

59-
attr_reader :special
59+
attr_reader :regexp_handlings
6060

6161
##
6262
# Creates a new attribute manager that understands bold, emphasized and
@@ -66,7 +66,7 @@ def initialize
6666
@html_tags = {}
6767
@matching_word_pairs = {}
6868
@protectable = %w[<]
69-
@special = []
69+
@regexp_handlings = []
7070
@word_pair_map = {}
7171
@attributes = RDoc::Markup::Attributes.new
7272

@@ -166,22 +166,22 @@ def convert_html(str, attrs)
166166
end
167167

168168
##
169-
# Converts special sequences to RDoc attributes
169+
# Converts regexp handling sequences to RDoc attributes
170170

171-
def convert_specials str, attrs
172-
@special.each do |regexp, attribute|
171+
def convert_regexp_handlings str, attrs
172+
@regexp_handlings.each do |regexp, attribute|
173173
str.scan(regexp) do
174174
capture = $~.size == 1 ? 0 : 1
175175

176176
s, e = $~.offset capture
177177

178-
attrs.set_attrs s, e - s, attribute | @attributes.special
178+
attrs.set_attrs s, e - s, attribute | @attributes.regexp_handling
179179
end
180180
end
181181
end
182182

183183
##
184-
# Escapes special sequences of text to prevent conversion to RDoc
184+
# Escapes regexp handling sequences of text to prevent conversion to RDoc
185185

186186
def mask_protected_sequences
187187
# protect __send__, __FILE__, etc.
@@ -193,7 +193,7 @@ def mask_protected_sequences
193193
end
194194

195195
##
196-
# Unescapes special sequences of text
196+
# Unescapes regexp handling sequences of text
197197

198198
def unmask_protected_sequences
199199
@str.gsub!(/(.)#{PROTECT_ATTR}/, "\\1\000")
@@ -233,17 +233,17 @@ def add_html(tag, name)
233233
end
234234

235235
##
236-
# Adds a special handler for +pattern+ with +name+. A simple URL handler
236+
# Adds a regexp handling for +pattern+ with +name+. A simple URL handler
237237
# would be:
238238
#
239-
# @am.add_special(/((https?:)\S+\w)/, :HYPERLINK)
239+
# @am.add_regexp_handling(/((https?:)\S+\w)/, :HYPERLINK)
240240

241-
def add_special pattern, name
242-
@special << [pattern, @attributes.bitmap_for(name)]
241+
def add_regexp_handling pattern, name
242+
@regexp_handlings << [pattern, @attributes.bitmap_for(name)]
243243
end
244244

245245
##
246-
# Processes +str+ converting attributes, HTML and specials
246+
# Processes +str+ converting attributes, HTML and regexp handlings
247247

248248
def flow str
249249
@str = str.dup
@@ -252,9 +252,9 @@ def flow str
252252

253253
@attrs = RDoc::Markup::AttrSpan.new @str.length
254254

255-
convert_attrs @str, @attrs
256-
convert_html @str, @attrs
257-
convert_specials @str, @attrs
255+
convert_attrs @str, @attrs
256+
convert_html @str, @attrs
257+
convert_regexp_handlings @str, @attrs
258258

259259
unmask_protected_sequences
260260

@@ -312,12 +312,12 @@ def split_into_flow
312312
res << change_attribute(current_attr, new_attr)
313313
current_attr = new_attr
314314

315-
if (current_attr & @attributes.special) != 0 then
315+
if (current_attr & @attributes.regexp_handling) != 0 then
316316
i += 1 while
317-
i < str_len and (@attrs[i] & @attributes.special) != 0
317+
i < str_len and (@attrs[i] & @attributes.regexp_handling) != 0
318318

319-
res << RDoc::Markup::Special.new(current_attr,
320-
copy_string(start_pos, i))
319+
res << RDoc::Markup::RegexpHandling.new(current_attr,
320+
copy_string(start_pos, i))
321321
start_pos = i
322322
next
323323
end

lib/rdoc/markup/attributes.rb

+6-6
Original file line numberDiff line numberDiff line change
@@ -6,21 +6,21 @@
66
class RDoc::Markup::Attributes
77

88
##
9-
# The special attribute type. See RDoc::Markup#add_special
9+
# The regexp handling attribute type. See RDoc::Markup#add_regexp_handling
1010

11-
attr_reader :special
11+
attr_reader :regexp_handling
1212

1313
##
1414
# Creates a new attributes set.
1515

1616
def initialize
17-
@special = 1
17+
@regexp_handling = 1
1818

1919
@name_to_bitmap = [
20-
[:_SPECIAL_, @special],
20+
[:_REGEXP_HANDLING_, @regexp_handling],
2121
]
2222

23-
@next_bitmap = @special << 1
23+
@next_bitmap = @regexp_handling << 1
2424
end
2525

2626
##
@@ -61,7 +61,7 @@ def each_name_of bitmap
6161
return enum_for __method__, bitmap unless block_given?
6262

6363
@name_to_bitmap.each do |name, bit|
64-
next if bit == @special
64+
next if bit == @regexp_handling
6565

6666
yield name.to_s if (bitmap & bit) != 0
6767
end

lib/rdoc/markup/formatter.rb

+24-23
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ def initialize options, markup = nil
5050

5151
@markup = markup || RDoc::Markup.new
5252
@am = @markup.attribute_manager
53-
@am.add_special(/<br>/, :HARD_BREAK)
53+
@am.add_regexp_handling(/<br>/, :HARD_BREAK)
5454

5555
@attributes = @am.attributes
5656

@@ -78,23 +78,24 @@ def accept_document document
7878
end
7979

8080
##
81-
# Adds a special for links of the form rdoc-...:
81+
# Adds a regexp handling for links of the form rdoc-...:
8282

83-
def add_special_RDOCLINK
84-
@markup.add_special(/rdoc-[a-z]+:[^\s\]]+/, :RDOCLINK)
83+
def add_regexp_handling_RDOCLINK
84+
@markup.add_regexp_handling(/rdoc-[a-z]+:[^\s\]]+/, :RDOCLINK)
8585
end
8686

8787
##
88-
# Adds a special for links of the form {<text>}[<url>] and <word>[<url>]
88+
# Adds a regexp handling for links of the form {<text>}[<url>] and
89+
# <word>[<url>]
8990

90-
def add_special_TIDYLINK
91-
@markup.add_special(/(?:
92-
\{.*?\} | # multi-word label
93-
\b[^\s{}]+? # single-word label
94-
)
91+
def add_regexp_handling_TIDYLINK
92+
@markup.add_regexp_handling(/(?:
93+
\{.*?\} | # multi-word label
94+
\b[^\s{}]+? # single-word label
95+
)
9596
96-
\[\S+?\] # link target
97-
/x, :TIDYLINK)
97+
\[\S+?\] # link target
98+
/x, :TIDYLINK)
9899
end
99100

100101
##
@@ -133,8 +134,8 @@ def convert_flow(flow)
133134
when RDoc::Markup::AttrChanger then
134135
off_tags res, item
135136
on_tags res, item
136-
when RDoc::Markup::Special then
137-
res << convert_special(item)
137+
when RDoc::Markup::RegexpHandling then
138+
res << convert_regexp_handling(item)
138139
else
139140
raise "Unknown flow element: #{item.inspect}"
140141
end
@@ -144,29 +145,29 @@ def convert_flow(flow)
144145
end
145146

146147
##
147-
# Converts added specials. See RDoc::Markup#add_special
148+
# Converts added regexp handlings. See RDoc::Markup#add_regexp_handling
148149

149-
def convert_special special
150-
return special.text if in_tt?
150+
def convert_regexp_handling target
151+
return target.text if in_tt?
151152

152153
handled = false
153154

154-
@attributes.each_name_of special.type do |name|
155-
method_name = "handle_special_#{name}"
155+
@attributes.each_name_of target.type do |name|
156+
method_name = "handle_regexp_#{name}"
156157

157158
if respond_to? method_name then
158-
special.text = send method_name, special
159+
target.text = send method_name, target
159160
handled = true
160161
end
161162
end
162163

163164
unless handled then
164-
special_name = @attributes.as_string special.type
165+
target_name = @attributes.as_string target.type
165166

166-
raise RDoc::Error, "Unhandled special #{special_name}: #{special}"
167+
raise RDoc::Error, "Unhandled regexp handling #{target_name}: #{target}"
167168
end
168169

169-
special.text
170+
target.text
170171
end
171172

172173
##

lib/rdoc/markup/heading.rb

+3-3
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,12 @@ def self.to_html
2323
return @to_html if @to_html
2424

2525
markup = RDoc::Markup.new
26-
markup.add_special RDoc::CrossReference::CROSSREF_REGEXP, :CROSSREF
26+
markup.add_regexp_handling RDoc::CrossReference::CROSSREF_REGEXP, :CROSSREF
2727

2828
@to_html = RDoc::Markup::ToHtml.new nil
2929

30-
def @to_html.handle_special_CROSSREF special
31-
special.text.sub(/^\\/, '')
30+
def @to_html.handle_regexp_CROSSREF target
31+
target.text.sub(/^\\/, '')
3232
end
3333

3434
@to_html

lib/rdoc/markup/regexp_handling.rb

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
# frozen_string_literal: true
2+
##
3+
# Hold details of a regexp handling sequence
4+
5+
class RDoc::Markup::RegexpHandling
6+
7+
##
8+
# Regexp handling type
9+
10+
attr_reader :type
11+
12+
##
13+
# Regexp handling text
14+
15+
attr_accessor :text
16+
17+
##
18+
# Creates a new regexp handling sequence of +type+ with +text+
19+
20+
def initialize(type, text)
21+
@type, @text = type, text
22+
end
23+
24+
##
25+
# Regexp handlings are equal when the have the same text and type
26+
27+
def ==(o)
28+
self.text == o.text && self.type == o.type
29+
end
30+
31+
def inspect # :nodoc:
32+
"#<RDoc::Markup::RegexpHandling:0x%x @type=%p, @text=%p>" % [
33+
object_id, @type, text.dump]
34+
end
35+
36+
def to_s # :nodoc:
37+
"RegexpHandling: type=#{type} text=#{text.dump}"
38+
end
39+
40+
end
41+

0 commit comments

Comments
 (0)