2010-12-19 22:22:49 -05:00
|
|
|
require 'rdoc'
|
2008-01-13 22:34:05 -05:00
|
|
|
class RDoc::Markup
|
2003-12-01 02:12:49 -05:00
|
|
|
|
2008-01-12 22:35:34 -05:00
|
|
|
##
|
|
|
|
# We manage a set of attributes. Each attribute has a symbol name and a bit
|
2008-01-13 22:34:05 -05:00
|
|
|
# value.
|
2003-12-01 02:12:49 -05:00
|
|
|
|
|
|
|
class Attribute
|
2010-04-01 03:45:16 -04:00
|
|
|
|
|
|
|
##
|
|
|
|
# Special attribute type. See RDoc::Markup#add_special
|
|
|
|
|
2003-12-01 02:12:49 -05:00
|
|
|
SPECIAL = 1
|
|
|
|
|
|
|
|
@@name_to_bitmap = { :_SPECIAL_ => SPECIAL }
|
|
|
|
@@next_bitmap = 2
|
|
|
|
|
2010-12-19 22:22:49 -05:00
|
|
|
##
|
|
|
|
# Returns a unique bit for +name+
|
|
|
|
|
2008-02-09 22:59:08 -05:00
|
|
|
def self.bitmap_for(name)
|
2003-12-01 02:12:49 -05:00
|
|
|
bitmap = @@name_to_bitmap[name]
|
2008-02-09 22:59:08 -05:00
|
|
|
unless bitmap then
|
2003-12-01 02:12:49 -05:00
|
|
|
bitmap = @@next_bitmap
|
|
|
|
@@next_bitmap <<= 1
|
|
|
|
@@name_to_bitmap[name] = bitmap
|
|
|
|
end
|
|
|
|
bitmap
|
|
|
|
end
|
|
|
|
|
2010-12-19 22:22:49 -05:00
|
|
|
##
|
2011-02-01 19:32:30 -05:00
|
|
|
# Returns a string representation of +bitmap+
|
2010-12-19 22:22:49 -05:00
|
|
|
|
2008-02-09 22:59:08 -05:00
|
|
|
def self.as_string(bitmap)
|
2003-12-01 02:12:49 -05:00
|
|
|
return "none" if bitmap.zero?
|
|
|
|
res = []
|
|
|
|
@@name_to_bitmap.each do |name, bit|
|
|
|
|
res << name if (bitmap & bit) != 0
|
|
|
|
end
|
|
|
|
res.join(",")
|
|
|
|
end
|
|
|
|
|
2010-12-19 22:22:49 -05:00
|
|
|
##
|
|
|
|
# yields each attribute name in +bitmap+
|
|
|
|
|
2008-02-09 22:59:08 -05:00
|
|
|
def self.each_name_of(bitmap)
|
2003-12-01 02:12:49 -05:00
|
|
|
@@name_to_bitmap.each do |name, bit|
|
|
|
|
next if bit == SPECIAL
|
|
|
|
yield name.to_s if (bitmap & bit) != 0
|
|
|
|
end
|
|
|
|
end
|
2010-04-01 03:45:16 -04:00
|
|
|
|
2003-12-01 02:12:49 -05:00
|
|
|
end
|
|
|
|
|
2010-04-01 03:45:16 -04:00
|
|
|
AttrChanger = Struct.new :turn_on, :turn_off # :nodoc:
|
2008-04-26 12:14:19 -04:00
|
|
|
|
2008-01-12 22:35:34 -05:00
|
|
|
##
|
|
|
|
# An AttrChanger records a change in attributes. It contains a bitmap of the
|
2008-01-13 22:34:05 -05:00
|
|
|
# attributes to turn on, and a bitmap of those to turn off.
|
2003-12-01 02:12:49 -05:00
|
|
|
|
|
|
|
class AttrChanger
|
2010-04-01 03:45:16 -04:00
|
|
|
def to_s # :nodoc:
|
2011-06-27 22:28:25 -04:00
|
|
|
"Attr: +#{Attribute.as_string turn_on}/-#{Attribute.as_string turn_off}"
|
|
|
|
end
|
|
|
|
|
|
|
|
def inspect # :nodoc:
|
|
|
|
"+%s/-%s" % [
|
|
|
|
Attribute.as_string(turn_on),
|
|
|
|
Attribute.as_string(turn_off),
|
|
|
|
]
|
2003-12-01 02:12:49 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2008-01-12 22:35:34 -05:00
|
|
|
##
|
2008-01-13 22:34:05 -05:00
|
|
|
# An array of attributes which parallels the characters in a string.
|
2008-01-12 22:35:34 -05:00
|
|
|
|
2003-12-01 02:12:49 -05:00
|
|
|
class AttrSpan
|
2010-04-01 03:45:16 -04:00
|
|
|
|
|
|
|
##
|
|
|
|
# Creates a new AttrSpan for +length+ characters
|
|
|
|
|
2003-12-01 02:12:49 -05:00
|
|
|
def initialize(length)
|
|
|
|
@attrs = Array.new(length, 0)
|
|
|
|
end
|
|
|
|
|
2010-04-01 03:45:16 -04:00
|
|
|
##
|
|
|
|
# Toggles +bits+ from +start+ to +length+
|
2003-12-01 02:12:49 -05:00
|
|
|
def set_attrs(start, length, bits)
|
|
|
|
for i in start ... (start+length)
|
|
|
|
@attrs[i] |= bits
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2010-04-01 03:45:16 -04:00
|
|
|
##
|
2010-12-19 22:22:49 -05:00
|
|
|
# Accesses flags for character +n+
|
2010-04-01 03:45:16 -04:00
|
|
|
|
2003-12-01 02:12:49 -05:00
|
|
|
def [](n)
|
|
|
|
@attrs[n]
|
|
|
|
end
|
2010-04-01 03:45:16 -04:00
|
|
|
|
2003-12-01 02:12:49 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
##
|
|
|
|
# Hold details of a special sequence
|
|
|
|
|
|
|
|
class Special
|
2010-04-01 03:45:16 -04:00
|
|
|
|
|
|
|
##
|
|
|
|
# Special type
|
|
|
|
|
2003-12-01 02:12:49 -05:00
|
|
|
attr_reader :type
|
2010-04-01 03:45:16 -04:00
|
|
|
|
|
|
|
##
|
|
|
|
# Special text
|
|
|
|
|
2003-12-01 02:12:49 -05:00
|
|
|
attr_accessor :text
|
|
|
|
|
2010-04-01 03:45:16 -04:00
|
|
|
##
|
|
|
|
# Creates a new special sequence of +type+ with +text+
|
|
|
|
|
2003-12-01 02:12:49 -05:00
|
|
|
def initialize(type, text)
|
|
|
|
@type, @text = type, text
|
|
|
|
end
|
|
|
|
|
2010-04-01 03:45:16 -04:00
|
|
|
##
|
|
|
|
# Specials are equal when the have the same text and type
|
|
|
|
|
2003-12-01 02:12:49 -05:00
|
|
|
def ==(o)
|
|
|
|
self.text == o.text && self.type == o.type
|
|
|
|
end
|
|
|
|
|
2010-04-01 03:45:16 -04:00
|
|
|
def inspect # :nodoc:
|
2008-01-13 22:34:05 -05:00
|
|
|
"#<RDoc::Markup::Special:0x%x @type=%p, name=%p @text=%p>" % [
|
|
|
|
object_id, @type, RDoc::Markup::Attribute.as_string(type), text.dump]
|
2003-12-01 02:12:49 -05:00
|
|
|
end
|
2008-02-09 22:59:08 -05:00
|
|
|
|
2010-04-01 03:45:16 -04:00
|
|
|
def to_s # :nodoc:
|
2008-02-09 22:59:08 -05:00
|
|
|
"Special: type=#{type}, name=#{RDoc::Markup::Attribute.as_string type}, text=#{text.dump}"
|
|
|
|
end
|
|
|
|
|
2003-12-01 02:12:49 -05:00
|
|
|
end
|
2008-01-12 22:35:34 -05:00
|
|
|
|
2003-12-01 02:12:49 -05:00
|
|
|
end
|
2008-01-12 22:35:34 -05:00
|
|
|
|