annotate .svn/pristine/9f/9fd113b96c23f167364f0fc6748b93c60320709b.svn-base @ 1519:afce8026aaeb redmine-2.4-integration

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