annotate .svn/pristine/81/815073e9f3bcaa80c2a4ee60b82257dc8dc26440.svn-base @ 1524:82fac3dcf466 redmine-2.5-integration

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