You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

redcloth3.rb 39KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226
  1. # frozen_string_literal: true
  2. # vim:ts=4:sw=4:
  3. # = RedCloth - Textile and Markdown Hybrid for Ruby
  4. #
  5. # Homepage:: http://whytheluckystiff.net/ruby/redcloth/
  6. # Author:: why the lucky stiff (http://whytheluckystiff.net/)
  7. # Copyright:: (cc) 2004 why the lucky stiff (and his puppet organizations.)
  8. # License:: BSD
  9. #
  10. # (see http://hobix.com/textile/ for a Textile Reference.)
  11. #
  12. # Based on (and also inspired by) both:
  13. #
  14. # PyTextile: http://diveintomark.org/projects/textile/textile.py.txt
  15. # Textism for PHP: http://www.textism.com/tools/textile/
  16. #
  17. #
  18. # = RedCloth
  19. #
  20. # RedCloth is a Ruby library for converting Textile and/or Markdown
  21. # into HTML. You can use either format, intermingled or separately.
  22. # You can also extend RedCloth to honor your own custom text stylings.
  23. #
  24. # RedCloth users are encouraged to use Textile if they are generating
  25. # HTML and to use Markdown if others will be viewing the plain text.
  26. #
  27. # == What is Textile?
  28. #
  29. # Textile is a simple formatting style for text
  30. # documents, loosely based on some HTML conventions.
  31. #
  32. # == Sample Textile Text
  33. #
  34. # h2. This is a title
  35. #
  36. # h3. This is a subhead
  37. #
  38. # This is a bit of paragraph.
  39. #
  40. # bq. This is a blockquote.
  41. #
  42. # = Writing Textile
  43. #
  44. # A Textile document consists of paragraphs. Paragraphs
  45. # can be specially formatted by adding a small instruction
  46. # to the beginning of the paragraph.
  47. #
  48. # h[n]. Header of size [n].
  49. # bq. Blockquote.
  50. # # Numeric list.
  51. # * Bulleted list.
  52. #
  53. # == Quick Phrase Modifiers
  54. #
  55. # Quick phrase modifiers are also included, to allow formatting
  56. # of small portions of text within a paragraph.
  57. #
  58. # \_emphasis\_
  59. # \_\_italicized\_\_
  60. # \*strong\*
  61. # \*\*bold\*\*
  62. # ??citation??
  63. # -deleted text-
  64. # +inserted text+
  65. # ^superscript^
  66. # ~subscript~
  67. # @code@
  68. # %(classname)span%
  69. #
  70. # ==notextile== (leave text alone)
  71. #
  72. # == Links
  73. #
  74. # To make a hypertext link, put the link text in "quotation
  75. # marks" followed immediately by a colon and the URL of the link.
  76. #
  77. # Optional: text in (parentheses) following the link text,
  78. # but before the closing quotation mark, will become a Title
  79. # attribute for the link, visible as a tool tip when a cursor is above it.
  80. #
  81. # Example:
  82. #
  83. # "This is a link (This is a title) ":http://www.textism.com
  84. #
  85. # Will become:
  86. #
  87. # <a href="http://www.textism.com" title="This is a title">This is a link</a>
  88. #
  89. # == Images
  90. #
  91. # To insert an image, put the URL for the image inside exclamation marks.
  92. #
  93. # Optional: text that immediately follows the URL in (parentheses) will
  94. # be used as the Alt text for the image. Images on the web should always
  95. # have descriptive Alt text for the benefit of readers using non-graphical
  96. # browsers.
  97. #
  98. # Optional: place a colon followed by a URL immediately after the
  99. # closing ! to make the image into a link.
  100. #
  101. # Example:
  102. #
  103. # !http://www.textism.com/common/textist.gif(Textist)!
  104. #
  105. # Will become:
  106. #
  107. # <img src="http://www.textism.com/common/textist.gif" alt="Textist" />
  108. #
  109. # With a link:
  110. #
  111. # !/common/textist.gif(Textist)!:http://textism.com
  112. #
  113. # Will become:
  114. #
  115. # <a href="http://textism.com"><img src="/common/textist.gif" alt="Textist" /></a>
  116. #
  117. # == Defining Acronyms
  118. #
  119. # HTML allows authors to define acronyms via the tag. The definition appears as a
  120. # tool tip when a cursor hovers over the acronym. A crucial aid to clear writing,
  121. # this should be used at least once for each acronym in documents where they appear.
  122. #
  123. # To quickly define an acronym in Textile, place the full text in (parentheses)
  124. # immediately following the acronym.
  125. #
  126. # Example:
  127. #
  128. # ACLU(American Civil Liberties Union)
  129. #
  130. # Will become:
  131. #
  132. # <abbr title="American Civil Liberties Union">ACLU</abbr>
  133. #
  134. # == Adding Tables
  135. #
  136. # In Textile, simple tables can be added by separating each column by
  137. # a pipe.
  138. #
  139. # |a|simple|table|row|
  140. # |And|Another|table|row|
  141. #
  142. # Attributes are defined by style definitions in parentheses.
  143. #
  144. # table(border:1px solid black).
  145. # (background:#ddd;color:red). |{}| | | |
  146. #
  147. # == Using RedCloth
  148. #
  149. # RedCloth is simply an extension of the String class, which can handle
  150. # Textile formatting. Use it like a String and output HTML with its
  151. # RedCloth#to_html method.
  152. #
  153. # doc = RedCloth.new "
  154. #
  155. # h2. Test document
  156. #
  157. # Just a simple test."
  158. #
  159. # puts doc.to_html
  160. #
  161. # By default, RedCloth uses both Textile and Markdown formatting, with
  162. # Textile formatting taking precedence. If you want to turn off Markdown
  163. # formatting, to boost speed and limit the processor:
  164. #
  165. # class RedCloth::Textile.new( str )
  166. class RedCloth3 < String
  167. include Redmine::Helpers::URL
  168. VERSION = '3.0.4'
  169. DEFAULT_RULES = [:textile, :markdown]
  170. #
  171. # Two accessor for setting security restrictions.
  172. #
  173. # This is a nice thing if you're using RedCloth for
  174. # formatting in public places (e.g. Wikis) where you
  175. # don't want users to abuse HTML for bad things.
  176. #
  177. # If +:filter_html+ is set, HTML which wasn't
  178. # created by the Textile processor will be escaped.
  179. #
  180. # If +:filter_styles+ is set, it will also disable
  181. # the style markup specifier. ('{color: red}')
  182. #
  183. attr_accessor :filter_html, :filter_styles
  184. #
  185. # Accessor for toggling hard breaks.
  186. #
  187. # If +:hard_breaks+ is set, single newlines will
  188. # be converted to HTML break tags. This is the
  189. # default behavior for traditional RedCloth.
  190. #
  191. attr_accessor :hard_breaks
  192. # Accessor for toggling lite mode.
  193. #
  194. # In lite mode, block-level rules are ignored. This means
  195. # that tables, paragraphs, lists, and such aren't available.
  196. # Only the inline markup for bold, italics, entities and so on.
  197. #
  198. # r = RedCloth.new( "And then? She *fell*!", [:lite_mode] )
  199. # r.to_html
  200. # #=> "And then? She <strong>fell</strong>!"
  201. #
  202. attr_accessor :lite_mode
  203. #
  204. # Accessor for toggling span caps.
  205. #
  206. # Textile places `span' tags around capitalized
  207. # words by default, but this wreaks havoc on Wikis.
  208. # If +:no_span_caps+ is set, this will be
  209. # suppressed.
  210. #
  211. attr_accessor :no_span_caps
  212. #
  213. # Establishes the markup predence. Available rules include:
  214. #
  215. # == Textile Rules
  216. #
  217. # The following textile rules can be set individually. Or add the complete
  218. # set of rules with the single :textile rule, which supplies the rule set in
  219. # the following precedence:
  220. #
  221. # refs_textile:: Textile references (i.e. [hobix]http://hobix.com/)
  222. # block_textile_table:: Textile table block structures
  223. # block_textile_lists:: Textile list structures
  224. # block_textile_prefix:: Textile blocks with prefixes (i.e. bq., h2., etc.)
  225. # inline_textile_image:: Textile inline images
  226. # inline_textile_link:: Textile inline links
  227. # inline_textile_span:: Textile inline spans
  228. # glyphs_textile:: Textile entities (such as em-dashes and smart quotes)
  229. #
  230. # == Markdown
  231. #
  232. # refs_markdown:: Markdown references (for example: [hobix]: http://hobix.com/)
  233. # block_markdown_setext:: Markdown setext headers
  234. # block_markdown_atx:: Markdown atx headers
  235. # block_markdown_rule:: Markdown horizontal rules
  236. # block_markdown_bq:: Markdown blockquotes
  237. # block_markdown_lists:: Markdown lists
  238. # inline_markdown_link:: Markdown links
  239. attr_accessor :rules
  240. # Returns a new RedCloth object, based on _string_ and
  241. # enforcing all the included _restrictions_.
  242. #
  243. # r = RedCloth.new( "h1. A <b>bold</b> man", [:filter_html] )
  244. # r.to_html
  245. # #=>"<h1>A &lt;b&gt;bold&lt;/b&gt; man</h1>"
  246. #
  247. def initialize( string, restrictions = [] )
  248. restrictions.each { |r| method( "#{ r }=" ).call( true ) }
  249. super( string )
  250. end
  251. #
  252. # Generates HTML from the Textile contents.
  253. #
  254. # r = RedCloth.new( "And then? She *fell*!" )
  255. # r.to_html( true )
  256. # #=>"And then? She <strong>fell</strong>!"
  257. #
  258. def to_html( *rules )
  259. rules = DEFAULT_RULES if rules.empty?
  260. # make our working copy
  261. text = self.dup
  262. @urlrefs = {}
  263. @shelf = []
  264. textile_rules = [:block_textile_table, :block_textile_lists,
  265. :block_textile_prefix, :inline_textile_image, :inline_textile_code,
  266. :inline_textile_span, :inline_textile_link, :glyphs_textile]
  267. markdown_rules = [:refs_markdown, :block_markdown_setext, :block_markdown_atx, :block_markdown_rule,
  268. :block_markdown_bq, :block_markdown_lists,
  269. :inline_markdown_reflink, :inline_markdown_link]
  270. @rules = rules.collect do |rule|
  271. case rule
  272. when :markdown
  273. markdown_rules
  274. when :textile
  275. textile_rules
  276. else
  277. rule
  278. end
  279. end.flatten
  280. # standard clean up
  281. incoming_entities text
  282. clean_white_space text
  283. # start processor
  284. @pre_list = []
  285. rip_offtags text
  286. no_textile text
  287. escape_html_tags text
  288. # need to do this before #hard_break and #blocks
  289. block_textile_quotes text unless @lite_mode
  290. hard_break text
  291. unless @lite_mode
  292. refs text
  293. blocks text
  294. end
  295. inline text
  296. smooth_offtags text
  297. retrieve text
  298. text.gsub!( /<\/?notextile>/, '' )
  299. text.gsub!( /x%x%/, '&#38;' )
  300. clean_html text if filter_html
  301. text.strip!
  302. text
  303. end
  304. #######
  305. private
  306. #######
  307. #
  308. # Mapping of 8-bit ASCII codes to HTML numerical entity equivalents.
  309. # (from PyTextile)
  310. #
  311. TEXTILE_TAGS =
  312. [[128, 8364], [129, 0], [130, 8218], [131, 402], [132, 8222], [133, 8230],
  313. [134, 8224], [135, 8225], [136, 710], [137, 8240], [138, 352], [139, 8249],
  314. [140, 338], [141, 0], [142, 0], [143, 0], [144, 0], [145, 8216], [146, 8217],
  315. [147, 8220], [148, 8221], [149, 8226], [150, 8211], [151, 8212], [152, 732],
  316. [153, 8482], [154, 353], [155, 8250], [156, 339], [157, 0], [158, 0], [159, 376]].
  317. collect! do |a, b|
  318. [a.chr, ( b.zero? and "" or "&#{ b };" )]
  319. end
  320. #
  321. # Regular expressions to convert to HTML.
  322. #
  323. A_HLGN = /(?:(?:<>|<|>|\=|[()]+)+)/
  324. A_VLGN = /[\-^~]/
  325. C_CLAS = '(?:\([^")]+\))'
  326. C_LNGE = '(?:\[[a-z\-_]+\])'
  327. C_STYL = '(?:\{[^{][^"}]+\})'
  328. S_CSPN = '(?:\\\\\d+)'
  329. S_RSPN = '(?:/\d+)'
  330. A = "(?:#{A_HLGN}?#{A_VLGN}?|#{A_VLGN}?#{A_HLGN}?)"
  331. S = "(?:#{S_CSPN}?#{S_RSPN}|#{S_RSPN}?#{S_CSPN}?)"
  332. C = "(?:#{C_CLAS}?#{C_STYL}?#{C_LNGE}?|#{C_STYL}?#{C_LNGE}?#{C_CLAS}?|#{C_LNGE}?#{C_STYL}?#{C_CLAS}?)"
  333. # PUNCT = Regexp::quote( '!"#$%&\'()*+,-./:;<=>?@[\\]^_`{|}~' )
  334. PUNCT = Regexp::quote( '!"#$%&\'*+,-./:;=?@\\^_`|~' )
  335. PUNCT_NOQ = Regexp::quote( '!"#$&\',./:;=?@\\`|' )
  336. PUNCT_Q = Regexp::quote( '*-_+^~%' )
  337. HYPERLINK = '(\S+?)([^\w\s/;=\?]*?)(?=\s|<|$)'
  338. # Text markup tags, don't conflict with block tags
  339. SIMPLE_HTML_TAGS = [
  340. 'tt', 'b', 'i', 'big', 'small', 'em', 'strong', 'dfn', 'code',
  341. 'samp', 'kbd', 'var', 'cite', 'abbr', 'acronym', 'a', 'img', 'br',
  342. 'br', 'map', 'q', 'sub', 'sup', 'span', 'bdo'
  343. ]
  344. QTAGS = [
  345. ['**', 'b', :limit],
  346. ['*', 'strong', :limit],
  347. ['??', 'cite', :limit],
  348. ['-', 'del', :limit],
  349. ['__', 'i', :limit],
  350. ['_', 'em', :limit],
  351. ['%', 'span', :limit],
  352. ['+', 'ins', :limit],
  353. ['^', 'sup', :limit],
  354. ['~', 'sub', :limit]
  355. ]
  356. QTAGS_JOIN = QTAGS.map {|rc, ht, rtype| Regexp::quote rc}.join('|')
  357. QTAGS.collect! do |rc, ht, rtype|
  358. rcq = Regexp::quote rc
  359. re =
  360. case rtype
  361. when :limit
  362. /(^|[>\s\(]) # sta
  363. (?!\-\-)
  364. (#{QTAGS_JOIN}|) # oqs
  365. (#{rcq}) # qtag
  366. ([[:word:]]|[^\s].*?[^\s]) # content
  367. (?!\-\-)
  368. #{rcq}
  369. (#{QTAGS_JOIN}|) # oqa
  370. (?=[[:punct:]]|<|\s|\)|$)/x
  371. else
  372. /(#{rcq})
  373. (#{C})
  374. (?::(\S+))?
  375. ([[:word:]]|[^\s\-].*?[^\s\-])
  376. #{rcq}/xm
  377. end
  378. [rc, ht, re, rtype]
  379. end
  380. # Elements to handle
  381. GLYPHS = [
  382. # [ /([^\s\[{(>])?\'([dmst]\b|ll\b|ve\b|\s|:|$)/, '\1&#8217;\2' ], # single closing
  383. # [ /([^\s\[{(>#{PUNCT_Q}][#{PUNCT_Q}]*)\'/, '\1&#8217;' ], # single closing
  384. # [ /\'(?=[#{PUNCT_Q}]*(s\b|[\s#{PUNCT_NOQ}]))/, '&#8217;' ], # single closing
  385. # [ /\'/, '&#8216;' ], # single opening
  386. # [ /</, '&lt;' ], # less-than
  387. # [ />/, '&gt;' ], # greater-than
  388. # [ /([^\s\[{(])?"(\s|:|$)/, '\1&#8221;\2' ], # double closing
  389. # [ /([^\s\[{(>#{PUNCT_Q}][#{PUNCT_Q}]*)"/, '\1&#8221;' ], # double closing
  390. # [ /"(?=[#{PUNCT_Q}]*[\s#{PUNCT_NOQ}])/, '&#8221;' ], # double closing
  391. # [ /"/, '&#8220;' ], # double opening
  392. # [ /\b( )?\.{3}/, '\1&#8230;' ], # ellipsis
  393. # [ /\b([A-Z][A-Z0-9]{2,})\b(?:[(]([^)]*)[)])/, '<acronym title="\2">\1</acronym>' ], # 3+ uppercase acronym
  394. # [ /(^|[^"][>\s])([A-Z][A-Z0-9 ]+[A-Z0-9])([^<A-Za-z0-9]|$)/, '\1<span class="caps">\2</span>\3', :no_span_caps ], # 3+ uppercase caps
  395. # [ /(\.\s)?\s?--\s?/, '\1&#8212;' ], # em dash
  396. # [ /\s->\s/, ' &rarr; ' ], # right arrow
  397. # [ /\s-\s/, ' &#8211; ' ], # en dash
  398. # [ /(\d+) ?x ?(\d+)/, '\1&#215;\2' ], # dimension sign
  399. # [ /\b ?[(\[]TM[\])]/i, '&#8482;' ], # trademark
  400. # [ /\b ?[(\[]R[\])]/i, '&#174;' ], # registered
  401. # [ /\b ?[(\[]C[\])]/i, '&#169;' ] # copyright
  402. ]
  403. H_ALGN_VALS = {
  404. '<' => 'left',
  405. '=' => 'center',
  406. '>' => 'right',
  407. '<>' => 'justify'
  408. }
  409. V_ALGN_VALS = {
  410. '^' => 'top',
  411. '-' => 'middle',
  412. '~' => 'bottom'
  413. }
  414. #
  415. # Flexible HTML escaping
  416. #
  417. def htmlesc( str, mode=:Quotes )
  418. if str
  419. str.gsub!( '&', '&amp;' )
  420. str.gsub!( '"', '&quot;' ) if mode != :NoQuotes
  421. str.gsub!( "'", '&#039;' ) if mode == :Quotes
  422. str.gsub!( '<', '&lt;')
  423. str.gsub!( '>', '&gt;')
  424. end
  425. str
  426. end
  427. # Search and replace for Textile glyphs (quotes, dashes, other symbols)
  428. def pgl( text )
  429. #GLYPHS.each do |re, resub, tog|
  430. # next if tog and method( tog ).call
  431. # text.gsub! re, resub
  432. #end
  433. text.gsub!(/\b([A-Z][A-Z0-9]{1,})\b(?:[(]([^)]*)[)])/) do |m|
  434. "<abbr title=\"#{htmlesc $2}\">#{$1}</abbr>"
  435. end
  436. end
  437. # Parses Textile attribute lists and builds an HTML attribute string
  438. def pba( text_in, element = "" )
  439. return +'' unless text_in
  440. style = []
  441. text = text_in.dup
  442. if element == 'td'
  443. colspan = $1 if text =~ /\\(\d+)/
  444. rowspan = $1 if text =~ /\/(\d+)/
  445. style << "vertical-align:#{ v_align( $& ) };" if text =~ A_VLGN
  446. end
  447. if text.sub!( /\{([^"}]*)\}/, '' ) && !filter_styles
  448. sanitized = sanitize_styles($1)
  449. style << "#{ sanitized };" unless sanitized.blank?
  450. end
  451. lang = $1 if
  452. text.sub!( /\[([a-z\-_]+?)\]/, '' )
  453. cls = $1 if
  454. text.sub!( /\(([^()]+?)\)/, '' )
  455. style << "padding-left:#{ $1.length }em;" if
  456. text.sub!( /([(]+)/, '' )
  457. style << "padding-right:#{ $1.length }em;" if text.sub!( /([)]+)/, '' )
  458. style << "text-align:#{ h_align( $& ) };" if text =~ A_HLGN
  459. cls, id = $1, $2 if cls =~ /^(.*?)#(.*)$/
  460. # add wiki-class- and wiki-id- to classes and ids to prevent setting of
  461. # arbitrary classes and ids
  462. cls = cls.split(/\s+/).map do |c|
  463. c.starts_with?('wiki-class-') ? c : "wiki-class-#{c}"
  464. end.join(' ') if cls
  465. id = id.starts_with?('wiki-id-') ? id : "wiki-id-#{id}" if id
  466. atts = +''
  467. atts << " style=\"#{ style.join }\"" unless style.empty?
  468. atts << " class=\"#{ cls }\"" unless cls.to_s.empty?
  469. atts << " lang=\"#{ lang }\"" if lang
  470. atts << " id=\"#{ id }\"" if id
  471. atts << " colspan=\"#{ colspan }\"" if colspan
  472. atts << " rowspan=\"#{ rowspan }\"" if rowspan
  473. atts
  474. end
  475. STYLES_RE = /^(color|(min-|max-)?+(width|height)|border|background|padding|margin|font|text|float)(-[a-z]+)*:\s*((\d+%?|\d+px|\d+(\.\d+)?em|#[0-9a-f]+|[a-z]+)\s*)+$/i
  476. def sanitize_styles(str)
  477. styles = str.split(";").map(&:strip)
  478. styles.reject! do |style|
  479. !style.match(STYLES_RE)
  480. end
  481. styles.join(";")
  482. end
  483. TABLE_RE = /^(?:table(_?#{S}#{A}#{C})\. ?\n)?^(#{A}#{C}\.? ?\|.*?\|)(\n\n|\Z)/m
  484. # Parses a Textile table block, building HTML from the result.
  485. def block_textile_table( text )
  486. text.gsub!( TABLE_RE ) do |matches|
  487. tatts, fullrow = $~[1..2]
  488. tatts = pba( tatts, 'table' )
  489. tatts = shelve( tatts ) if tatts
  490. rows = []
  491. fullrow.gsub!(/([^|\s])\s*\n/, "\\1<br />")
  492. fullrow.each_line do |row|
  493. ratts, row = pba( $1, 'tr' ), $2 if row =~ /^(#{A}#{C}\. )(.*)/m
  494. cells = []
  495. # the regexp prevents wiki links with a | from being cut as cells
  496. row.scan(/\|(_?#{S}#{A}#{C}\. ?)?((\[\[[^|\]]*\|[^|\]]*\]\]|[^|])*?)(?=\|)/) do |modifiers, cell|
  497. ctyp = 'd'
  498. ctyp = 'h' if modifiers && modifiers =~ /^_/
  499. catts = nil
  500. catts = pba( modifiers, 'td' ) if modifiers
  501. catts = shelve( catts ) if catts
  502. cells << "\t\t\t<t#{ ctyp }#{ catts }>#{ cell }</t#{ ctyp }>"
  503. end
  504. ratts = shelve( ratts ) if ratts
  505. rows << "\t\t<tr#{ ratts }>\n#{ cells.join( "\n" ) }\n\t\t</tr>"
  506. end
  507. "\t<table#{ tatts }>\n#{ rows.join( "\n" ) }\n\t</table>\n\n"
  508. end
  509. end
  510. LISTS_RE = /^([#*]+?#{C} .*?)$(?![^#*])/m
  511. LISTS_CONTENT_RE = /^([#*]+)(#{A}#{C}) (.*)$/m
  512. # Parses Textile lists and generates HTML
  513. def block_textile_lists( text )
  514. text.gsub!( LISTS_RE ) do |match|
  515. lines = match.split( /\n/ )
  516. last_line = -1
  517. depth = []
  518. lines.each_with_index do |line, line_id|
  519. if line =~ LISTS_CONTENT_RE
  520. tl,atts,content = $~[1..3]
  521. if depth.last
  522. if depth.last.length > tl.length
  523. (depth.length - 1).downto(0) do |i|
  524. break if depth[i].length == tl.length
  525. lines[line_id - 1] << "</li>\n\t</#{ lT( depth[i] ) }l>\n\t"
  526. depth.pop
  527. end
  528. end
  529. if depth.last and depth.last.length == tl.length
  530. lines[line_id - 1] << '</li>'
  531. end
  532. end
  533. unless depth.last == tl
  534. depth << tl
  535. atts = pba( atts )
  536. atts = shelve( atts ) if atts
  537. lines[line_id] = +"\t<#{ lT(tl) }l#{ atts }>\n\t<li>#{ content }"
  538. else
  539. lines[line_id] = +"\t\t<li>#{ content }"
  540. end
  541. last_line = line_id
  542. else
  543. last_line = line_id
  544. end
  545. if line_id - last_line > 1 or line_id == lines.length - 1
  546. while v = depth.pop
  547. lines[last_line] << "</li>\n\t</#{ lT( v ) }l>"
  548. end
  549. end
  550. end
  551. lines.join( "\n" )
  552. end
  553. end
  554. QUOTES_RE = /(^>+([^\n]*?)(\n|$))+/m
  555. QUOTES_CONTENT_RE = /^([> ]+)(.*)$/m
  556. def block_textile_quotes( text )
  557. text.gsub!( QUOTES_RE ) do |match|
  558. lines = match.split( /\n/ )
  559. quotes = +''
  560. indent = 0
  561. lines.each do |line|
  562. line =~ QUOTES_CONTENT_RE
  563. bq,content = $1, $2
  564. l = bq.count('>')
  565. if l != indent
  566. quotes << ("\n\n" + (l>indent ? '<blockquote>' * (l-indent) : '</blockquote>' * (indent-l)) + "\n\n")
  567. indent = l
  568. end
  569. quotes << (content + "\n")
  570. end
  571. quotes << ("\n" + '</blockquote>' * indent + "\n\n")
  572. quotes
  573. end
  574. end
  575. CODE_RE = /(\W)
  576. @
  577. (?:\|(\w+?)\|)?
  578. (.+?)
  579. @
  580. (?=\W)/x
  581. def inline_textile_code( text )
  582. text.gsub!( CODE_RE ) do |m|
  583. before,lang,code,after = $~[1..4]
  584. lang = " lang=\"#{ lang }\"" if lang
  585. rip_offtags( +"#{ before }<code#{ lang }>#{ code }</code>#{ after }", false )
  586. end
  587. end
  588. def lT( text )
  589. text =~ /\#$/ ? 'o' : 'u'
  590. end
  591. def hard_break( text )
  592. text.gsub!( /(.)\n(?!\Z| *([#*=]+(\s|$)|[{|]))/, "\\1<br />" ) if hard_breaks
  593. end
  594. BLOCKS_GROUP_RE = /\n{2,}(?! )/m
  595. def blocks( text, deep_code = false )
  596. text.replace( text.split( BLOCKS_GROUP_RE ).collect do |blk|
  597. plain = blk !~ /\A[#*> ]/
  598. # skip blocks that are complex HTML
  599. if blk =~ /^<\/?(\w+).*>/ and not SIMPLE_HTML_TAGS.include? $1
  600. blk
  601. else
  602. # search for indentation levels
  603. blk.strip!
  604. if blk.empty?
  605. blk
  606. else
  607. code_blk = nil
  608. blk.gsub!( /((?:\n(?:\n^ +[^\n]*)+)+)/m ) do |iblk|
  609. flush_left iblk
  610. blocks iblk, plain
  611. iblk.gsub( /^(\S)/, "\t\\1" )
  612. if plain
  613. code_blk = iblk; ""
  614. else
  615. iblk
  616. end
  617. end
  618. block_applied = 0
  619. @rules.each do |rule_name|
  620. block_applied += 1 if ( rule_name.to_s.match /^block_/ and method( rule_name ).call( blk ) )
  621. end
  622. if block_applied.zero?
  623. if deep_code
  624. blk = "\t<pre><code>#{ blk }</code></pre>"
  625. else
  626. blk = "\t<p>#{ blk }</p>"
  627. end
  628. end
  629. # hard_break blk
  630. blk + "\n#{ code_blk }"
  631. end
  632. end
  633. end.join( "\n\n" ) )
  634. end
  635. def textile_bq( tag, atts, cite, content )
  636. cite, cite_title = check_refs( cite )
  637. cite = " cite=\"#{ cite }\"" if cite
  638. atts = shelve( atts ) if atts
  639. "\t<blockquote#{ cite }>\n\t\t<p#{ atts }>#{ content }</p>\n\t</blockquote>"
  640. end
  641. def textile_p( tag, atts, cite, content )
  642. atts = shelve( atts ) if atts
  643. "\t<#{ tag }#{ atts }>#{ content }</#{ tag }>"
  644. end
  645. alias textile_h1 textile_p
  646. alias textile_h2 textile_p
  647. alias textile_h3 textile_p
  648. alias textile_h4 textile_p
  649. alias textile_h5 textile_p
  650. alias textile_h6 textile_p
  651. def textile_fn_( tag, num, atts, cite, content )
  652. atts << " id=\"fn#{ num }\" class=\"footnote\""
  653. content = "<sup>#{ num }</sup> #{ content }"
  654. atts = shelve( atts ) if atts
  655. "\t<p#{ atts }>#{ content }</p>"
  656. end
  657. BLOCK_RE = /^(([a-z]+)(\d*))(#{A}#{C})\.(?::(\S+))? (.*)$/m
  658. def block_textile_prefix( text )
  659. if text =~ BLOCK_RE
  660. tag,tagpre,num,atts,cite,content = $~[1..6]
  661. atts = pba( atts )
  662. # pass to prefix handler
  663. replacement = nil
  664. if respond_to? "textile_#{ tag }", true
  665. replacement = method( "textile_#{ tag }" ).call( tag, atts, cite, content )
  666. elsif respond_to? "textile_#{ tagpre }_", true
  667. replacement = method( "textile_#{ tagpre }_" ).call( tagpre, num, atts, cite, content )
  668. end
  669. text.gsub!( $& ) { replacement } if replacement
  670. end
  671. end
  672. SETEXT_RE = /\A(.+?)\n([=-])[=-]* *$/m
  673. def block_markdown_setext( text )
  674. if text =~ SETEXT_RE
  675. tag = if $2 == "="; "h1"; else; "h2"; end
  676. blk, cont = "<#{ tag }>#{ $1 }</#{ tag }>", $'
  677. blocks cont
  678. text.replace( blk + cont )
  679. end
  680. end
  681. ATX_RE = /\A(\#{1,6}) # $1 = string of #'s
  682. [ ]*
  683. (.+?) # $2 = Header text
  684. [ ]*
  685. \#* # optional closing #'s (not counted)
  686. $/x
  687. def block_markdown_atx( text )
  688. if text =~ ATX_RE
  689. tag = "h#{ $1.length }"
  690. blk, cont = "<#{ tag }>#{ $2 }</#{ tag }>\n\n", $'
  691. blocks cont
  692. text.replace( blk + cont )
  693. end
  694. end
  695. MARKDOWN_BQ_RE = /\A(^ *> ?.+$(.+\n)*\n*)+/m
  696. def block_markdown_bq( text )
  697. text.gsub!( MARKDOWN_BQ_RE ) do |blk|
  698. blk.gsub!( /^ *> ?/, '' )
  699. flush_left blk
  700. blocks blk
  701. blk.gsub!( /^(\S)/, "\t\\1" )
  702. "<blockquote>\n#{ blk }\n</blockquote>\n\n"
  703. end
  704. end
  705. MARKDOWN_RULE_RE = /^(#{
  706. ['*', '-', '_'].collect { |ch| ' ?(' + Regexp::quote( ch ) + ' ?){3,}' }.join( '|' )
  707. })$/
  708. def block_markdown_rule( text )
  709. text.gsub!( MARKDOWN_RULE_RE ) do |blk|
  710. "<hr />"
  711. end
  712. end
  713. # XXX TODO XXX
  714. def block_markdown_lists( text )
  715. end
  716. def inline_textile_span( text )
  717. QTAGS.each do |qtag_rc, ht, qtag_re, rtype|
  718. text.gsub!( qtag_re ) do |m|
  719. case rtype
  720. when :limit
  721. sta,oqs,qtag,content,oqa = $~[1..6]
  722. atts = nil
  723. if content =~ /^(#{C})(.+)$/
  724. atts, content = $~[1..2]
  725. end
  726. else
  727. qtag,atts,cite,content = $~[1..4]
  728. sta = ''
  729. end
  730. atts = pba( atts )
  731. atts = shelve( atts ) if atts
  732. "#{ sta }#{ oqs }<#{ ht }#{ atts }>#{ content }</#{ ht }>#{ oqa }"
  733. end
  734. end
  735. end
  736. LINK_RE = /
  737. (
  738. ([\s\[{(]|[#{PUNCT}])? # $pre
  739. " # start
  740. (#{C}) # $atts
  741. ([^"\n]+?) # $text
  742. \s?
  743. (?:\(([^)]+?)\)(?="))? # $title
  744. ":
  745. ( # $url
  746. (\/|[a-zA-Z]+:\/\/|www\.|mailto:) # $proto
  747. [[:alnum:]_\/]\S+?
  748. )
  749. (\/)? # $slash
  750. ([^[:alnum:]_\=\/;\(\)\-]*?) # $post
  751. )
  752. (?=<|\s|$)
  753. /x
  754. #"
  755. def inline_textile_link( text )
  756. text.gsub!( LINK_RE ) do |m|
  757. all,pre,atts,text,title,url,proto,slash,post = $~[1..9]
  758. if text.include?('<br />')
  759. all
  760. else
  761. url, url_title = check_refs( url )
  762. title ||= url_title
  763. # Idea below : an URL with unbalanced parethesis and
  764. # ending by ')' is put into external parenthesis
  765. if ( url[-1]==?) and ((url.count("(") - url.count(")")) < 0 ) )
  766. url=url[0..-2] # discard closing parenth from url
  767. post = ")"+post # add closing parenth to post
  768. end
  769. atts = pba( atts )
  770. atts = +" href=\"#{ htmlesc url }#{ slash }\"#{ atts }"
  771. atts << " title=\"#{ htmlesc title }\"" if title
  772. atts = shelve( atts ) if atts
  773. external = (url =~ /^https?:\/\//) ? ' class="external"' : ''
  774. "#{ pre }<a#{ atts }#{ external }>#{ text }</a>#{ post }"
  775. end
  776. end
  777. end
  778. MARKDOWN_REFLINK_RE = /
  779. \[([^\[\]]+)\] # $text
  780. [ ]? # opt. space
  781. (?:\n[ ]*)? # one optional newline followed by spaces
  782. \[(.*?)\] # $id
  783. /x
  784. def inline_markdown_reflink( text )
  785. text.gsub!( MARKDOWN_REFLINK_RE ) do |m|
  786. text, id = $~[1..2]
  787. if id.empty?
  788. url, title = check_refs( text )
  789. else
  790. url, title = check_refs( id )
  791. end
  792. atts = " href=\"#{ url }\""
  793. atts << " title=\"#{ title }\"" if title
  794. atts = shelve( atts )
  795. "<a#{ atts }>#{ text }</a>"
  796. end
  797. end
  798. MARKDOWN_LINK_RE = /
  799. \[([^\[\]]+)\] # $text
  800. \( # open paren
  801. [ \t]* # opt space
  802. <?(.+?)>? # $href
  803. [ \t]* # opt space
  804. (?: # whole title
  805. (['"]) # $quote
  806. (.*?) # $title
  807. \3 # matching quote
  808. )? # title is optional
  809. \)
  810. /x
  811. def inline_markdown_link( text )
  812. text.gsub!( MARKDOWN_LINK_RE ) do |m|
  813. text, url, quote, title = $~[1..4]
  814. atts = " href=\"#{ url }\""
  815. atts << " title=\"#{ title }\"" if title
  816. atts = shelve( atts )
  817. "<a#{ atts }>#{ text }</a>"
  818. end
  819. end
  820. TEXTILE_REFS_RE = /(^ *)\[([^\[\n]+?)\](#{HYPERLINK})(?=\s|$)/
  821. MARKDOWN_REFS_RE = /(^ *)\[([^\n]+?)\]:\s+<?(#{HYPERLINK})>?(?:\s+"((?:[^"]|\\")+)")?(?=\s|$)/m
  822. def refs( text )
  823. @rules.each do |rule_name|
  824. method( rule_name ).call( text ) if rule_name.to_s.match /^refs_/
  825. end
  826. end
  827. def refs_textile( text )
  828. text.gsub!( TEXTILE_REFS_RE ) do |m|
  829. flag, url = $~[2..3]
  830. @urlrefs[flag.downcase] = [url, nil]
  831. nil
  832. end
  833. end
  834. def refs_markdown( text )
  835. text.gsub!( MARKDOWN_REFS_RE ) do |m|
  836. flag, url = $~[2..3]
  837. title = $~[6]
  838. @urlrefs[flag.downcase] = [url, title]
  839. nil
  840. end
  841. end
  842. def check_refs( text )
  843. ret = @urlrefs[text.downcase] if text
  844. ret || [text, nil]
  845. end
  846. IMAGE_RE = /
  847. (>|\s|^) # start of line?
  848. \! # opening
  849. (\<|\=|\>)? # optional alignment atts
  850. (#{C}) # optional style,class atts
  851. (?:\. )? # optional dot-space
  852. ([^\s(!]+?) # presume this is the src
  853. \s? # optional space
  854. (?:\(((?:[^\(\)]|\([^\)]+\))+?)\))? # optional title
  855. \! # closing
  856. (?::#{ HYPERLINK })? # optional href
  857. /x
  858. def inline_textile_image( text )
  859. text.gsub!( IMAGE_RE ) do |m|
  860. stln,algn,atts,url,title,href,href_a1,href_a2 = $~[1..8]
  861. htmlesc title
  862. atts = pba( atts )
  863. atts = +" src=\"#{ htmlesc url.dup }\"#{ atts }"
  864. atts << " title=\"#{ title }\"" if title
  865. atts << " alt=\"#{ title }\""
  866. # size = @getimagesize($url);
  867. # if($size) $atts.= " $size[3]";
  868. href, alt_title = check_refs( href ) if href
  869. url, url_title = check_refs( url )
  870. next m unless uri_with_safe_scheme?(url)
  871. out = +''
  872. out << "<a#{ shelve( " href=\"#{ href }\"" ) }>" if href
  873. out << "<img#{ shelve( atts ) } />"
  874. out << "</a>#{ href_a1 }#{ href_a2 }" if href
  875. if algn
  876. algn = h_align( algn )
  877. if stln == "<p>"
  878. out = "<p style=\"float:#{ algn }\">#{ out }"
  879. else
  880. out = "#{ stln }<span style=\"float:#{ algn }\">#{ out }</span>"
  881. end
  882. else
  883. out = stln + out
  884. end
  885. out
  886. end
  887. end
  888. def shelve( val )
  889. @shelf << val
  890. " :redsh##{ @shelf.length }:"
  891. end
  892. def retrieve( text )
  893. text.gsub!(/ :redsh#(\d+):/) do
  894. @shelf[$1.to_i - 1] || $&
  895. end
  896. end
  897. def incoming_entities( text )
  898. ## turn any incoming ampersands into a dummy character for now.
  899. ## This uses a negative lookahead for alphanumerics followed by a semicolon,
  900. ## implying an incoming html entity, to be skipped
  901. text.gsub!( /&(?![#a-z0-9]+;)/i, "x%x%" )
  902. end
  903. def no_textile( text )
  904. text.gsub!( /(^|\s)==([^=]+.*?)==(\s|$)?/,
  905. '\1<notextile>\2</notextile>\3' )
  906. text.gsub!( /^ *==([^=]+.*?)==/m,
  907. '\1<notextile>\2</notextile>\3' )
  908. end
  909. def clean_white_space( text )
  910. # normalize line breaks
  911. text.gsub!( /\r\n/, "\n" )
  912. text.gsub!( /\r/, "\n" )
  913. text.gsub!( /\t/, ' ' )
  914. text.gsub!( /^ +$/, '' )
  915. text.gsub!( /\n{3,}/, "\n\n" )
  916. text.gsub!( /"$/, "\" " )
  917. # if entire document is indented, flush
  918. # to the left side
  919. flush_left text
  920. end
  921. def flush_left( text )
  922. indt = 0
  923. if text =~ /^ /
  924. while text !~ /^ {#{indt}}[^ ]/
  925. indt += 1
  926. end unless text.empty?
  927. if indt.nonzero?
  928. text.gsub!( /^ {#{indt}}/, '' )
  929. end
  930. end
  931. end
  932. def footnote_ref( text )
  933. text.gsub!( /\b\[([0-9]+?)\](\s)?/,
  934. '<sup><a href="#fn\1">\1</a></sup>\2' )
  935. end
  936. OFFTAGS = /(code|pre|kbd|notextile)/
  937. OFFTAG_MATCH = /(?:(<\/#{ OFFTAGS }\b>)|(<#{ OFFTAGS }\b[^>]*>))(.*?)(?=<\/?#{ OFFTAGS }\b\W|\Z)/mi
  938. OFFTAG_OPEN = /<#{ OFFTAGS }/
  939. OFFTAG_CLOSE = /<\/?#{ OFFTAGS }/
  940. HASTAG_MATCH = /(<\/?\w[^\n]*?>)/m
  941. ALLTAG_MATCH = /(<\/?\w[^\n]*?>)|.*?(?=<\/?\w[^\n]*?>|$)/m
  942. def glyphs_textile( text, level = 0 )
  943. if text !~ HASTAG_MATCH
  944. pgl text
  945. footnote_ref text
  946. else
  947. codepre = 0
  948. text.gsub!( ALLTAG_MATCH ) do |line|
  949. ## matches are off if we're between <code>, <pre> etc.
  950. if $1
  951. if line =~ OFFTAG_OPEN
  952. codepre += 1
  953. elsif line =~ OFFTAG_CLOSE
  954. codepre -= 1
  955. codepre = 0 if codepre < 0
  956. end
  957. elsif codepre.zero?
  958. glyphs_textile( line, level + 1 )
  959. else
  960. htmlesc( line, :NoQuotes )
  961. end
  962. # p [level, codepre, line]
  963. line
  964. end
  965. end
  966. end
  967. def rip_offtags( text, escape_aftertag=true, escape_line=true )
  968. if text =~ /<.*>/
  969. ## strip and encode <pre> content
  970. codepre, used_offtags = 0, {}
  971. text.gsub!( OFFTAG_MATCH ) do |line|
  972. if $3
  973. first, offtag, aftertag = $3, $4, $5
  974. codepre += 1
  975. used_offtags[offtag] = true
  976. if codepre - used_offtags.length > 0
  977. htmlesc( line, :NoQuotes ) if escape_line
  978. @pre_list.last << line
  979. line = +""
  980. else
  981. ### htmlesc is disabled between CODE tags which will be parsed with highlighter
  982. ### Regexp in formatter.rb is : /<code\s+class="(\w+)">\s?(.+)/m
  983. ### NB: some changes were made not to use $N variables, because we use "match"
  984. ### and it breaks following lines
  985. htmlesc( aftertag, :NoQuotes ) if aftertag && escape_aftertag && !first.match(/<code\s+class="(\w+)">/)
  986. line = +"<redpre##{ @pre_list.length }>"
  987. first.match(/<#{ OFFTAGS }([^>]*)>/)
  988. tag = $1
  989. $2.to_s.match(/(class\=("[^"]+"|'[^']+'))/i)
  990. tag << " #{$1}" if $1 && tag == 'code'
  991. @pre_list << +"<#{ tag }>#{ aftertag }"
  992. end
  993. elsif $1 and codepre > 0
  994. if codepre - used_offtags.length > 0
  995. htmlesc( line, :NoQuotes ) if escape_line
  996. @pre_list.last << line
  997. line = +""
  998. end
  999. codepre -= 1 unless codepre.zero?
  1000. used_offtags = {} if codepre.zero?
  1001. end
  1002. line
  1003. end
  1004. end
  1005. text
  1006. end
  1007. def smooth_offtags( text )
  1008. unless @pre_list.empty?
  1009. ## replace <pre> content
  1010. text.gsub!( /<redpre#(\d+)>/ ) { @pre_list[$1.to_i] }
  1011. end
  1012. end
  1013. def inline( text )
  1014. [/^inline_/, /^glyphs_/].each do |meth_re|
  1015. @rules.each do |rule_name|
  1016. method( rule_name ).call( text ) if rule_name.to_s.match( meth_re )
  1017. end
  1018. end
  1019. end
  1020. def h_align( text )
  1021. H_ALGN_VALS[text]
  1022. end
  1023. def v_align( text )
  1024. V_ALGN_VALS[text]
  1025. end
  1026. def textile_popup_help( name, windowW, windowH )
  1027. ' <a target="_blank" href="http://hobix.com/textile/#' + helpvar + '" onclick="window.open(this.href, \'popupwindow\', \'width=' + windowW + ',height=' + windowH + ',scrollbars,resizable\'); return false;">' + name + '</a><br />'
  1028. end
  1029. # HTML cleansing stuff
  1030. BASIC_TAGS = {
  1031. 'a' => ['href', 'title'],
  1032. 'img' => ['src', 'alt', 'title'],
  1033. 'br' => [],
  1034. 'i' => nil,
  1035. 'u' => nil,
  1036. 'b' => nil,
  1037. 'pre' => nil,
  1038. 'kbd' => nil,
  1039. 'code' => ['lang'],
  1040. 'cite' => nil,
  1041. 'strong' => nil,
  1042. 'em' => nil,
  1043. 'ins' => nil,
  1044. 'sup' => nil,
  1045. 'sub' => nil,
  1046. 'del' => nil,
  1047. 'table' => nil,
  1048. 'tr' => nil,
  1049. 'td' => ['colspan', 'rowspan'],
  1050. 'th' => nil,
  1051. 'ol' => nil,
  1052. 'ul' => nil,
  1053. 'li' => nil,
  1054. 'p' => nil,
  1055. 'h1' => nil,
  1056. 'h2' => nil,
  1057. 'h3' => nil,
  1058. 'h4' => nil,
  1059. 'h5' => nil,
  1060. 'h6' => nil,
  1061. 'blockquote' => ['cite']
  1062. }
  1063. def clean_html( text, tags = BASIC_TAGS )
  1064. text.gsub!( /<!\[CDATA\[/, '' )
  1065. text.gsub!( /<(\/*)(\w+)([^>]*)>/ ) do
  1066. raw = $~
  1067. tag = raw[2].downcase
  1068. if tags.has_key? tag
  1069. pcs = [tag]
  1070. tags[tag].each do |prop|
  1071. ['"', "'", ''].each do |q|
  1072. q2 = ( q != '' ? q : '\s' )
  1073. if raw[3] =~ /#{prop}\s*=\s*#{q}([^#{q2}]+)#{q}/i
  1074. attrv = $1
  1075. next if prop == 'src' and attrv =~ %r{^(?!http)\w+:}
  1076. pcs << "#{prop}=\"#{$1.gsub('"', '\\"')}\""
  1077. break
  1078. end
  1079. end
  1080. end if tags[tag]
  1081. "<#{raw[1]}#{pcs.join " "}>"
  1082. else
  1083. " "
  1084. end
  1085. end
  1086. end
  1087. ALLOWED_TAGS = %w(redpre pre code kbd notextile)
  1088. def escape_html_tags(text)
  1089. text.gsub!(%r{<(\/?([!\w]+)[^<>\n]*)(>?)}) do |m|
  1090. if ALLOWED_TAGS.include?($2) && $3.present?
  1091. "<#{$1}#{$3}"
  1092. else
  1093. "&lt;#{$1}#{'&gt;' unless $3.blank?}"
  1094. end
  1095. end
  1096. end
  1097. end