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)
148 add_special(/<!--(.*?)-->/, :COMMENT)
151 def add_word_pair(start, stop, name)
152 raise ArgumentError, "Word flags may not start with '<'" if
155 bitmap = RDoc::Markup::Attribute.bitmap_for name
157 if start == stop then
158 MATCHING_WORD_PAIRS[start] = bitmap
160 pattern = /(#{Regexp.escape start})(\S+)(#{Regexp.escape stop})/
161 WORD_PAIR_MAP[pattern] = bitmap
164 PROTECTABLE << start[0,1]
168 def add_html(tag, name)
169 HTML_TAGS[tag.downcase] = RDoc::Markup::Attribute.bitmap_for name
172 def add_special(pattern, name)
173 SPECIAL[pattern] = RDoc::Markup::Attribute.bitmap_for name
179 puts("Before flow, str='#{@str.dump}'") if $DEBUG_RDOC
180 mask_protected_sequences
182 @attrs = RDoc::Markup::AttrSpan.new @str.length
184 puts("After protecting, str='#{@str.dump}'") if $DEBUG_RDOC
186 convert_attrs(@str, @attrs)
187 convert_html(@str, @attrs)
188 convert_specials(str, @attrs)
190 unmask_protected_sequences
192 puts("After flow, str='#{@str.dump}'") if $DEBUG_RDOC
194 return split_into_flow
197 def display_attributes
199 puts @str.tr(NULL, "!")
203 @str.length.times do |i|
204 if (@attrs[i] & bit) == 0
210 line << ("%d" % (bno+1))
214 puts(line) unless line =~ /^ *$/
220 display_attributes if $DEBUG_RDOC
226 str_len = @str.length
228 # skip leading invisible text
230 i += 1 while i < str_len and @str[i].chr == "\0"
233 # then scan the string, chunking it on attribute changes
236 if new_attr != current_attr
238 res << copy_string(start_pos, i)
242 res << change_attribute(current_attr, new_attr)
243 current_attr = new_attr
245 if (current_attr & RDoc::Markup::Attribute::SPECIAL) != 0 then
247 i < str_len and (@attrs[i] & RDoc::Markup::Attribute::SPECIAL) != 0
249 res << RDoc::Markup::Special.new(current_attr,
250 copy_string(start_pos, i))
256 # move on, skipping any invisible characters
259 end while i < str_len and @str[i].chr == "\0"
262 # tidy up trailing text
263 if start_pos < str_len
264 res << copy_string(start_pos, str_len)
267 # and reset to all attributes off
268 res << change_attribute(current_attr, 0) if current_attr != 0