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

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220
  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. private
  305. #
  306. # Mapping of 8-bit ASCII codes to HTML numerical entity equivalents.
  307. # (from PyTextile)
  308. #
  309. TEXTILE_TAGS =
  310. [[128, 8364], [129, 0], [130, 8218], [131, 402], [132, 8222], [133, 8230],
  311. [134, 8224], [135, 8225], [136, 710], [137, 8240], [138, 352], [139, 8249],
  312. [140, 338], [141, 0], [142, 0], [143, 0], [144, 0], [145, 8216], [146, 8217],
  313. [147, 8220], [148, 8221], [149, 8226], [150, 8211], [151, 8212], [152, 732],
  314. [153, 8482], [154, 353], [155, 8250], [156, 339], [157, 0], [158, 0], [159, 376]].
  315. collect! do |a, b|
  316. [a.chr, ( b.zero? and "" or "&#{b};" )]
  317. end
  318. #
  319. # Regular expressions to convert to HTML.
  320. #
  321. A_HLGN = /(?:(?:<>|<|>|\=|[()]+)+)/
  322. A_VLGN = /[\-^~]/
  323. C_CLAS = '(?:\([^")]+\))'
  324. C_LNGE = '(?:\[[a-z\-_]+\])'
  325. C_STYL = '(?:\{[^{][^"}]+\})'
  326. S_CSPN = '(?:\\\\\d+)'
  327. S_RSPN = '(?:/\d+)'
  328. A = "(?:#{A_HLGN}?#{A_VLGN}?|#{A_VLGN}?#{A_HLGN}?)"
  329. S = "(?:#{S_CSPN}?#{S_RSPN}|#{S_RSPN}?#{S_CSPN}?)"
  330. C = "(?:#{C_CLAS}?#{C_STYL}?#{C_LNGE}?|#{C_STYL}?#{C_LNGE}?#{C_CLAS}?|#{C_LNGE}?#{C_STYL}?#{C_CLAS}?)"
  331. # PUNCT = Regexp::quote( '!"#$%&\'()*+,-./:;<=>?@[\\]^_`{|}~' )
  332. PUNCT = Regexp::quote( '!"#$%&\'*+,-./:;=?@\\^_`|~' )
  333. PUNCT_NOQ = Regexp::quote( '!"#$&\',./:;=?@\\`|' )
  334. PUNCT_Q = Regexp::quote( '*-_+^~%' )
  335. HYPERLINK = '(\S+?)([^\w\s/;=\?]*?)(?=\s|<|$)'
  336. # Text markup tags, don't conflict with block tags
  337. SIMPLE_HTML_TAGS = [
  338. 'tt', 'b', 'i', 'big', 'small', 'em', 'strong', 'dfn', 'code',
  339. 'samp', 'kbd', 'var', 'cite', 'abbr', 'acronym', 'a', 'img', 'br',
  340. 'br', 'map', 'q', 'sub', 'sup', 'span', 'bdo'
  341. ]
  342. QTAGS = [
  343. ['**', 'b', :limit],
  344. ['*', 'strong', :limit],
  345. ['??', 'cite', :limit],
  346. ['-', 'del', :limit],
  347. ['__', 'i', :limit],
  348. ['_', 'em', :limit],
  349. ['%', 'span', :limit],
  350. ['+', 'ins', :limit],
  351. ['^', 'sup', :limit],
  352. ['~', 'sub', :limit]
  353. ]
  354. QTAGS_JOIN = QTAGS.map {|rc, ht, rtype| Regexp::quote rc}.join('|')
  355. QTAGS.collect! do |rc, ht, rtype|
  356. rcq = Regexp::quote rc
  357. re =
  358. case rtype
  359. when :limit
  360. /(^|[>\s\(]) # sta
  361. (?!\-\-)
  362. (#{QTAGS_JOIN}|) # oqs
  363. (#{rcq}) # qtag
  364. ([[:word:]]|[^\s].*?[^\s]) # content
  365. (?!\-\-)
  366. #{rcq}
  367. (#{QTAGS_JOIN}|) # oqa
  368. (?=[[:punct:]]|<|\s|\)|$)/x
  369. else
  370. /(#{rcq})
  371. (#{C})
  372. (?::(\S+))?
  373. ([[:word:]]|[^\s\-].*?[^\s\-])
  374. #{rcq}/xm
  375. end
  376. [rc, ht, re, rtype]
  377. end
  378. # Elements to handle
  379. GLYPHS = [
  380. # [ /([^\s\[{(>])?\'([dmst]\b|ll\b|ve\b|\s|:|$)/, '\1&#8217;\2' ], # single closing
  381. # [ /([^\s\[{(>#{PUNCT_Q}][#{PUNCT_Q}]*)\'/, '\1&#8217;' ], # single closing
  382. # [ /\'(?=[#{PUNCT_Q}]*(s\b|[\s#{PUNCT_NOQ}]))/, '&#8217;' ], # single closing
  383. # [ /\'/, '&#8216;' ], # single opening
  384. # [ /</, '&lt;' ], # less-than
  385. # [ />/, '&gt;' ], # greater-than
  386. # [ /([^\s\[{(])?"(\s|:|$)/, '\1&#8221;\2' ], # double closing
  387. # [ /([^\s\[{(>#{PUNCT_Q}][#{PUNCT_Q}]*)"/, '\1&#8221;' ], # double closing
  388. # [ /"(?=[#{PUNCT_Q}]*[\s#{PUNCT_NOQ}])/, '&#8221;' ], # double closing
  389. # [ /"/, '&#8220;' ], # double opening
  390. # [ /\b( )?\.{3}/, '\1&#8230;' ], # ellipsis
  391. # [ /\b([A-Z][A-Z0-9]{2,})\b(?:[(]([^)]*)[)])/, '<acronym title="\2">\1</acronym>' ], # 3+ uppercase acronym
  392. # [ /(^|[^"][>\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
  393. # [ /(\.\s)?\s?--\s?/, '\1&#8212;' ], # em dash
  394. # [ /\s->\s/, ' &rarr; ' ], # right arrow
  395. # [ /\s-\s/, ' &#8211; ' ], # en dash
  396. # [ /(\d+) ?x ?(\d+)/, '\1&#215;\2' ], # dimension sign
  397. # [ /\b ?[(\[]TM[\])]/i, '&#8482;' ], # trademark
  398. # [ /\b ?[(\[]R[\])]/i, '&#174;' ], # registered
  399. # [ /\b ?[(\[]C[\])]/i, '&#169;' ] # copyright
  400. ]
  401. H_ALGN_VALS = {
  402. '<' => 'left',
  403. '=' => 'center',
  404. '>' => 'right',
  405. '<>' => 'justify'
  406. }
  407. V_ALGN_VALS = {
  408. '^' => 'top',
  409. '-' => 'middle',
  410. '~' => 'bottom'
  411. }
  412. #
  413. # Flexible HTML escaping
  414. #
  415. def htmlesc( str, mode=:Quotes )
  416. if str
  417. str.gsub!( '&', '&amp;' )
  418. str.gsub!( '"', '&quot;' ) if mode != :NoQuotes
  419. str.gsub!( "'", '&#039;' ) if mode == :Quotes
  420. str.gsub!( '<', '&lt;')
  421. str.gsub!( '>', '&gt;')
  422. end
  423. str
  424. end
  425. # Search and replace for Textile glyphs (quotes, dashes, other symbols)
  426. def pgl( text )
  427. # GLYPHS.each do |re, resub, tog|
  428. # next if tog and method( tog ).call
  429. # text.gsub! re, resub
  430. # end
  431. text.gsub!(/\b([A-Z][A-Z0-9]{1,})\b(?:[(]([^)]*)[)])/) do |m|
  432. "<abbr title=\"#{htmlesc $2}\">#{$1}</abbr>"
  433. end
  434. end
  435. # Parses Textile attribute lists and builds an HTML attribute string
  436. def pba( text_in, element = "" )
  437. return +'' unless text_in
  438. style = []
  439. text = text_in.dup
  440. if element == 'td'
  441. colspan = $1 if text =~ /\\(\d+)/
  442. rowspan = $1 if text =~ /\/(\d+)/
  443. style << "vertical-align:#{v_align($&)};" if text =~ A_VLGN
  444. end
  445. if text.sub!( /\{([^"}]*)\}/, '' ) && !filter_styles
  446. sanitized = sanitize_styles($1)
  447. style << "#{sanitized};" unless sanitized.blank?
  448. end
  449. lang = $1 if
  450. text.sub!( /\[([a-z\-_]+?)\]/, '' )
  451. cls = $1 if
  452. text.sub!( /\(([^()]+?)\)/, '' )
  453. style << "padding-left:#{$1.length}em;" if
  454. text.sub!( /([(]+)/, '' )
  455. style << "padding-right:#{$1.length}em;" if text.sub!( /([)]+)/, '' )
  456. style << "text-align:#{h_align($&)};" if text =~ A_HLGN
  457. cls, id = $1, $2 if cls =~ /^(.*?)#(.*)$/
  458. # add wiki-class- and wiki-id- to classes and ids to prevent setting of
  459. # arbitrary classes and ids
  460. cls = cls.split(/\s+/).map do |c|
  461. c.starts_with?('wiki-class-') ? c : "wiki-class-#{c}"
  462. end.join(' ') if cls
  463. id = id.starts_with?('wiki-id-') ? id : "wiki-id-#{id}" if id
  464. atts = +''
  465. atts << " style=\"#{style.join}\"" unless style.empty?
  466. atts << " class=\"#{cls}\"" unless cls.to_s.empty?
  467. atts << " lang=\"#{lang}\"" if lang
  468. atts << " id=\"#{id}\"" if id
  469. atts << " colspan=\"#{colspan}\"" if colspan
  470. atts << " rowspan=\"#{rowspan}\"" if rowspan
  471. atts
  472. end
  473. 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
  474. def sanitize_styles(str)
  475. styles = str.split(";").map(&:strip)
  476. styles.reject! do |style|
  477. !style.match(STYLES_RE)
  478. end
  479. styles.join(";")
  480. end
  481. TABLE_RE = /^(?:table(_?#{S}#{A}#{C})\. ?\n)?^(#{A}#{C}\.? ?\|.*?\|)(\n\n|\Z)/m
  482. # Parses a Textile table block, building HTML from the result.
  483. def block_textile_table( text )
  484. text.gsub!( TABLE_RE ) do |matches|
  485. tatts, fullrow = $~[1..2]
  486. tatts = pba( tatts, 'table' )
  487. tatts = shelve( tatts ) if tatts
  488. rows = []
  489. fullrow.gsub!(/([^|\s])\s*\n/, "\\1<br />")
  490. fullrow.each_line do |row|
  491. ratts, row = pba( $1, 'tr' ), $2 if row =~ /^(#{A}#{C}\. )(.*)/m
  492. cells = []
  493. # the regexp prevents wiki links with a | from being cut as cells
  494. row.scan(/\|(_?#{S}#{A}#{C}\. ?)?((\[\[[^|\]]*\|[^|\]]*\]\]|[^|])*?)(?=\|)/) do |modifiers, cell|
  495. ctyp = 'd'
  496. ctyp = 'h' if modifiers && modifiers =~ /^_/
  497. catts = nil
  498. catts = pba( modifiers, 'td' ) if modifiers
  499. catts = shelve( catts ) if catts
  500. cells << "\t\t\t<t#{ctyp}#{catts}>#{cell}</t#{ctyp}>"
  501. end
  502. ratts = shelve( ratts ) if ratts
  503. rows << "\t\t<tr#{ratts}>\n#{cells.join("\n")}\n\t\t</tr>"
  504. end
  505. "\t<table#{tatts}>\n#{rows.join("\n")}\n\t</table>\n\n"
  506. end
  507. end
  508. LISTS_RE = /^([#*]+?#{C} .*?)$(?![^#*])/m
  509. LISTS_CONTENT_RE = /^([#*]+)(#{A}#{C}) (.*)$/m
  510. # Parses Textile lists and generates HTML
  511. def block_textile_lists( text )
  512. text.gsub!( LISTS_RE ) do |match|
  513. lines = match.split( /\n/ )
  514. last_line = -1
  515. depth = []
  516. lines.each_with_index do |line, line_id|
  517. if line =~ LISTS_CONTENT_RE
  518. tl,atts,content = $~[1..3]
  519. if depth.last
  520. if depth.last.length > tl.length
  521. (depth.length - 1).downto(0) do |i|
  522. break if depth[i].length == tl.length
  523. lines[line_id - 1] << "</li>\n\t</#{lT(depth[i])}l>\n\t"
  524. depth.pop
  525. end
  526. end
  527. if depth.last and depth.last.length == tl.length
  528. lines[line_id - 1] << '</li>'
  529. end
  530. end
  531. if depth.last != tl
  532. depth << tl
  533. atts = pba( atts )
  534. atts = shelve( atts ) if atts
  535. lines[line_id] = +"\t<#{lT(tl)}l#{atts}>\n\t<li>#{content}"
  536. else
  537. lines[line_id] = +"\t\t<li>#{content}"
  538. end
  539. last_line = line_id
  540. else
  541. last_line = line_id
  542. end
  543. if line_id - last_line > 1 or line_id == lines.length - 1
  544. while v = depth.pop
  545. lines[last_line] << "</li>\n\t</#{lT(v)}l>"
  546. end
  547. end
  548. end
  549. lines.join( "\n" )
  550. end
  551. end
  552. QUOTES_RE = /(^>+([^\n]*?)(\n|$))+/m
  553. QUOTES_CONTENT_RE = /^([> ]+)(.*)$/m
  554. def block_textile_quotes( text )
  555. text.gsub!( QUOTES_RE ) do |match|
  556. lines = match.split( /\n/ )
  557. quotes = +''
  558. indent = 0
  559. lines.each do |line|
  560. line =~ QUOTES_CONTENT_RE
  561. bq,content = $1, $2
  562. l = bq.count('>')
  563. if l != indent
  564. quotes << ("\n\n" + (l>indent ? '<blockquote>' * (l-indent) : '</blockquote>' * (indent-l)) + "\n\n")
  565. indent = l
  566. end
  567. quotes << (content + "\n")
  568. end
  569. quotes << ("\n" + '</blockquote>' * indent + "\n\n")
  570. quotes
  571. end
  572. end
  573. CODE_RE = /(\W)
  574. @
  575. (?:\|(\w+?)\|)?
  576. (.+?)
  577. @
  578. (?=\W)/x
  579. def inline_textile_code( text )
  580. text.gsub!( CODE_RE ) do |m|
  581. before,lang,code,after = $~[1..4]
  582. lang = " lang=\"#{lang}\"" if lang
  583. rip_offtags( +"#{before}<code#{lang}>#{code}</code>#{after}", false )
  584. end
  585. end
  586. def lT( text )
  587. /\#$/.match?(text) ? 'o' : 'u'
  588. end
  589. def hard_break( text )
  590. text.gsub!( /(.)\n(?!\Z| *([#*=]+(\s|$)|[{|]))/, "\\1<br />" ) if hard_breaks
  591. end
  592. BLOCKS_GROUP_RE = /\n{2,}(?! )/m
  593. def blocks( text, deep_code = false )
  594. text.replace( text.split( BLOCKS_GROUP_RE ).collect do |blk|
  595. plain = blk !~ /\A[#*> ]/
  596. # skip blocks that are complex HTML
  597. if blk =~ /^<\/?(\w+).*>/ and not SIMPLE_HTML_TAGS.include? $1
  598. blk
  599. else
  600. # search for indentation levels
  601. blk.strip!
  602. if blk.empty?
  603. blk
  604. else
  605. code_blk = nil
  606. blk.gsub!( /((?:\n(?:\n^ +[^\n]*)+)+)/m ) do |iblk|
  607. flush_left iblk
  608. blocks iblk, plain
  609. iblk.gsub( /^(\S)/, "\t\\1" )
  610. if plain
  611. code_blk = iblk; ""
  612. else
  613. iblk
  614. end
  615. end
  616. block_applied = 0
  617. @rules.each do |rule_name|
  618. block_applied += 1 if rule_name.to_s.match /^block_/ and method(rule_name).call(blk)
  619. end
  620. if block_applied.zero?
  621. if deep_code
  622. blk = "\t<pre><code>#{blk}</code></pre>"
  623. else
  624. blk = "\t<p>#{blk}</p>"
  625. end
  626. end
  627. # hard_break blk
  628. blk + "\n#{code_blk}"
  629. end
  630. end
  631. end.join( "\n\n" ) )
  632. end
  633. def textile_bq( tag, atts, cite, content )
  634. cite, cite_title = check_refs( cite )
  635. cite = " cite=\"#{cite}\"" if cite
  636. atts = shelve( atts ) if atts
  637. "\t<blockquote#{cite}>\n\t\t<p#{atts}>#{content}</p>\n\t</blockquote>"
  638. end
  639. def textile_p( tag, atts, cite, content )
  640. atts = shelve( atts ) if atts
  641. "\t<#{tag}#{atts}>#{content}</#{tag}>"
  642. end
  643. alias textile_h1 textile_p
  644. alias textile_h2 textile_p
  645. alias textile_h3 textile_p
  646. alias textile_h4 textile_p
  647. alias textile_h5 textile_p
  648. alias textile_h6 textile_p
  649. def textile_fn_( tag, num, atts, cite, content )
  650. atts << " id=\"fn#{num}\" class=\"footnote\""
  651. content = "<sup>#{num}</sup> #{content}"
  652. atts = shelve( atts ) if atts
  653. "\t<p#{atts}>#{content}</p>"
  654. end
  655. BLOCK_RE = /^(([a-z]+)(\d*))(#{A}#{C})\.(?::(\S+))? (.*)$/m
  656. def block_textile_prefix( text )
  657. if text =~ BLOCK_RE
  658. tag,tagpre,num,atts,cite,content = $~[1..6]
  659. atts = pba( atts )
  660. # pass to prefix handler
  661. replacement = nil
  662. if respond_to? "textile_#{tag}", true
  663. replacement = method( "textile_#{tag}" ).call( tag, atts, cite, content )
  664. elsif respond_to? "textile_#{tagpre}_", true
  665. replacement = method( "textile_#{tagpre}_" ).call( tagpre, num, atts, cite, content )
  666. end
  667. text.gsub!( $& ) { replacement } if replacement
  668. end
  669. end
  670. SETEXT_RE = /\A(.+?)\n([=-])[=-]* *$/m
  671. def block_markdown_setext( text )
  672. if text =~ SETEXT_RE
  673. tag = ($2 == "=" ? "h1" : "h2")
  674. blk, cont = "<#{tag}>#{$1}</#{tag}>", $'
  675. blocks cont
  676. text.replace( blk + cont )
  677. end
  678. end
  679. ATX_RE = /\A(\#{1,6}) # $1 = string of #'s
  680. [ ]*
  681. (.+?) # $2 = Header text
  682. [ ]*
  683. \#* # optional closing #'s (not counted)
  684. $/x
  685. def block_markdown_atx( text )
  686. if text =~ ATX_RE
  687. tag = "h#{$1.length}"
  688. blk, cont = "<#{tag}>#{$2}</#{tag}>\n\n", $'
  689. blocks cont
  690. text.replace( blk + cont )
  691. end
  692. end
  693. MARKDOWN_BQ_RE = /\A(^ *> ?.+$(.+\n)*\n*)+/m
  694. def block_markdown_bq( text )
  695. text.gsub!( MARKDOWN_BQ_RE ) do |blk|
  696. blk.gsub!( /^ *> ?/, '' )
  697. flush_left blk
  698. blocks blk
  699. blk.gsub!( /^(\S)/, "\t\\1" )
  700. "<blockquote>\n#{blk}\n</blockquote>\n\n"
  701. end
  702. end
  703. MARKDOWN_RULE_RE = /^(#{
  704. ['*', '-', '_'].collect { |ch| ' ?(' + Regexp::quote( ch ) + ' ?){3,}' }.join( '|' )
  705. })$/
  706. def block_markdown_rule( text )
  707. text.gsub!( MARKDOWN_RULE_RE ) do |blk|
  708. "<hr />"
  709. end
  710. end
  711. # XXX TODO XXX
  712. def block_markdown_lists( text )
  713. end
  714. def inline_textile_span( text )
  715. QTAGS.each do |qtag_rc, ht, qtag_re, rtype|
  716. text.gsub!( qtag_re ) do |m|
  717. case rtype
  718. when :limit
  719. sta,oqs,qtag,content,oqa = $~[1..6]
  720. atts = nil
  721. if content =~ /^(#{C})(.+)$/
  722. atts, content = $~[1..2]
  723. end
  724. else
  725. qtag,atts,cite,content = $~[1..4]
  726. sta = ''
  727. end
  728. atts = pba( atts )
  729. atts = shelve( atts ) if atts
  730. "#{sta}#{oqs}<#{ht}#{atts}>#{content}</#{ht}>#{oqa}"
  731. end
  732. end
  733. end
  734. LINK_RE = /
  735. (
  736. ([\s\[{(]|[#{PUNCT}])? # $pre
  737. " # start
  738. (#{C}) # $atts
  739. ([^"\n]+?) # $text
  740. \s?
  741. (?:\(([^)]+?)\)(?="))? # $title
  742. ":
  743. ( # $url
  744. (\/|[a-zA-Z]+:\/\/|www\.|mailto:) # $proto
  745. [[:alnum:]_\/]\S+?
  746. )
  747. (\/)? # $slash
  748. ([^[:alnum:]_\=\/;\(\)\-]*?) # $post
  749. )
  750. (?=<|\s|$)
  751. /x
  752. def inline_textile_link( text )
  753. text.gsub!( LINK_RE ) do |m|
  754. all,pre,atts,text,title,url,proto,slash,post = $~[1..9]
  755. if text.include?('<br />')
  756. all
  757. else
  758. url, url_title = check_refs(url)
  759. title ||= url_title
  760. # Idea below : an URL with unbalanced parethesis and
  761. # ending by ')' is put into external parenthesis
  762. if url[-1] == ")" and ((url.count("(") - url.count(")")) < 0)
  763. url = url[0..-2] # discard closing parenth from url
  764. post = ")" + post # add closing parenth to post
  765. end
  766. url = htmlesc(url.dup)
  767. next all if url.downcase.start_with?('javascript:')
  768. atts = pba(atts)
  769. atts = +" href=\"#{url}#{slash}\"#{atts}"
  770. atts << " title=\"#{htmlesc title}\"" if title
  771. atts = shelve(atts) if atts
  772. external = (url =~ /^https?:\/\//) ? ' class="external"' : ''
  773. "#{pre}<a#{atts}#{external}>#{text}</a>#{post}"
  774. end
  775. end
  776. end
  777. MARKDOWN_REFLINK_RE = /
  778. \[([^\[\]]+)\] # $text
  779. [ ]? # opt. space
  780. (?:\n[ ]*)? # one optional newline followed by spaces
  781. \[(.*?)\] # $id
  782. /x
  783. def inline_markdown_reflink( text )
  784. text.gsub!( MARKDOWN_REFLINK_RE ) do |m|
  785. text, id = $~[1..2]
  786. if id.empty?
  787. url, title = check_refs( text )
  788. else
  789. url, title = check_refs( id )
  790. end
  791. atts = " href=\"#{url}\""
  792. atts << " title=\"#{title}\"" if title
  793. atts = shelve( atts )
  794. "<a#{atts}>#{text}</a>"
  795. end
  796. end
  797. MARKDOWN_LINK_RE = /
  798. \[([^\[\]]+)\] # $text
  799. \( # open paren
  800. [ \t]* # opt space
  801. <?(.+?)>? # $href
  802. [ \t]* # opt space
  803. (?: # whole title
  804. (['"]) # $quote
  805. (.*?) # $title
  806. \3 # matching quote
  807. )? # title is optional
  808. \)
  809. /x
  810. def inline_markdown_link( text )
  811. text.gsub!( MARKDOWN_LINK_RE ) do |m|
  812. text, url, quote, title = $~[1..4]
  813. atts = " href=\"#{url}\""
  814. atts << " title=\"#{title}\"" if title
  815. atts = shelve( atts )
  816. "<a#{atts}>#{text}</a>"
  817. end
  818. end
  819. TEXTILE_REFS_RE = /(^ *)\[([^\[\n]+?)\](#{HYPERLINK})(?=\s|$)/
  820. MARKDOWN_REFS_RE = /(^ *)\[([^\n]+?)\]:\s+<?(#{HYPERLINK})>?(?:\s+"((?:[^"]|\\")+)")?(?=\s|$)/m
  821. def refs( text )
  822. @rules.each do |rule_name|
  823. method( rule_name ).call( text ) if rule_name.to_s.match? /^refs_/
  824. end
  825. end
  826. def refs_textile( text )
  827. text.gsub!( TEXTILE_REFS_RE ) do |m|
  828. flag, url = $~[2..3]
  829. @urlrefs[flag.downcase] = [url, nil]
  830. nil
  831. end
  832. end
  833. def refs_markdown( text )
  834. text.gsub!( MARKDOWN_REFS_RE ) do |m|
  835. flag, url = $~[2..3]
  836. title = $~[6]
  837. @urlrefs[flag.downcase] = [url, title]
  838. nil
  839. end
  840. end
  841. def check_refs( text )
  842. ret = @urlrefs[text.downcase] if text
  843. ret || [text, nil]
  844. end
  845. IMAGE_RE = /
  846. (>|\s|^) # start of line?
  847. \! # opening
  848. (\<|\=|\>)? # optional alignment atts
  849. (#{C}) # optional style,class atts
  850. (?:\. )? # optional dot-space
  851. ([^\s(!]+?) # presume this is the src
  852. \s? # optional space
  853. (?:\(((?:[^\(\)]|\([^\)]+\))+?)\))? # optional title
  854. \! # closing
  855. (?::#{HYPERLINK})? # optional href
  856. /x
  857. def inline_textile_image( text )
  858. text.gsub!( IMAGE_RE ) do |m|
  859. stln,algn,atts,url,title,href,href_a1,href_a2 = $~[1..8]
  860. htmlesc title
  861. atts = pba( atts )
  862. atts = +" src=\"#{htmlesc url.dup}\"#{atts}"
  863. atts << " title=\"#{title}\"" if title
  864. atts << " alt=\"#{title}\""
  865. # size = @getimagesize($url);
  866. # if($size) $atts.= " $size[3]";
  867. href, alt_title = check_refs( href ) if href
  868. url, url_title = check_refs( url )
  869. next m unless uri_with_safe_scheme?(url)
  870. if href
  871. href = htmlesc(href.dup)
  872. next m if href.downcase.start_with?('javascript:')
  873. end
  874. out = +''
  875. out << "<a#{shelve(" href=\"#{href}\"")}>" if href
  876. out << "<img#{shelve(atts)} />"
  877. out << "</a>#{href_a1}#{href_a2}" if href
  878. if algn
  879. algn = h_align( algn )
  880. if stln == "<p>"
  881. out = "<p style=\"float:#{algn}\">#{out}"
  882. else
  883. out = "#{stln}<span style=\"float:#{algn}\">#{out}</span>"
  884. end
  885. else
  886. out = stln + out
  887. end
  888. out
  889. end
  890. end
  891. def shelve( val )
  892. @shelf << val
  893. " :redsh##{@shelf.length}:"
  894. end
  895. def retrieve( text )
  896. text.gsub!(/ :redsh#(\d+):/) do
  897. @shelf[$1.to_i - 1] || $&
  898. end
  899. end
  900. def incoming_entities( text )
  901. ## turn any incoming ampersands into a dummy character for now.
  902. ## This uses a negative lookahead for alphanumerics followed by a semicolon,
  903. ## implying an incoming html entity, to be skipped
  904. text.gsub!( /&(?![#a-z0-9]+;)/i, "x%x%" )
  905. end
  906. def no_textile( text )
  907. text.gsub!(/(^|\s)==([^=]+.*?)==(\s|$)?/,
  908. '\1<notextile>\2</notextile>\3')
  909. text.gsub!(/^ *==([^=]+.*?)==/m,
  910. '\1<notextile>\2</notextile>\3')
  911. end
  912. def clean_white_space( text )
  913. # normalize line breaks
  914. text.gsub!( /\r\n/, "\n" )
  915. text.tr!( "\r", "\n" )
  916. text.gsub!( /\t/, ' ' )
  917. text.gsub!( /^ +$/, '' )
  918. text.gsub!( /\n{3,}/, "\n\n" )
  919. text.gsub!( /"$/, "\" " )
  920. # if entire document is indented, flush
  921. # to the left side
  922. flush_left text
  923. end
  924. def flush_left( text )
  925. if /(?![\r\n\t ])[[:cntrl:]]/.match?(text)
  926. text.gsub!(/(?![\r\n\t ])[[:cntrl:]]/, '')
  927. end
  928. if /^ +\S/.match?(text)
  929. indt = 0
  930. indt += 1 until /^ {#{indt}}\S/.match?(text)
  931. if indt.nonzero?
  932. text.gsub!( /^ {#{indt}}/, '' )
  933. end
  934. end
  935. end
  936. def footnote_ref( text )
  937. text.gsub!(/\b\[([0-9]+?)\](\s)?/,
  938. '<sup><a href="#fn\1">\1</a></sup>\2')
  939. end
  940. OFFTAGS = /(code|pre|kbd|notextile)/
  941. OFFTAG_MATCH = /(?:(<\/#{OFFTAGS}\b>)|(<#{OFFTAGS}\b[^>]*>))(.*?)(?=<\/?#{OFFTAGS}\b\W|\Z)/mi
  942. OFFTAG_OPEN = /<#{OFFTAGS}/
  943. OFFTAG_CLOSE = /<\/?#{OFFTAGS}/
  944. HASTAG_MATCH = /(<\/?\w[^\n]*?>)/m
  945. ALLTAG_MATCH = /(<\/?\w[^\n]*?>)|.*?(?=<\/?\w[^\n]*?>|$)/m
  946. def glyphs_textile( text, level = 0 )
  947. if text !~ HASTAG_MATCH
  948. pgl text
  949. footnote_ref text
  950. else
  951. codepre = 0
  952. text.gsub!( ALLTAG_MATCH ) do |line|
  953. ## matches are off if we're between <code>, <pre> etc.
  954. if $1
  955. if OFFTAG_OPEN.match?(line)
  956. codepre += 1
  957. elsif OFFTAG_CLOSE.match?(line)
  958. codepre -= 1
  959. codepre = 0 if codepre < 0
  960. end
  961. elsif codepre.zero?
  962. glyphs_textile( line, level + 1 )
  963. else
  964. htmlesc( line, :NoQuotes )
  965. end
  966. # p [level, codepre, line]
  967. line
  968. end
  969. end
  970. end
  971. def rip_offtags( text, escape_aftertag=true, escape_line=true )
  972. if text =~ /<.*>/
  973. ## strip and encode <pre> content
  974. codepre, used_offtags = 0, {}
  975. text.gsub!( OFFTAG_MATCH ) do |line|
  976. if $3
  977. first, offtag, aftertag = $3, $4, $5
  978. codepre += 1
  979. used_offtags[offtag] = true
  980. if codepre - used_offtags.length > 0
  981. htmlesc( line, :NoQuotes ) if escape_line
  982. @pre_list.last << line
  983. line = +""
  984. else
  985. ### htmlesc is disabled between CODE tags which will be parsed with highlighter
  986. ### Regexp in formatter.rb is : /<code\s+class="(\w+)">\s?(.+)/m
  987. ### NB: some changes were made not to use $N variables, because we use "match"
  988. ### and it breaks following lines
  989. htmlesc( aftertag, :NoQuotes ) if aftertag && escape_aftertag && !first.match(/<code\s+class="(\w+)">/)
  990. line = +"<redpre##{@pre_list.length}>"
  991. first.match(/<#{OFFTAGS}([^>]*)>/)
  992. tag = $1
  993. $2.to_s.match(/(class\=("[^"]+"|'[^']+'))/i)
  994. tag << " #{$1}" if $1 && tag == 'code'
  995. @pre_list << +"<#{tag}>#{aftertag}"
  996. end
  997. elsif $1 and codepre > 0
  998. if codepre - used_offtags.length > 0
  999. htmlesc( line, :NoQuotes ) if escape_line
  1000. @pre_list.last << line
  1001. line = +""
  1002. end
  1003. codepre -= 1 unless codepre.zero?
  1004. used_offtags = {} if codepre.zero?
  1005. end
  1006. line
  1007. end
  1008. end
  1009. text
  1010. end
  1011. def smooth_offtags( text )
  1012. unless @pre_list.empty?
  1013. ## replace <pre> content
  1014. text.gsub!( /<redpre#(\d+)>/ ) { @pre_list[$1.to_i] }
  1015. end
  1016. end
  1017. def inline( text )
  1018. [/^inline_/, /^glyphs_/].each do |meth_re|
  1019. @rules.each do |rule_name|
  1020. method( rule_name ).call( text ) if rule_name.to_s.match( meth_re )
  1021. end
  1022. end
  1023. end
  1024. def h_align( text )
  1025. H_ALGN_VALS[text]
  1026. end
  1027. def v_align( text )
  1028. V_ALGN_VALS[text]
  1029. end
  1030. def textile_popup_help( name, windowW, windowH )
  1031. ' <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 />'
  1032. end
  1033. # HTML cleansing stuff
  1034. BASIC_TAGS = {
  1035. 'a' => ['href', 'title'],
  1036. 'img' => ['src', 'alt', 'title'],
  1037. 'br' => [],
  1038. 'i' => nil,
  1039. 'u' => nil,
  1040. 'b' => nil,
  1041. 'pre' => nil,
  1042. 'kbd' => nil,
  1043. 'code' => ['lang'],
  1044. 'cite' => nil,
  1045. 'strong' => nil,
  1046. 'em' => nil,
  1047. 'ins' => nil,
  1048. 'sup' => nil,
  1049. 'sub' => nil,
  1050. 'del' => nil,
  1051. 'table' => nil,
  1052. 'tr' => nil,
  1053. 'td' => ['colspan', 'rowspan'],
  1054. 'th' => nil,
  1055. 'ol' => nil,
  1056. 'ul' => nil,
  1057. 'li' => nil,
  1058. 'p' => nil,
  1059. 'h1' => nil,
  1060. 'h2' => nil,
  1061. 'h3' => nil,
  1062. 'h4' => nil,
  1063. 'h5' => nil,
  1064. 'h6' => nil,
  1065. 'blockquote' => ['cite']
  1066. }
  1067. def clean_html( text, tags = BASIC_TAGS )
  1068. text.gsub!( /<!\[CDATA\[/, '' )
  1069. text.gsub!( /<(\/*)(\w+)([^>]*)>/ ) do
  1070. raw = $~
  1071. tag = raw[2].downcase
  1072. if tags.has_key? tag
  1073. pcs = [tag]
  1074. tags[tag].each do |prop|
  1075. ['"', "'", ''].each do |q|
  1076. q2 = ( q != '' ? q : '\s' )
  1077. if raw[3] =~ /#{prop}\s*=\s*#{q}([^#{q2}]+)#{q}/i
  1078. attrv = $1
  1079. next if prop == 'src' and attrv =~ %r{^(?!http)\w+:}
  1080. pcs << "#{prop}=\"#{$1.gsub('"', '\\"')}\""
  1081. break
  1082. end
  1083. end
  1084. end if tags[tag]
  1085. "<#{raw[1]}#{pcs.join " "}>"
  1086. else
  1087. " "
  1088. end
  1089. end
  1090. end
  1091. ALLOWED_TAGS = %w(redpre pre code kbd notextile)
  1092. def escape_html_tags(text)
  1093. text.gsub!(%r{<(\/?([!\w]+)[^<>\n]*)(>?)}) do |m|
  1094. if ALLOWED_TAGS.include?($2) && $3.present?
  1095. "<#{$1}#{$3}"
  1096. else
  1097. "&lt;#{$1}#{'&gt;' unless $3.blank?}"
  1098. end
  1099. end
  1100. end
  1101. end