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.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212
  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 = '(?=\/|https?:\/\/|s?ftps?:\/\/|www\.|mailto:)(\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. 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. (\/|https?:\/\/|s?ftps?:\/\/|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. atts = pba(atts)
  767. atts = +" href=\"#{htmlesc url}#{slash}\"#{atts}"
  768. atts << " title=\"#{htmlesc title}\"" if title
  769. atts = shelve(atts) if atts
  770. external = (url =~ /^https?:\/\//) ? ' class="external"' : ''
  771. "#{pre}<a#{atts}#{external}>#{text}</a>#{post}"
  772. end
  773. end
  774. end
  775. MARKDOWN_REFLINK_RE = /
  776. \[([^\[\]]+)\] # $text
  777. [ ]? # opt. space
  778. (?:\n[ ]*)? # one optional newline followed by spaces
  779. \[(.*?)\] # $id
  780. /x
  781. def inline_markdown_reflink( text )
  782. text.gsub!( MARKDOWN_REFLINK_RE ) do |m|
  783. text, id = $~[1..2]
  784. if id.empty?
  785. url, title = check_refs( text )
  786. else
  787. url, title = check_refs( id )
  788. end
  789. atts = " href=\"#{url}\""
  790. atts << " title=\"#{title}\"" if title
  791. atts = shelve( atts )
  792. "<a#{atts}>#{text}</a>"
  793. end
  794. end
  795. MARKDOWN_LINK_RE = /
  796. \[([^\[\]]+)\] # $text
  797. \( # open paren
  798. [ \t]* # opt space
  799. <?(.+?)>? # $href
  800. [ \t]* # opt space
  801. (?: # whole title
  802. (['"]) # $quote
  803. (.*?) # $title
  804. \3 # matching quote
  805. )? # title is optional
  806. \)
  807. /x
  808. def inline_markdown_link( text )
  809. text.gsub!( MARKDOWN_LINK_RE ) do |m|
  810. text, url, quote, title = $~[1..4]
  811. atts = " href=\"#{url}\""
  812. atts << " title=\"#{title}\"" if title
  813. atts = shelve( atts )
  814. "<a#{atts}>#{text}</a>"
  815. end
  816. end
  817. TEXTILE_REFS_RE = /(^ *)\[([^\[\n]+?)\](#{HYPERLINK})(?=\s|$)/
  818. MARKDOWN_REFS_RE = /(^ *)\[([^\n]+?)\]:\s+<?(#{HYPERLINK})>?(?:\s+"((?:[^"]|\\")+)")?(?=\s|$)/m
  819. def refs( text )
  820. @rules.each do |rule_name|
  821. method( rule_name ).call( text ) if rule_name.to_s.match /^refs_/
  822. end
  823. end
  824. def refs_textile( text )
  825. text.gsub!( TEXTILE_REFS_RE ) do |m|
  826. flag, url = $~[2..3]
  827. @urlrefs[flag.downcase] = [url, nil]
  828. nil
  829. end
  830. end
  831. def refs_markdown( text )
  832. text.gsub!( MARKDOWN_REFS_RE ) do |m|
  833. flag, url = $~[2..3]
  834. title = $~[6]
  835. @urlrefs[flag.downcase] = [url, title]
  836. nil
  837. end
  838. end
  839. def check_refs( text )
  840. ret = @urlrefs[text.downcase] if text
  841. ret || [text, nil]
  842. end
  843. IMAGE_RE = /
  844. (>|\s|^) # start of line?
  845. \! # opening
  846. (\<|\=|\>)? # optional alignment atts
  847. (#{C}) # optional style,class atts
  848. (?:\. )? # optional dot-space
  849. ([^\s(!]+?) # presume this is the src
  850. \s? # optional space
  851. (?:\(((?:[^\(\)]|\([^\)]+\))+?)\))? # optional title
  852. \! # closing
  853. (?::#{HYPERLINK})? # optional href
  854. /x
  855. def inline_textile_image( text )
  856. text.gsub!( IMAGE_RE ) do |m|
  857. stln,algn,atts,url,title,href,href_a1,href_a2 = $~[1..8]
  858. htmlesc title
  859. atts = pba( atts )
  860. atts = +" src=\"#{htmlesc url.dup}\"#{atts}"
  861. atts << " title=\"#{title}\"" if title
  862. atts << " alt=\"#{title}\""
  863. # size = @getimagesize($url);
  864. # if($size) $atts.= " $size[3]";
  865. href, alt_title = check_refs( href ) if href
  866. url, url_title = check_refs( url )
  867. next m unless uri_with_safe_scheme?(url)
  868. out = +''
  869. out << "<a#{shelve(" href=\"#{href}\"")}>" if href
  870. out << "<img#{shelve(atts)} />"
  871. out << "</a>#{href_a1}#{href_a2}" if href
  872. if algn
  873. algn = h_align( algn )
  874. if stln == "<p>"
  875. out = "<p style=\"float:#{algn}\">#{out}"
  876. else
  877. out = "#{stln}<span style=\"float:#{algn}\">#{out}</span>"
  878. end
  879. else
  880. out = stln + out
  881. end
  882. out
  883. end
  884. end
  885. def shelve( val )
  886. @shelf << val
  887. " :redsh##{@shelf.length}:"
  888. end
  889. def retrieve( text )
  890. text.gsub!(/ :redsh#(\d+):/) do
  891. @shelf[$1.to_i - 1] || $&
  892. end
  893. end
  894. def incoming_entities( text )
  895. ## turn any incoming ampersands into a dummy character for now.
  896. ## This uses a negative lookahead for alphanumerics followed by a semicolon,
  897. ## implying an incoming html entity, to be skipped
  898. text.gsub!( /&(?![#a-z0-9]+;)/i, "x%x%" )
  899. end
  900. def no_textile( text )
  901. text.gsub!(/(^|\s)==([^=]+.*?)==(\s|$)?/,
  902. '\1<notextile>\2</notextile>\3')
  903. text.gsub!(/^ *==([^=]+.*?)==/m,
  904. '\1<notextile>\2</notextile>\3')
  905. end
  906. def clean_white_space( text )
  907. # normalize line breaks
  908. text.gsub!( /\r\n/, "\n" )
  909. text.gsub!( /\r/, "\n" )
  910. text.gsub!( /\t/, ' ' )
  911. text.gsub!( /^ +$/, '' )
  912. text.gsub!( /\n{3,}/, "\n\n" )
  913. text.gsub!( /"$/, "\" " )
  914. # if entire document is indented, flush
  915. # to the left side
  916. flush_left text
  917. end
  918. def flush_left( text )
  919. if /(?![\r\n\t ])[[:cntrl:]]/.match?(text)
  920. text.gsub!(/(?![\r\n\t ])[[:cntrl:]]/, '')
  921. end
  922. if /^ +\S/.match?(text)
  923. indt = 0
  924. indt += 1 until /^ {#{indt}}\S/.match?(text)
  925. if indt.nonzero?
  926. text.gsub!( /^ {#{indt}}/, '' )
  927. end
  928. end
  929. end
  930. def footnote_ref( text )
  931. text.gsub!(/\b\[([0-9]+?)\](\s)?/,
  932. '<sup><a href="#fn\1">\1</a></sup>\2')
  933. end
  934. OFFTAGS = /(code|pre|kbd|notextile)/
  935. OFFTAG_MATCH = /(?:(<\/#{OFFTAGS}\b>)|(<#{OFFTAGS}\b[^>]*>))(.*?)(?=<\/?#{OFFTAGS}\b\W|\Z)/mi
  936. OFFTAG_OPEN = /<#{OFFTAGS}/
  937. OFFTAG_CLOSE = /<\/?#{OFFTAGS}/
  938. HASTAG_MATCH = /(<\/?\w[^\n]*?>)/m
  939. ALLTAG_MATCH = /(<\/?\w[^\n]*?>)|.*?(?=<\/?\w[^\n]*?>|$)/m
  940. def glyphs_textile( text, level = 0 )
  941. if text !~ HASTAG_MATCH
  942. pgl text
  943. footnote_ref text
  944. else
  945. codepre = 0
  946. text.gsub!( ALLTAG_MATCH ) do |line|
  947. ## matches are off if we're between <code>, <pre> etc.
  948. if $1
  949. if line =~ OFFTAG_OPEN
  950. codepre += 1
  951. elsif line =~ OFFTAG_CLOSE
  952. codepre -= 1
  953. codepre = 0 if codepre < 0
  954. end
  955. elsif codepre.zero?
  956. glyphs_textile( line, level + 1 )
  957. else
  958. htmlesc( line, :NoQuotes )
  959. end
  960. # p [level, codepre, line]
  961. line
  962. end
  963. end
  964. end
  965. def rip_offtags( text, escape_aftertag=true, escape_line=true )
  966. if text =~ /<.*>/
  967. ## strip and encode <pre> content
  968. codepre, used_offtags = 0, {}
  969. text.gsub!( OFFTAG_MATCH ) do |line|
  970. if $3
  971. first, offtag, aftertag = $3, $4, $5
  972. codepre += 1
  973. used_offtags[offtag] = true
  974. if codepre - used_offtags.length > 0
  975. htmlesc( line, :NoQuotes ) if escape_line
  976. @pre_list.last << line
  977. line = +""
  978. else
  979. ### htmlesc is disabled between CODE tags which will be parsed with highlighter
  980. ### Regexp in formatter.rb is : /<code\s+class="(\w+)">\s?(.+)/m
  981. ### NB: some changes were made not to use $N variables, because we use "match"
  982. ### and it breaks following lines
  983. htmlesc( aftertag, :NoQuotes ) if aftertag && escape_aftertag && !first.match(/<code\s+class="(\w+)">/)
  984. line = +"<redpre##{@pre_list.length}>"
  985. first.match(/<#{OFFTAGS}([^>]*)>/)
  986. tag = $1
  987. $2.to_s.match(/(class\=("[^"]+"|'[^']+'))/i)
  988. tag << " #{$1}" if $1 && tag == 'code'
  989. @pre_list << +"<#{tag}>#{aftertag}"
  990. end
  991. elsif $1 and codepre > 0
  992. if codepre - used_offtags.length > 0
  993. htmlesc( line, :NoQuotes ) if escape_line
  994. @pre_list.last << line
  995. line = +""
  996. end
  997. codepre -= 1 unless codepre.zero?
  998. used_offtags = {} if codepre.zero?
  999. end
  1000. line
  1001. end
  1002. end
  1003. text
  1004. end
  1005. def smooth_offtags( text )
  1006. unless @pre_list.empty?
  1007. ## replace <pre> content
  1008. text.gsub!( /<redpre#(\d+)>/ ) { @pre_list[$1.to_i] }
  1009. end
  1010. end
  1011. def inline( text )
  1012. [/^inline_/, /^glyphs_/].each do |meth_re|
  1013. @rules.each do |rule_name|
  1014. method( rule_name ).call( text ) if rule_name.to_s.match( meth_re )
  1015. end
  1016. end
  1017. end
  1018. def h_align( text )
  1019. H_ALGN_VALS[text]
  1020. end
  1021. def v_align( text )
  1022. V_ALGN_VALS[text]
  1023. end
  1024. def textile_popup_help( name, windowW, windowH )
  1025. ' <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 />'
  1026. end
  1027. # HTML cleansing stuff
  1028. BASIC_TAGS = {
  1029. 'a' => ['href', 'title'],
  1030. 'img' => ['src', 'alt', 'title'],
  1031. 'br' => [],
  1032. 'i' => nil,
  1033. 'u' => nil,
  1034. 'b' => nil,
  1035. 'pre' => nil,
  1036. 'kbd' => nil,
  1037. 'code' => ['lang'],
  1038. 'cite' => nil,
  1039. 'strong' => nil,
  1040. 'em' => nil,
  1041. 'ins' => nil,
  1042. 'sup' => nil,
  1043. 'sub' => nil,
  1044. 'del' => nil,
  1045. 'table' => nil,
  1046. 'tr' => nil,
  1047. 'td' => ['colspan', 'rowspan'],
  1048. 'th' => nil,
  1049. 'ol' => nil,
  1050. 'ul' => nil,
  1051. 'li' => nil,
  1052. 'p' => nil,
  1053. 'h1' => nil,
  1054. 'h2' => nil,
  1055. 'h3' => nil,
  1056. 'h4' => nil,
  1057. 'h5' => nil,
  1058. 'h6' => nil,
  1059. 'blockquote' => ['cite']
  1060. }
  1061. def clean_html( text, tags = BASIC_TAGS )
  1062. text.gsub!( /<!\[CDATA\[/, '' )
  1063. text.gsub!( /<(\/*)(\w+)([^>]*)>/ ) do
  1064. raw = $~
  1065. tag = raw[2].downcase
  1066. if tags.has_key? tag
  1067. pcs = [tag]
  1068. tags[tag].each do |prop|
  1069. ['"', "'", ''].each do |q|
  1070. q2 = ( q != '' ? q : '\s' )
  1071. if raw[3] =~ /#{prop}\s*=\s*#{q}([^#{q2}]+)#{q}/i
  1072. attrv = $1
  1073. next if prop == 'src' and attrv =~ %r{^(?!http)\w+:}
  1074. pcs << "#{prop}=\"#{$1.gsub('"', '\\"')}\""
  1075. break
  1076. end
  1077. end
  1078. end if tags[tag]
  1079. "<#{raw[1]}#{pcs.join " "}>"
  1080. else
  1081. " "
  1082. end
  1083. end
  1084. end
  1085. ALLOWED_TAGS = %w(redpre pre code kbd notextile)
  1086. def escape_html_tags(text)
  1087. text.gsub!(%r{<(\/?([!\w]+)[^<>\n]*)(>?)}) do |m|
  1088. if ALLOWED_TAGS.include?($2) && $3.present?
  1089. "<#{$1}#{$3}"
  1090. else
  1091. "&lt;#{$1}#{'&gt;' unless $3.blank?}"
  1092. end
  1093. end
  1094. end
  1095. end