annotate .svn/pristine/fa/fa16a1d3cc85f7d975c8b277b6a1fb967cd6fdd6.svn-base @ 1327:287f201c2802 redmine-2.2-integration

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