PageRenderTime 47ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/benchmark/app/rdoc-2.4.3/lib/rdoc/markup/attribute_manager.rb

http://github.com/rubinius/rubinius
Ruby | 311 lines | 173 code | 71 blank | 67 comment | 22 complexity | e8997c671390feba6c00b384080bd578 MD5 | raw file
Possible License(s): BSD-3-Clause, MPL-2.0-no-copyleft-exception, 0BSD, GPL-2.0, LGPL-2.1
  1. require 'rdoc/markup/inline'
  2. ##
  3. # Manages changes of attributes in a block of text
  4. class RDoc::Markup::AttributeManager
  5. ##
  6. # The NUL character
  7. NULL = "\000".freeze
  8. ##
  9. # We work by substituting non-printing characters in to the text. For now
  10. # I'm assuming that I can substitute a character in the range 0..8 for a 7
  11. # bit character without damaging the encoded string, but this might be
  12. # optimistic
  13. A_PROTECT = 004
  14. PROTECT_ATTR = A_PROTECT.chr
  15. ##
  16. # This maps delimiters that occur around words (such as *bold* or +tt+)
  17. # where the start and end delimiters and the same. This lets us optimize
  18. # the regexp
  19. MATCHING_WORD_PAIRS = {}
  20. ##
  21. # And this is used when the delimiters aren't the same. In this case the
  22. # hash maps a pattern to the attribute character
  23. WORD_PAIR_MAP = {}
  24. ##
  25. # This maps HTML tags to the corresponding attribute char
  26. HTML_TAGS = {}
  27. ##
  28. # And this maps _special_ sequences to a name. A special sequence is
  29. # something like a WikiWord
  30. SPECIAL = {}
  31. ##
  32. # Return an attribute object with the given turn_on and turn_off bits set
  33. def attribute(turn_on, turn_off)
  34. RDoc::Markup::AttrChanger.new turn_on, turn_off
  35. end
  36. def change_attribute(current, new)
  37. diff = current ^ new
  38. attribute(new & diff, current & diff)
  39. end
  40. def changed_attribute_by_name(current_set, new_set)
  41. current = new = 0
  42. current_set.each do |name|
  43. current |= RDoc::Markup::Attribute.bitmap_for(name)
  44. end
  45. new_set.each do |name|
  46. new |= RDoc::Markup::Attribute.bitmap_for(name)
  47. end
  48. change_attribute(current, new)
  49. end
  50. def copy_string(start_pos, end_pos)
  51. res = @str[start_pos...end_pos]
  52. res.gsub!(/\000/, '')
  53. res
  54. end
  55. ##
  56. # Map attributes like <b>text</b>to the sequence
  57. # \001\002<char>\001\003<char>, where <char> is a per-attribute specific
  58. # character
  59. def convert_attrs(str, attrs)
  60. # first do matching ones
  61. tags = MATCHING_WORD_PAIRS.keys.join("")
  62. re = /(^|\W)([#{tags}])([#:\\]?[\w.\/-]+?\S?)\2(\W|$)/
  63. 1 while str.gsub!(re) do
  64. attr = MATCHING_WORD_PAIRS[$2]
  65. attrs.set_attrs($`.length + $1.length + $2.length, $3.length, attr)
  66. $1 + NULL * $2.length + $3 + NULL * $2.length + $4
  67. end
  68. # then non-matching
  69. unless WORD_PAIR_MAP.empty? then
  70. WORD_PAIR_MAP.each do |regexp, attr|
  71. str.gsub!(regexp) {
  72. attrs.set_attrs($`.length + $1.length, $2.length, attr)
  73. NULL * $1.length + $2 + NULL * $3.length
  74. }
  75. end
  76. end
  77. end
  78. ##
  79. # Converts HTML tags to RDoc attributes
  80. def convert_html(str, attrs)
  81. tags = HTML_TAGS.keys.join '|'
  82. 1 while str.gsub!(/<(#{tags})>(.*?)<\/\1>/i) {
  83. attr = HTML_TAGS[$1.downcase]
  84. html_length = $1.length + 2
  85. seq = NULL * html_length
  86. attrs.set_attrs($`.length + html_length, $2.length, attr)
  87. seq + $2 + seq + NULL
  88. }
  89. end
  90. ##
  91. # Converts special sequences to RDoc attributes
  92. def convert_specials(str, attrs)
  93. unless SPECIAL.empty?
  94. SPECIAL.each do |regexp, attr|
  95. str.scan(regexp) do
  96. attrs.set_attrs($`.length, $&.length,
  97. attr | RDoc::Markup::Attribute::SPECIAL)
  98. end
  99. end
  100. end
  101. end
  102. ##
  103. # A \ in front of a character that would normally be processed turns off
  104. # processing. We do this by turning \< into <#{PROTECT}
  105. PROTECTABLE = %w[<\\]
  106. ##
  107. # Escapes special sequences of text to prevent conversion to RDoc
  108. def mask_protected_sequences
  109. protect_pattern = Regexp.new("\\\\([#{Regexp.escape(PROTECTABLE.join(''))}])")
  110. @str.gsub!(protect_pattern, "\\1#{PROTECT_ATTR}")
  111. end
  112. ##
  113. # Unescapes special sequences of text
  114. def unmask_protected_sequences
  115. @str.gsub!(/(.)#{PROTECT_ATTR}/, "\\1\000")
  116. end
  117. ##
  118. # Creates a new attribute manager that understands bold, emphasized and
  119. # teletype text.
  120. def initialize
  121. add_word_pair("*", "*", :BOLD)
  122. add_word_pair("_", "_", :EM)
  123. add_word_pair("+", "+", :TT)
  124. add_html("em", :EM)
  125. add_html("i", :EM)
  126. add_html("b", :BOLD)
  127. add_html("tt", :TT)
  128. add_html("code", :TT)
  129. end
  130. ##
  131. # Adds a markup class with +name+ for words wrapped in the +start+ and
  132. # +stop+ character. To make words wrapped with "*" bold:
  133. #
  134. # am.add_word_pair '*', '*', :BOLD
  135. def add_word_pair(start, stop, name)
  136. raise ArgumentError, "Word flags may not start with '<'" if
  137. start[0,1] == '<'
  138. bitmap = RDoc::Markup::Attribute.bitmap_for name
  139. if start == stop then
  140. MATCHING_WORD_PAIRS[start] = bitmap
  141. else
  142. pattern = /(#{Regexp.escape start})(\S+)(#{Regexp.escape stop})/
  143. WORD_PAIR_MAP[pattern] = bitmap
  144. end
  145. PROTECTABLE << start[0,1]
  146. PROTECTABLE.uniq!
  147. end
  148. ##
  149. # Adds a markup class with +name+ for words surrounded by HTML tag +tag+.
  150. # To process emphasis tags:
  151. #
  152. # am.add_html 'em', :EM
  153. def add_html(tag, name)
  154. HTML_TAGS[tag.downcase] = RDoc::Markup::Attribute.bitmap_for name
  155. end
  156. ##
  157. # Adds a special handler for +pattern+ with +name+. A simple URL handler
  158. # would be:
  159. #
  160. # @am.add_special(/((https?:)\S+\w)/, :HYPERLINK)
  161. def add_special(pattern, name)
  162. SPECIAL[pattern] = RDoc::Markup::Attribute.bitmap_for name
  163. end
  164. ##
  165. # Processes +str+ converting attributes, HTML and specials
  166. def flow(str)
  167. @str = str
  168. mask_protected_sequences
  169. @attrs = RDoc::Markup::AttrSpan.new @str.length
  170. convert_attrs(@str, @attrs)
  171. convert_html(@str, @attrs)
  172. convert_specials(str, @attrs)
  173. unmask_protected_sequences
  174. split_into_flow
  175. end
  176. ##
  177. # Debug method that prints a string along with its attributes
  178. def display_attributes
  179. puts
  180. puts @str.tr(NULL, "!")
  181. bit = 1
  182. 16.times do |bno|
  183. line = ""
  184. @str.length.times do |i|
  185. if (@attrs[i] & bit) == 0
  186. line << " "
  187. else
  188. if bno.zero?
  189. line << "S"
  190. else
  191. line << ("%d" % (bno+1))
  192. end
  193. end
  194. end
  195. puts(line) unless line =~ /^ *$/
  196. bit <<= 1
  197. end
  198. end
  199. def split_into_flow
  200. res = []
  201. current_attr = 0
  202. str = ""
  203. str_len = @str.length
  204. # skip leading invisible text
  205. i = 0
  206. i += 1 while i < str_len and @str[i].chr == "\0"
  207. start_pos = i
  208. # then scan the string, chunking it on attribute changes
  209. while i < str_len
  210. new_attr = @attrs[i]
  211. if new_attr != current_attr
  212. if i > start_pos
  213. res << copy_string(start_pos, i)
  214. start_pos = i
  215. end
  216. res << change_attribute(current_attr, new_attr)
  217. current_attr = new_attr
  218. if (current_attr & RDoc::Markup::Attribute::SPECIAL) != 0 then
  219. i += 1 while
  220. i < str_len and (@attrs[i] & RDoc::Markup::Attribute::SPECIAL) != 0
  221. res << RDoc::Markup::Special.new(current_attr,
  222. copy_string(start_pos, i))
  223. start_pos = i
  224. next
  225. end
  226. end
  227. # move on, skipping any invisible characters
  228. begin
  229. i += 1
  230. end while i < str_len and @str[i].chr == "\0"
  231. end
  232. # tidy up trailing text
  233. if start_pos < str_len
  234. res << copy_string(start_pos, str_len)
  235. end
  236. # and reset to all attributes off
  237. res << change_attribute(current_attr, 0) if current_attr != 0
  238. res
  239. end
  240. end