Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Removed global state from the markup attribute bitmask

RDoc::Markup::Attribute only contained global state.  It has been
renamed to RDoc::Markup::Attributes (no user should directly access it)
and the class methods have been replaced with instance methods.
  • Loading branch information...
commit 620f834cf5ce9f27ca7b9882b9927aa254d7dcbe 1 parent 1a27eb4
@drbrain drbrain authored
View
3  Manifest.txt
@@ -96,8 +96,8 @@ lib/rdoc/markdown/literals_1_9.rb
lib/rdoc/markup.rb
lib/rdoc/markup/attr_changer.rb
lib/rdoc/markup/attr_span.rb
-lib/rdoc/markup/attribute.rb
lib/rdoc/markup/attribute_manager.rb
+lib/rdoc/markup/attributes.rb
lib/rdoc/markup/blank_line.rb
lib/rdoc/markup/block_quote.rb
lib/rdoc/markup/document.rb
@@ -220,6 +220,7 @@ test/test_rdoc_markdown.rb
test/test_rdoc_markdown_test.rb
test/test_rdoc_markup.rb
test/test_rdoc_markup_attribute_manager.rb
+test/test_rdoc_markup_attributes.rb
test/test_rdoc_markup_document.rb
test/test_rdoc_markup_formatter.rb
test/test_rdoc_markup_hard_break.rb
View
2  lib/rdoc/markup.rb
@@ -832,7 +832,7 @@ def convert input, formatter
# Inline markup classes
autoload :AttrChanger, 'rdoc/markup/attr_changer'
autoload :AttrSpan, 'rdoc/markup/attr_span'
- autoload :Attribute, 'rdoc/markup/attribute'
+ autoload :Attributes, 'rdoc/markup/attributes'
autoload :AttributeManager, 'rdoc/markup/attribute_manager'
autoload :Special, 'rdoc/markup/special'
View
58 lib/rdoc/markup/attribute.rb
@@ -1,58 +0,0 @@
-##
-# We manage a set of attributes. Each attribute has a symbol name and a bit
-# value.
-
-class RDoc::Markup::Attribute
-
- ##
- # Special attribute type. See RDoc::Markup#add_special
-
- SPECIAL = 1
-
- @@name_to_bitmap = [
- [:_SPECIAL_, SPECIAL],
- ]
-
- @@next_bitmap = 2
-
- ##
- # Returns a unique bit for +name+
-
- def self.bitmap_for name
- bitmap = @@name_to_bitmap.assoc name
-
- unless bitmap then
- bitmap = @@next_bitmap
- @@next_bitmap <<= 1
- @@name_to_bitmap << [name, bitmap]
- else
- bitmap = bitmap.last
- end
-
- bitmap
- end
-
- ##
- # Returns a string representation of +bitmap+
-
- def self.as_string(bitmap)
- return "none" if bitmap.zero?
- res = []
- @@name_to_bitmap.each do |name, bit|
- res << name if (bitmap & bit) != 0
- end
- res.join(",")
- end
-
- ##
- # yields each attribute name in +bitmap+
-
- def self.each_name_of(bitmap)
- @@name_to_bitmap.each do |name, bit|
- next if bit == SPECIAL
- yield name.to_s if (bitmap & bit) != 0
- end
- end
-
-end
-
View
22 lib/rdoc/markup/attribute_manager.rb
@@ -23,6 +23,11 @@ class RDoc::Markup::AttributeManager
PROTECT_ATTR = A_PROTECT.chr # :nodoc:
##
+ # The attributes enabled for this markup object.
+
+ attr_reader :attributes
+
+ ##
# This maps delimiters that occur around words (such as *bold* or +tt+)
# where the start and end delimiters and the same. This lets us optimize
# the regexp
@@ -62,6 +67,7 @@ def initialize
@protectable = %w[<]
@special = []
@word_pair_map = {}
+ @attributes = RDoc::Markup::Attributes.new
add_word_pair "*", "*", :BOLD
add_word_pair "_", "_", :EM
@@ -96,11 +102,11 @@ def change_attribute current, new
def changed_attribute_by_name current_set, new_set
current = new = 0
current_set.each do |name|
- current |= RDoc::Markup::Attribute.bitmap_for(name)
+ current |= @attributes.bitmap_for(name)
end
new_set.each do |name|
- new |= RDoc::Markup::Attribute.bitmap_for(name)
+ new |= @attributes.bitmap_for(name)
end
change_attribute(current, new)
@@ -166,7 +172,7 @@ def convert_specials(str, attrs)
@special.each do |regexp, attr|
str.scan(regexp) do
attrs.set_attrs($`.length, $&.length,
- attr | RDoc::Markup::Attribute::SPECIAL)
+ attr | @attributes.special)
end
end
end
@@ -200,7 +206,7 @@ def add_word_pair(start, stop, name)
raise ArgumentError, "Word flags may not start with '<'" if
start[0,1] == '<'
- bitmap = RDoc::Markup::Attribute.bitmap_for name
+ bitmap = @attributes.bitmap_for name
if start == stop then
@matching_word_pairs[start] = bitmap
@@ -220,7 +226,7 @@ def add_word_pair(start, stop, name)
# am.add_html 'em', :EM
def add_html(tag, name)
- @html_tags[tag.downcase] = RDoc::Markup::Attribute.bitmap_for name
+ @html_tags[tag.downcase] = @attributes.bitmap_for name
end
##
@@ -230,7 +236,7 @@ def add_html(tag, name)
# @am.add_special(/((https?:)\S+\w)/, :HYPERLINK)
def add_special pattern, name
- @special << [pattern, RDoc::Markup::Attribute.bitmap_for(name)]
+ @special << [pattern, @attributes.bitmap_for(name)]
end
##
@@ -303,9 +309,9 @@ def split_into_flow
res << change_attribute(current_attr, new_attr)
current_attr = new_attr
- if (current_attr & RDoc::Markup::Attribute::SPECIAL) != 0 then
+ if (current_attr & @attributes.special) != 0 then
i += 1 while
- i < str_len and (@attrs[i] & RDoc::Markup::Attribute::SPECIAL) != 0
+ i < str_len and (@attrs[i] & @attributes.special) != 0
res << RDoc::Markup::Special.new(current_attr,
copy_string(start_pos, i))
View
70 lib/rdoc/markup/attributes.rb
@@ -0,0 +1,70 @@
+##
+# We manage a set of attributes. Each attribute has a symbol name and a bit
+# value.
+
+class RDoc::Markup::Attributes
+
+ ##
+ # The special attribute type. See RDoc::Markup#add_special
+
+ attr_reader :special
+
+ ##
+ # Creates a new attributes set.
+
+ def initialize
+ @special = 1
+
+ @name_to_bitmap = [
+ [:_SPECIAL_, @special],
+ ]
+
+ @next_bitmap = @special << 1
+ end
+
+ ##
+ # Returns a unique bit for +name+
+
+ def bitmap_for name
+ bitmap = @name_to_bitmap.assoc name
+
+ unless bitmap then
+ bitmap = @next_bitmap
+ @next_bitmap <<= 1
+ @name_to_bitmap << [name, bitmap]
+ else
+ bitmap = bitmap.last
+ end
+
+ bitmap
+ end
+
+ ##
+ # Returns a string representation of +bitmap+
+
+ def as_string bitmap
+ return 'none' if bitmap.zero?
+ res = []
+
+ @name_to_bitmap.each do |name, bit|
+ res << name if (bitmap & bit) != 0
+ end
+
+ res.join ','
+ end
+
+ ##
+ # yields each attribute name in +bitmap+
+
+ def each_name_of bitmap
+ return enum_for __method__, bitmap unless block_given?
+
+ @name_to_bitmap.each do |name, bit|
+ next if bit == @special
+
+ yield name.to_s if (bitmap & bit) != 0
+ end
+ end
+
+end
+
View
8 lib/rdoc/markup/formatter.rb
@@ -27,10 +27,12 @@ def initialize options, markup = nil
@am = @markup.attribute_manager
@am.add_special(/<br>/, :HARD_BREAK)
+ @attributes = @am.attributes
+
@attr_tags = []
@in_tt = 0
- @tt_bit = RDoc::Markup::Attribute.bitmap_for :TT
+ @tt_bit = @attributes.bitmap_for :TT
@hard_break = ''
end
@@ -49,7 +51,7 @@ def accept_document document
# tags for flexibility
def add_tag(name, start, stop)
- attr = RDoc::Markup::Attribute.bitmap_for name
+ attr = @attributes.bitmap_for name
@attr_tags << InlineTag.new(attr, start, stop)
end
@@ -98,7 +100,7 @@ def convert_special special
handled = false
- RDoc::Markup::Attribute.each_name_of special.type do |name|
+ @attributes.each_name_of special.type do |name|
method_name = "handle_special_#{name}"
if respond_to? method_name then
View
4 test/test_rdoc_markup_attribute_manager.rb
@@ -29,8 +29,8 @@ def setup
end
def crossref(text)
- crossref_bitmap = RDoc::Markup::Attribute.bitmap_for(:_SPECIAL_) |
- RDoc::Markup::Attribute.bitmap_for(:CROSSREF)
+ crossref_bitmap = @am.attributes.bitmap_for(:_SPECIAL_) |
+ @am.attributes.bitmap_for(:CROSSREF)
[ @am.changed_attribute_by_name([], [:CROSSREF, :_SPECIAL_]),
RDoc::Markup::Special.new(crossref_bitmap, text),
View
39 test/test_rdoc_markup_attributes.rb
@@ -0,0 +1,39 @@
+require 'rdoc/test_case'
+
+class TestRDocMarkupAttributes < RDoc::TestCase
+
+ def setup
+ super
+
+ @as = RDoc::Markup::Attributes.new
+ end
+
+ def test_bitmap_for
+ assert_equal 2, @as.bitmap_for('two')
+ assert_equal 2, @as.bitmap_for('two')
+ assert_equal 4, @as.bitmap_for('three')
+ end
+
+ def test_as_string
+ @as.bitmap_for 'two'
+ @as.bitmap_for 'three'
+
+ assert_equal 'none', @as.as_string(0)
+ assert_equal '_SPECIAL_', @as.as_string(1)
+ assert_equal 'two', @as.as_string(2)
+ assert_equal '_SPECIAL_,two', @as.as_string(3)
+ end
+
+ def test_each_name_of
+ @as.bitmap_for 'two'
+ @as.bitmap_for 'three'
+
+ assert_equal %w[], @as.each_name_of(0).to_a
+ assert_equal %w[], @as.each_name_of(1).to_a
+ assert_equal %w[two], @as.each_name_of(2).to_a
+ assert_equal %w[three], @as.each_name_of(4).to_a
+ assert_equal %w[two three], @as.each_name_of(6).to_a
+ end
+
+end
+
View
9 test/test_rdoc_markup_formatter.rb
@@ -38,11 +38,14 @@ def setup
@markup = @RM.new
@markup.add_special(/[A-Z]+/, :CAPS)
+ @attribute_manager = @markup.attribute_manager
+ @attributes = @attribute_manager.attributes
+
@to = ToTest.new @markup
- @caps = @RM::Attribute.bitmap_for :CAPS
- @special = @RM::Attribute.bitmap_for :_SPECIAL_
- @tt = @RM::Attribute.bitmap_for :TT
+ @caps = @attributes.bitmap_for :CAPS
+ @special = @attributes.bitmap_for :_SPECIAL_
+ @tt = @attributes.bitmap_for :TT
end
def test_convert_tt_special
Please sign in to comment.
Something went wrong with that request. Please try again.