1 require 'rdoc/markup/inline'
3 class RDoc::Markup::AttributeManager
8 # We work by substituting non-printing characters in to the text. For now
9 # I'm assuming that I can substitute a character in the range 0..8 for a 7
10 # bit character without damaging the encoded string, but this might be
14 PROTECT_ATTR = A_PROTECT.chr
17 # This maps delimiters that occur around words (such as *bold* or +tt+)
18 # where the start and end delimiters and the same. This lets us optimize
21 MATCHING_WORD_PAIRS = {}
24 # And this is used when the delimiters aren't the same. In this case the
25 # hash maps a pattern to the attribute character
30 # This maps HTML tags to the corresponding attribute char
35 # And this maps _special_ sequences to a name. A special sequence is
36 # something like a WikiWord
41 # Return an attribute object with the given turn_on and turn_off bits set
43 def attribute(turn_on, turn_off)
44 RDoc::Markup::AttrChanger.new turn_on, turn_off
47 def change_attribute(current, new)
49 attribute(new & diff, current & diff)
52 def changed_attribute_by_name(current_set, new_set)
54 current_set.each do |name|
55 current |= RDoc::Markup::Attribute.bitmap_for(name)
58 new_set.each do |name|
59 new |= RDoc::Markup::Attribute.bitmap_for(name)
62 change_attribute(current, new)
65 def copy_string(start_pos, end_pos)
66 res = @str[start_pos...end_pos]
72 # Map attributes like <b>text</b>to the sequence
73 # \001\002<char>\001\003<char>, where <char> is a per-attribute specific
76 def convert_attrs(str, attrs)
77 # first do matching ones
78 tags = MATCHING_WORD_PAIRS.keys.join("")
80 re = /(^|\W)([#{tags}])([#:\\]?[\w.\/-]+?\S?)\2(\W|$)/
82 1 while str.gsub!(re) do
83 attr = MATCHING_WORD_PAIRS[$2]
84 attrs.set_attrs($`.length + $1.length + $2.length, $3.length, attr)
85 $1 + NULL * $2.length + $3 + NULL * $2.length + $4
89 unless WORD_PAIR_MAP.empty? then
90 WORD_PAIR_MAP.each do |regexp, attr|
92 attrs.set_attrs($`.length + $1.length, $2.length, attr)
93 NULL * $1.length + $2 + NULL * $3.length
99 def convert_html(str, attrs)
100 tags = HTML_TAGS.keys.join '|'
102 1 while str.gsub!(/<(#{tags})>(.*?)<\/\1>/i) {
103 attr = HTML_TAGS[$1.downcase]
104 html_length = $1.length + 2
105 seq = NULL * html_length
106 attrs.set_attrs($`.length + html_length, $2.length, attr)
107 seq + $2 + seq + NULL
111 def convert_specials(str, attrs)
112 unless SPECIAL.empty?
113 SPECIAL.each do |regexp, attr|
115 attrs.set_attrs($`.length, $&.length,
116 attr | RDoc::Markup::Attribute::SPECIAL)
123 # A \ in front of a character that would normally be processed turns off
124 # processing. We do this by turning \< into <#{PROTECT}
126 PROTECTABLE = %w[<\\]
128 def mask_protected_sequences
129 protect_pattern = Regexp.new("\\\\([#{Regexp.escape(PROTECTABLE.join(''))}])")
130 @str.gsub!(protect_pattern, "\\1#{PROTECT_ATTR}")
133 def unmask_protected_sequences
134 @str.gsub!(/(.)#{PROTECT_ATTR}/, "\\1\000")
138 add_word_pair("*", "*", :BOLD)
139 add_word_pair("_", "_", :EM)
140 add_word_pair("+", "+", :TT)
146 add_html("code", :TT)
149 def add_word_pair(start, stop, name)
150 raise ArgumentError, "Word flags may not start with '<'" if
153 bitmap = RDoc::Markup::Attribute.bitmap_for name
155 if start == stop then
156 MATCHING_WORD_PAIRS[start] = bitmap
158 pattern = /(#{Regexp.escape start})(\S+)(#{Regexp.escape stop})/
159 WORD_PAIR_MAP[pattern] = bitmap
162 PROTECTABLE << start[0,1]
166 def add_html(tag, name)
167 HTML_TAGS[tag.downcase] = RDoc::Markup::Attribute.bitmap_for name
170 def add_special(pattern, name)
171 SPECIAL[pattern] = RDoc::Markup::Attribute.bitmap_for name
177 mask_protected_sequences
179 @attrs = RDoc::Markup::AttrSpan.new @str.length
181 convert_attrs(@str, @attrs)
182 convert_html(@str, @attrs)
183 convert_specials(str, @attrs)
185 unmask_protected_sequences
187 return split_into_flow
190 def display_attributes
192 puts @str.tr(NULL, "!")
196 @str.length.times do |i|
197 if (@attrs[i] & bit) == 0
203 line << ("%d" % (bno+1))
207 puts(line) unless line =~ /^ *$/
217 str_len = @str.length
219 # skip leading invisible text
221 i += 1 while i < str_len and @str[i].chr == "\0"
224 # then scan the string, chunking it on attribute changes
227 if new_attr != current_attr
229 res << copy_string(start_pos, i)
233 res << change_attribute(current_attr, new_attr)
234 current_attr = new_attr
236 if (current_attr & RDoc::Markup::Attribute::SPECIAL) != 0 then
238 i < str_len and (@attrs[i] & RDoc::Markup::Attribute::SPECIAL) != 0
240 res << RDoc::Markup::Special.new(current_attr,
241 copy_string(start_pos, i))
247 # move on, skipping any invisible characters
250 end while i < str_len and @str[i].chr == "\0"
253 # tidy up trailing text
254 if start_pos < str_len
255 res << copy_string(start_pos, str_len)
258 # and reset to all attributes off
259 res << change_attribute(current_attr, 0) if current_attr != 0