annotate .svn/pristine/fa/fa16a1d3cc85f7d975c8b277b6a1fb967cd6fdd6.svn-base @ 1298:4f746d8966dd redmine_2.3_integration

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