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