Chris@0: # vim:ts=4:sw=4:
Chris@0: # = RedCloth - Textile and Markdown Hybrid for Ruby
Chris@0: #
Chris@0: # Homepage:: http://whytheluckystiff.net/ruby/redcloth/
Chris@0: # Author:: why the lucky stiff (http://whytheluckystiff.net/)
Chris@0: # Copyright:: (cc) 2004 why the lucky stiff (and his puppet organizations.)
Chris@0: # License:: BSD
Chris@0: #
Chris@0: # (see http://hobix.com/textile/ for a Textile Reference.)
Chris@0: #
Chris@0: # Based on (and also inspired by) both:
Chris@0: #
Chris@0: # PyTextile: http://diveintomark.org/projects/textile/textile.py.txt
Chris@0: # Textism for PHP: http://www.textism.com/tools/textile/
Chris@0: #
Chris@0: #
Chris@0:
Chris@0: # = RedCloth
Chris@0: #
Chris@0: # RedCloth is a Ruby library for converting Textile and/or Markdown
Chris@0: # into HTML. You can use either format, intermingled or separately.
Chris@0: # You can also extend RedCloth to honor your own custom text stylings.
Chris@0: #
Chris@0: # RedCloth users are encouraged to use Textile if they are generating
Chris@0: # HTML and to use Markdown if others will be viewing the plain text.
Chris@0: #
Chris@0: # == What is Textile?
Chris@0: #
Chris@0: # Textile is a simple formatting style for text
Chris@0: # documents, loosely based on some HTML conventions.
Chris@0: #
Chris@0: # == Sample Textile Text
Chris@0: #
Chris@0: # h2. This is a title
Chris@0: #
Chris@0: # h3. This is a subhead
Chris@0: #
Chris@0: # This is a bit of paragraph.
Chris@0: #
Chris@0: # bq. This is a blockquote.
Chris@0: #
Chris@0: # = Writing Textile
Chris@0: #
Chris@0: # A Textile document consists of paragraphs. Paragraphs
Chris@0: # can be specially formatted by adding a small instruction
Chris@0: # to the beginning of the paragraph.
Chris@0: #
Chris@0: # h[n]. Header of size [n].
Chris@0: # bq. Blockquote.
Chris@0: # # Numeric list.
Chris@0: # * Bulleted list.
Chris@0: #
Chris@0: # == Quick Phrase Modifiers
Chris@0: #
Chris@0: # Quick phrase modifiers are also included, to allow formatting
Chris@0: # of small portions of text within a paragraph.
Chris@0: #
Chris@0: # \_emphasis\_
Chris@0: # \_\_italicized\_\_
Chris@0: # \*strong\*
Chris@0: # \*\*bold\*\*
Chris@0: # ??citation??
Chris@0: # -deleted text-
Chris@0: # +inserted text+
Chris@0: # ^superscript^
Chris@0: # ~subscript~
Chris@0: # @code@
Chris@0: # %(classname)span%
Chris@0: #
Chris@0: # ==notextile== (leave text alone)
Chris@0: #
Chris@0: # == Links
Chris@0: #
Chris@0: # To make a hypertext link, put the link text in "quotation
Chris@0: # marks" followed immediately by a colon and the URL of the link.
Chris@0: #
Chris@0: # Optional: text in (parentheses) following the link text,
Chris@0: # but before the closing quotation mark, will become a Title
Chris@0: # attribute for the link, visible as a tool tip when a cursor is above it.
Chris@0: #
Chris@0: # Example:
Chris@0: #
Chris@0: # "This is a link (This is a title) ":http://www.textism.com
Chris@0: #
Chris@0: # Will become:
Chris@0: #
Chris@0: # This is a link
Chris@0: #
Chris@0: # == Images
Chris@0: #
Chris@0: # To insert an image, put the URL for the image inside exclamation marks.
Chris@0: #
Chris@0: # Optional: text that immediately follows the URL in (parentheses) will
Chris@0: # be used as the Alt text for the image. Images on the web should always
Chris@0: # have descriptive Alt text for the benefit of readers using non-graphical
Chris@0: # browsers.
Chris@0: #
Chris@0: # Optional: place a colon followed by a URL immediately after the
Chris@0: # closing ! to make the image into a link.
Chris@0: #
Chris@0: # Example:
Chris@0: #
Chris@0: # !http://www.textism.com/common/textist.gif(Textist)!
Chris@0: #
Chris@0: # Will become:
Chris@0: #
Chris@0: #
Chris@0: #
Chris@0: # With a link:
Chris@0: #
Chris@0: # !/common/textist.gif(Textist)!:http://textism.com
Chris@0: #
Chris@0: # Will become:
Chris@0: #
Chris@0: #
Chris@0: #
Chris@0: # == Defining Acronyms
Chris@0: #
Chris@0: # HTML allows authors to define acronyms via the tag. The definition appears as a
Chris@0: # tool tip when a cursor hovers over the acronym. A crucial aid to clear writing,
Chris@0: # this should be used at least once for each acronym in documents where they appear.
Chris@0: #
Chris@0: # To quickly define an acronym in Textile, place the full text in (parentheses)
Chris@0: # immediately following the acronym.
Chris@0: #
Chris@0: # Example:
Chris@0: #
Chris@0: # ACLU(American Civil Liberties Union)
Chris@0: #
Chris@0: # Will become:
Chris@0: #
Chris@1464: # ACLU
Chris@0: #
Chris@0: # == Adding Tables
Chris@0: #
Chris@0: # In Textile, simple tables can be added by seperating each column by
Chris@0: # a pipe.
Chris@0: #
Chris@0: # |a|simple|table|row|
Chris@0: # |And|Another|table|row|
Chris@0: #
Chris@0: # Attributes are defined by style definitions in parentheses.
Chris@0: #
Chris@0: # table(border:1px solid black).
Chris@0: # (background:#ddd;color:red). |{}| | | |
Chris@0: #
Chris@0: # == Using RedCloth
Chris@0: #
Chris@0: # RedCloth is simply an extension of the String class, which can handle
Chris@0: # Textile formatting. Use it like a String and output HTML with its
Chris@0: # RedCloth#to_html method.
Chris@0: #
Chris@0: # doc = RedCloth.new "
Chris@0: #
Chris@0: # h2. Test document
Chris@0: #
Chris@0: # Just a simple test."
Chris@0: #
Chris@0: # puts doc.to_html
Chris@0: #
Chris@0: # By default, RedCloth uses both Textile and Markdown formatting, with
Chris@0: # Textile formatting taking precedence. If you want to turn off Markdown
Chris@0: # formatting, to boost speed and limit the processor:
Chris@0: #
Chris@0: # class RedCloth::Textile.new( str )
Chris@0:
Chris@0: class RedCloth3 < String
Chris@0:
Chris@0: VERSION = '3.0.4'
Chris@0: DEFAULT_RULES = [:textile, :markdown]
Chris@0:
Chris@0: #
Chris@0: # Two accessor for setting security restrictions.
Chris@0: #
Chris@0: # This is a nice thing if you're using RedCloth for
Chris@0: # formatting in public places (e.g. Wikis) where you
Chris@0: # don't want users to abuse HTML for bad things.
Chris@0: #
Chris@0: # If +:filter_html+ is set, HTML which wasn't
Chris@0: # created by the Textile processor will be escaped.
Chris@0: #
Chris@0: # If +:filter_styles+ is set, it will also disable
Chris@0: # the style markup specifier. ('{color: red}')
Chris@0: #
Chris@0: attr_accessor :filter_html, :filter_styles
Chris@0:
Chris@0: #
Chris@0: # Accessor for toggling hard breaks.
Chris@0: #
Chris@0: # If +:hard_breaks+ is set, single newlines will
Chris@0: # be converted to HTML break tags. This is the
Chris@0: # default behavior for traditional RedCloth.
Chris@0: #
Chris@0: attr_accessor :hard_breaks
Chris@0:
Chris@0: # Accessor for toggling lite mode.
Chris@0: #
Chris@0: # In lite mode, block-level rules are ignored. This means
Chris@0: # that tables, paragraphs, lists, and such aren't available.
Chris@0: # Only the inline markup for bold, italics, entities and so on.
Chris@0: #
Chris@0: # r = RedCloth.new( "And then? She *fell*!", [:lite_mode] )
Chris@0: # r.to_html
Chris@0: # #=> "And then? She fell!"
Chris@0: #
Chris@0: attr_accessor :lite_mode
Chris@0:
Chris@0: #
Chris@0: # Accessor for toggling span caps.
Chris@0: #
Chris@0: # Textile places `span' tags around capitalized
Chris@0: # words by default, but this wreaks havoc on Wikis.
Chris@0: # If +:no_span_caps+ is set, this will be
Chris@0: # suppressed.
Chris@0: #
Chris@0: attr_accessor :no_span_caps
Chris@0:
Chris@0: #
Chris@0: # Establishes the markup predence. Available rules include:
Chris@0: #
Chris@0: # == Textile Rules
Chris@0: #
Chris@0: # The following textile rules can be set individually. Or add the complete
Chris@0: # set of rules with the single :textile rule, which supplies the rule set in
Chris@0: # the following precedence:
Chris@0: #
Chris@0: # refs_textile:: Textile references (i.e. [hobix]http://hobix.com/)
Chris@0: # block_textile_table:: Textile table block structures
Chris@0: # block_textile_lists:: Textile list structures
Chris@0: # block_textile_prefix:: Textile blocks with prefixes (i.e. bq., h2., etc.)
Chris@0: # inline_textile_image:: Textile inline images
Chris@0: # inline_textile_link:: Textile inline links
Chris@0: # inline_textile_span:: Textile inline spans
Chris@0: # glyphs_textile:: Textile entities (such as em-dashes and smart quotes)
Chris@0: #
Chris@0: # == Markdown
Chris@0: #
Chris@0: # refs_markdown:: Markdown references (for example: [hobix]: http://hobix.com/)
Chris@0: # block_markdown_setext:: Markdown setext headers
Chris@0: # block_markdown_atx:: Markdown atx headers
Chris@0: # block_markdown_rule:: Markdown horizontal rules
Chris@0: # block_markdown_bq:: Markdown blockquotes
Chris@0: # block_markdown_lists:: Markdown lists
Chris@0: # inline_markdown_link:: Markdown links
Chris@0: attr_accessor :rules
Chris@0:
Chris@0: # Returns a new RedCloth object, based on _string_ and
Chris@0: # enforcing all the included _restrictions_.
Chris@0: #
Chris@0: # r = RedCloth.new( "h1. A bold man", [:filter_html] )
Chris@0: # r.to_html
Chris@0: # #=>"
' * (l-indent) : '' * (indent-l)) + "\n\n") Chris@0: indent = l Chris@0: end Chris@0: quotes << (content + "\n") Chris@0: end Chris@0: quotes << ("\n" + '' * indent + "\n\n") Chris@0: quotes Chris@0: end Chris@0: end Chris@0: Chris@0: CODE_RE = /(\W) Chris@0: @ Chris@0: (?:\|(\w+?)\|)? Chris@0: (.+?) Chris@0: @ Chris@0: (?=\W)/x Chris@0: Chris@0: def inline_textile_code( text ) Chris@0: text.gsub!( CODE_RE ) do |m| Chris@0: before,lang,code,after = $~[1..4] Chris@0: lang = " lang=\"#{ lang }\"" if lang Chris@0: rip_offtags( "#{ before }
#{ code }
#{ after }", false )
Chris@0: end
Chris@0: end
Chris@0:
Chris@0: def lT( text )
Chris@0: text =~ /\#$/ ? 'o' : 'u'
Chris@0: end
Chris@0:
Chris@0: def hard_break( text )
Chris@0: text.gsub!( /(.)\n(?!\Z| *([#*=]+(\s|$)|[{|]))/, "\\1#{ blk }
"
Chris@0: else
Chris@0: blk = "\t#{ blk }
" Chris@0: end Chris@0: end Chris@0: # hard_break blk Chris@0: blk + "\n#{ code_blk }" Chris@0: end Chris@0: end Chris@0: Chris@0: end.join( "\n\n" ) ) Chris@0: end Chris@0: Chris@0: def textile_bq( tag, atts, cite, content ) Chris@0: cite, cite_title = check_refs( cite ) Chris@0: cite = " cite=\"#{ cite }\"" if cite Chris@0: atts = shelve( atts ) if atts Chris@0: "\t\n\t\t" Chris@0: end Chris@0: Chris@0: def textile_p( tag, atts, cite, content ) Chris@0: atts = shelve( atts ) if atts Chris@0: "\t<#{ tag }#{ atts }>#{ content }#{ tag }>" Chris@0: end Chris@0: Chris@0: alias textile_h1 textile_p Chris@0: alias textile_h2 textile_p Chris@0: alias textile_h3 textile_p Chris@0: alias textile_h4 textile_p Chris@0: alias textile_h5 textile_p Chris@0: alias textile_h6 textile_p Chris@0: Chris@0: def textile_fn_( tag, num, atts, cite, content ) Chris@0: atts << " id=\"fn#{ num }\" class=\"footnote\"" Chris@0: content = "#{ num } #{ content }" Chris@0: atts = shelve( atts ) if atts Chris@0: "\t#{ content }
\n\t
#{ content }
" Chris@0: end Chris@0: Chris@0: BLOCK_RE = /^(([a-z]+)(\d*))(#{A}#{C})\.(?::(\S+))? (.*)$/m Chris@0: Chris@0: def block_textile_prefix( text ) Chris@0: if text =~ BLOCK_RE Chris@0: tag,tagpre,num,atts,cite,content = $~[1..6] Chris@0: atts = pba( atts ) Chris@0: Chris@0: # pass to prefix handler Chris@441: replacement = nil Chris@0: if respond_to? "textile_#{ tag }", true Chris@441: replacement = method( "textile_#{ tag }" ).call( tag, atts, cite, content ) Chris@0: elsif respond_to? "textile_#{ tagpre }_", true Chris@441: replacement = method( "textile_#{ tagpre }_" ).call( tagpre, num, atts, cite, content ) Chris@0: end Chris@441: text.gsub!( $& ) { replacement } if replacement Chris@0: end Chris@0: end Chris@0: Chris@0: SETEXT_RE = /\A(.+?)\n([=-])[=-]* *$/m Chris@0: def block_markdown_setext( text ) Chris@0: if text =~ SETEXT_RE Chris@0: tag = if $2 == "="; "h1"; else; "h2"; end Chris@0: blk, cont = "<#{ tag }>#{ $1 }#{ tag }>", $' Chris@0: blocks cont Chris@0: text.replace( blk + cont ) Chris@0: end Chris@0: end Chris@0: Chris@0: ATX_RE = /\A(\#{1,6}) # $1 = string of #'s Chris@0: [ ]* Chris@0: (.+?) # $2 = Header text Chris@0: [ ]* Chris@0: \#* # optional closing #'s (not counted) Chris@0: $/x Chris@0: def block_markdown_atx( text ) Chris@0: if text =~ ATX_RE Chris@0: tag = "h#{ $1.length }" Chris@0: blk, cont = "<#{ tag }>#{ $2 }#{ tag }>\n\n", $' Chris@0: blocks cont Chris@0: text.replace( blk + cont ) Chris@0: end Chris@0: end Chris@0: Chris@0: MARKDOWN_BQ_RE = /\A(^ *> ?.+$(.+\n)*\n*)+/m Chris@0: Chris@0: def block_markdown_bq( text ) Chris@0: text.gsub!( MARKDOWN_BQ_RE ) do |blk| Chris@0: blk.gsub!( /^ *> ?/, '' ) Chris@0: flush_left blk Chris@0: blocks blk Chris@0: blk.gsub!( /^(\S)/, "\t\\1" ) Chris@0: "\n#{ blk }\n\n\n" Chris@0: end Chris@0: end Chris@0: Chris@0: MARKDOWN_RULE_RE = /^(#{ Chris@0: ['*', '-', '_'].collect { |ch| ' ?(' + Regexp::quote( ch ) + ' ?){3,}' }.join( '|' ) Chris@0: })$/ Chris@0: Chris@0: def block_markdown_rule( text ) Chris@0: text.gsub!( MARKDOWN_RULE_RE ) do |blk| Chris@0: "
" Chris@0: out = "
#{ out }" Chris@0: else Chris@0: out = "#{ stln }
, etc.
Chris@0: if $1
Chris@0: if line =~ OFFTAG_OPEN
Chris@0: codepre += 1
Chris@0: elsif line =~ OFFTAG_CLOSE
Chris@0: codepre -= 1
Chris@0: codepre = 0 if codepre < 0
Chris@0: end
Chris@0: elsif codepre.zero?
Chris@0: glyphs_textile( line, level + 1 )
Chris@0: else
Chris@0: htmlesc( line, :NoQuotes )
Chris@0: end
Chris@0: # p [level, codepre, line]
Chris@0:
Chris@0: line
Chris@0: end
Chris@0: end
Chris@0: end
Chris@0:
Chris@909: def rip_offtags( text, escape_aftertag=true, escape_line=true )
Chris@0: if text =~ /<.*>/
Chris@0: ## strip and encode content
Chris@0: codepre, used_offtags = 0, {}
Chris@0: text.gsub!( OFFTAG_MATCH ) do |line|
Chris@0: if $3
Chris@0: first, offtag, aftertag = $3, $4, $5
Chris@0: codepre += 1
Chris@0: used_offtags[offtag] = true
Chris@0: if codepre - used_offtags.length > 0
Chris@909: htmlesc( line, :NoQuotes ) if escape_line
Chris@0: @pre_list.last << line
Chris@0: line = ""
Chris@0: else
Chris@0: ### htmlesc is disabled between CODE tags which will be parsed with highlighter
Chris@0: ### Regexp in formatter.rb is : /\s?(.+)/m
Chris@0: ### NB: some changes were made not to use $N variables, because we use "match"
Chris@0: ### and it breaks following lines
Chris@0: htmlesc( aftertag, :NoQuotes ) if aftertag && escape_aftertag && !first.match(//)
Chris@0: line = ""
Chris@0: first.match(/<#{ OFFTAGS }([^>]*)>/)
Chris@0: tag = $1
Chris@119: $2.to_s.match(/(class\=("[^"]+"|'[^']+'))/i)
Chris@0: tag << " #{$1}" if $1
Chris@0: @pre_list << "<#{ tag }>#{ aftertag }"
Chris@0: end
Chris@0: elsif $1 and codepre > 0
Chris@0: if codepre - used_offtags.length > 0
Chris@909: htmlesc( line, :NoQuotes ) if escape_line
Chris@0: @pre_list.last << line
Chris@0: line = ""
Chris@0: end
Chris@0: codepre -= 1 unless codepre.zero?
Chris@0: used_offtags = {} if codepre.zero?
Chris@0: end
Chris@0: line
Chris@0: end
Chris@0: end
Chris@0: text
Chris@0: end
Chris@0:
Chris@0: def smooth_offtags( text )
Chris@0: unless @pre_list.empty?
Chris@0: ## replace content
Chris@0: text.gsub!( // ) { @pre_list[$1.to_i] }
Chris@0: end
Chris@0: end
Chris@0:
Chris@0: def inline( text )
Chris@0: [/^inline_/, /^glyphs_/].each do |meth_re|
Chris@0: @rules.each do |rule_name|
Chris@0: method( rule_name ).call( text ) if rule_name.to_s.match( meth_re )
Chris@0: end
Chris@0: end
Chris@0: end
Chris@0:
Chris@0: def h_align( text )
Chris@0: H_ALGN_VALS[text]
Chris@0: end
Chris@0:
Chris@0: def v_align( text )
Chris@0: V_ALGN_VALS[text]
Chris@0: end
Chris@0:
Chris@0: def textile_popup_help( name, windowW, windowH )
Chris@0: ' ' + name + '
'
Chris@0: end
Chris@0:
Chris@0: # HTML cleansing stuff
Chris@0: BASIC_TAGS = {
Chris@0: 'a' => ['href', 'title'],
Chris@0: 'img' => ['src', 'alt', 'title'],
Chris@0: 'br' => [],
Chris@0: 'i' => nil,
Chris@0: 'u' => nil,
Chris@0: 'b' => nil,
Chris@0: 'pre' => nil,
Chris@0: 'kbd' => nil,
Chris@0: 'code' => ['lang'],
Chris@0: 'cite' => nil,
Chris@0: 'strong' => nil,
Chris@0: 'em' => nil,
Chris@0: 'ins' => nil,
Chris@0: 'sup' => nil,
Chris@0: 'sub' => nil,
Chris@0: 'del' => nil,
Chris@0: 'table' => nil,
Chris@0: 'tr' => nil,
Chris@0: 'td' => ['colspan', 'rowspan'],
Chris@0: 'th' => nil,
Chris@0: 'ol' => nil,
Chris@0: 'ul' => nil,
Chris@0: 'li' => nil,
Chris@0: 'p' => nil,
Chris@0: 'h1' => nil,
Chris@0: 'h2' => nil,
Chris@0: 'h3' => nil,
Chris@0: 'h4' => nil,
Chris@0: 'h5' => nil,
Chris@0: 'h6' => nil,
Chris@0: 'blockquote' => ['cite']
Chris@0: }
Chris@0:
Chris@0: def clean_html( text, tags = BASIC_TAGS )
Chris@0: text.gsub!( /]*)>/ ) do
Chris@0: raw = $~
Chris@0: tag = raw[2].downcase
Chris@0: if tags.has_key? tag
Chris@0: pcs = [tag]
Chris@0: tags[tag].each do |prop|
Chris@0: ['"', "'", ''].each do |q|
Chris@0: q2 = ( q != '' ? q : '\s' )
Chris@0: if raw[3] =~ /#{prop}\s*=\s*#{q}([^#{q2}]+)#{q}/i
Chris@0: attrv = $1
Chris@0: next if prop == 'src' and attrv =~ %r{^(?!http)\w+:}
Chris@0: pcs << "#{prop}=\"#{$1.gsub('"', '\\"')}\""
Chris@0: break
Chris@0: end
Chris@0: end
Chris@0: end if tags[tag]
Chris@0: "<#{raw[1]}#{pcs.join " "}>"
Chris@0: else
Chris@0: " "
Chris@0: end
Chris@0: end
Chris@0: end
Chris@0:
Chris@0: ALLOWED_TAGS = %w(redpre pre code notextile)
Chris@0:
Chris@0: def escape_html_tags(text)
Chris@0: text.gsub!(%r{<(\/?([!\w]+)[^<>\n]*)(>?)}) {|m| ALLOWED_TAGS.include?($2) ? "<#{$1}#{$3}" : "<#{$1}#{'>' unless $3.blank?}" }
Chris@0: end
Chris@0: end
Chris@0: