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