Class: BlueCloth

Inherits:
Object
  • Object
show all
Defined in:
lib/bluecloth.rb,
ext/bluecloth.c

Overview

Bluecloth is a Ruby implementation of Markdown, a text-to-HTML conversion tool.

Authors

  • Michael Granger <ged@FaerieMUD.org>

Contributors

  • Martin Chase <stillflame@FaerieMUD.org> - Peer review, helpful suggestions

  • Florian Gross <flgr@ccan.de> - Filter options, suggestions

This product includes software developed by David Loren Parsons <www.pell.portland.or.us/~orc>.

Version

$Id$

License

:include: LICENSE -- Please see the LICENSE file included in the distribution for copyright and licensing details.

Constant Summary

INT2FIX(MKD_NOLINKS)
MKD_NOIMAGE =

Do not process `![]' and remove IMG tags from the output.

INT2FIX(MKD_NOIMAGE)
MKD_NOPANTS =

Do not do Smartypants-style mangling of quotes, dashes, or ellipses.

INT2FIX(MKD_NOPANTS)
MKD_NOHTML =

Escape all opening angle brackets in the input text instead of allowing block-level HTML

INT2FIX(MKD_NOHTML)
MKD_STRICT =

disable SUPERSCRIPT, RELAXED_EMPHASIS

INT2FIX(MKD_STRICT)
MKD_TAGTEXT =

process text inside an html tag; no <em>, no <bold>, no html or [] expansion

INT2FIX(MKD_TAGTEXT)
MKD_NO_EXT =

don't allow pseudo-protocols

INT2FIX(MKD_NO_EXT)
MKD_CDATA =

Generate code for xml ![CDATA]

INT2FIX(MKD_CDATA)
MKD_NOSUPERSCRIPT =

Don't use superscript extension

INT2FIX(MKD_NOSUPERSCRIPT)
MKD_NORELAXED =

Relaxed emphasis -- emphasis happens everywhere

INT2FIX(MKD_NORELAXED)
MKD_NOTABLES =

disallow tables

INT2FIX(MKD_NOTABLES)
MKD_NOSTRIKETHROUGH =

forbid ~~strikethrough~~

INT2FIX(MKD_NOSTRIKETHROUGH)
MKD_TOC =

do table-of-contents processing

INT2FIX(MKD_TOC)
MKD_1_COMPAT =

MarkdownTest 1.0 Compatibility Mode

INT2FIX(MKD_1_COMPAT)
MKD_EMBED =

MKD_NOLINKS|MKD_NOIMAGE|MKD_TAGTEXT

INT2FIX(MKD_EMBED)
INT2FIX(MKD_AUTOLINK)
INT2FIX(MKD_SAFELINK)
MKD_NOHEADER =

don't process header blocks

INT2FIX(MKD_NOHEADER)
MKD_TABSTOP =

Expand tabs to 4 spaces

INT2FIX(MKD_TABSTOP)
MKD_NODIVQUOTE =

Forbid '>%class%' blocks

INT2FIX(MKD_NODIVQUOTE)
MKD_NOALPHALIST =

Forbid alphabetic lists

INT2FIX(MKD_NOALPHALIST)
MKD_NODLIST =

Forbid definition lists

INT2FIX(MKD_NODLIST)
VERSION =

Release Version

'2.0.11'
DEFAULT_OPTIONS =

The defaults for all supported options.

{
	:alphalists       => true,
	:auto_links       => false,
	:definition_lists => false,
	:divquotes        => false,
	:escape_html      => false,
	:expand_tabs      => true,
	:header_labels    => false,
	:mdtest_1_compat  => false,
	:pandoc_headers   => false,
	:pseudoprotocols  => false,
	:relaxed          => false,
	:remove_images    => false,
	:remove_links     => false,
	:safe_links       => false,
	:smartypants      => true,
	:strict_mode      => true,
	:strikethrough    => true,
	:superscript      => false,
	:tables           => false,
	:tagtext_mode     => false,
	:xml_cdata        => false,
}.freeze
INSPECT_TEXT_LENGTH =

The number of characters of the original markdown source to include in the output of #inspect

50

Class Method Summary (collapse)

Instance Method Summary (collapse)

Constructor Details

- (Object) new(string = '', options = DEFAULT_OPTIONS)

Create a new BlueCloth object that will process the given string. The options argument is a Hash that can be used to control the generated markup, and to enable/disable extensions. The supported options are:

:remove_links

Ignore links in Markdown, and escape A tags in the output. Defaults to false.

:remove_images

Ignore images in Markdown, and escape IMG tags in the output. Defaults to false.

:smartypants

Do Smartypants-style mangling of quotes, dashes, or ellipses. Defaults to true.

:pseudoprotocols

Support Discount's pseudo-protocol links. Defaults to false.

:pandoc_headers

Support the extraction of Pandoc headers, which can be fetched as a Hash via the #header method. Defaults to false.

:header_labels

Generate ID attributes for all headers. Defaults to false.

:escape_html

Escape all HTML in the input string. Defaults to false.

:strict_mode

Disables Discount's relaxed emphasis (ignores underscores in the middle of words) and superscript notation. Defaults to true.



# File 'ext/bluecloth.c'

/*
 *  call-seq:
 *     BlueCloth.new( string='', options=DEFAULT_OPTIONS )   -> object
 *
 * Create a new BlueCloth object that will process the given +string+. The +options+ 
 * argument is a Hash that can be used to control the generated markup, and to 
 * enable/disable extensions. The supported options are:
 * 
 * [:remove_links]
 *   Ignore links in Markdown, and escape A tags in the output. Defaults to +false+.
 * [:remove_images]
 *   Ignore images in Markdown, and escape IMG tags in the output. Defaults to +false+.
 * [:smartypants]
 *   Do Smartypants-style mangling of quotes, dashes, or ellipses. Defaults to +true+.
 * [:pseudoprotocols]
 *   Support Discount's pseudo-protocol links. Defaults to +false+.
 * [:pandoc_headers]
 *   Support the extraction of 
 *   {Pandoc headers}[http://johnmacfarlane.net/pandoc/README.html#title-blocks], which 
 *   can be fetched as a Hash via the #header method. Defaults to +false+.
 * [:header_labels]
 *   Generate ID attributes for all headers. Defaults to +false+.
 * [:escape_html]
 *   Escape all HTML in the input string. Defaults to +false+.
 * [:strict_mode]
 *   Disables Discount's relaxed emphasis (ignores underscores in the middle of words) and
 *   superscript notation. Defaults to +true+.
 * 
 */
static VALUE
bluecloth_initialize( int argc, VALUE *argv, VALUE self ) 

Class Method Details

+ (String) discount_version

Return the version string of the Discount library BlueCloth was built on.

Returns:

  • (String)


# File 'ext/bluecloth.c'

/*
 *  call-seq:
 *     BlueCloth.discount_version   -> string
 *
 *  Return the version string of the Discount library BlueCloth was built on.
 *
 */
static VALUE
bluecloth_s_discount_version( VALUE klass ) 

+ (Object) flags_from_opthash(opthash = {})

# Convert the specified opthash into a flags bitmask. If it's already a # Fixnum (e.g., if someone passed in an ORed flags argument instead of an # opthash), just return it as-is.



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/bluecloth.rb', line 71

def self::flags_from_opthash( opthash={} )
	return opthash if opthash.is_a?( Integer )

	# Support BlueCloth1-style options
	if opthash == :filter_html || opthash == [:filter_html]
		opthash = { :escape_html => true }
	elsif opthash == :filter_styles
		opthash = {}
	elsif !opthash.is_a?( Hash )
		raise ArgumentError, "option %p not supported" % [ opthash ]
	end

	flags = 0

	if   opthash[:remove_links]     then flags |= MKD_NOLINKS;         end
	if   opthash[:remove_images]    then flags |= MKD_NOIMAGE;         end
	if ! opthash[:smartypants]      then flags |= MKD_NOPANTS;         end
	if   opthash[:escape_html]      then flags |= MKD_NOHTML;          end
	if   opthash[:strict_mode]      then flags |= MKD_STRICT;          end
	if   opthash[:tagtext_mode]     then flags |= MKD_TAGTEXT;         end
	if ! opthash[:pseudoprotocols]  then flags |= MKD_NO_EXT;          end
	if   opthash[:xml_cdata]        then flags |= MKD_CDATA;           end
	if ! opthash[:superscript]      then flags |= MKD_NOSUPERSCRIPT;   end
	if ! opthash[:relaxed]          then flags |= MKD_NORELAXED;       end
	if ! opthash[:tables]           then flags |= MKD_NOTABLES;        end
	if ! opthash[:strikethrough]    then flags |= MKD_NOSTRIKETHROUGH; end
	if   opthash[:header_labels]    then flags |= MKD_TOC;             end
	if   opthash[:mdtest_1_compat]  then flags |= MKD_1_COMPAT;        end
	if   opthash[:auto_links]       then flags |= MKD_AUTOLINK;        end
	if   opthash[:safe_links]       then flags |= MKD_SAFELINK;        end
	if ! opthash[:pandoc_headers]   then flags |= MKD_NOHEADER;        end
	if   opthash[:expand_tabs]      then flags |= MKD_TABSTOP;         end
	if ! opthash[:divquotes]        then flags |= MKD_NODIVQUOTE;      end
	if ! opthash[:alphalists]       then flags |= MKD_NOALPHALIST;     end
	if ! opthash[:definition_lists] then flags |= MKD_NODLIST;         end

	return flags
end

+ (Object) opthash_from_flags(flags = 0)

# Returns a Hash that reflects the settings from the specified flags Integer.



112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/bluecloth.rb', line 112

def self::opthash_from_flags( flags=0 )
	flags = flags.to_i

	opthash = {}
	if  ( flags & MKD_NOLINKS         ).nonzero? then opthash[:remove_links]     = true; end
	if  ( flags & MKD_NOIMAGE         ).nonzero? then opthash[:remove_images]    = true; end
	if !( flags & MKD_NOPANTS         ).nonzero? then opthash[:smartypants]      = true; end
	if  ( flags & MKD_NOHTML          ).nonzero? then opthash[:escape_html]      = true; end
	if  ( flags & MKD_STRICT          ).nonzero? then opthash[:strict_mode]      = true; end
	if  ( flags & MKD_TAGTEXT         ).nonzero? then opthash[:tagtext_mode]     = true; end
	if !( flags & MKD_NO_EXT          ).nonzero? then opthash[:pseudoprotocols]  = true; end
	if  ( flags & MKD_CDATA           ).nonzero? then opthash[:xml_cdata]        = true; end
	if !( flags & MKD_NOSUPERSCRIPT   ).nonzero? then opthash[:superscript]      = true; end
	if !( flags & MKD_NORELAXED       ).nonzero? then opthash[:relaxed]          = true; end
	if !( flags & MKD_NOTABLES        ).nonzero? then opthash[:tables]           = true; end
	if !( flags & MKD_NOSTRIKETHROUGH ).nonzero? then opthash[:strikethrough]    = true; end
	if  ( flags & MKD_TOC             ).nonzero? then opthash[:header_labels]    = true; end
	if  ( flags & MKD_1_COMPAT        ).nonzero? then opthash[:mdtest_1_compat]  = true; end
	if  ( flags & MKD_AUTOLINK        ).nonzero? then opthash[:auto_links]       = true; end
	if  ( flags & MKD_SAFELINK        ).nonzero? then opthash[:safe_links]       = true; end
	if !( flags & MKD_NOHEADER        ).nonzero? then opthash[:pandoc_headers]   = true; end
	if  ( flags & MKD_TABSTOP         ).nonzero? then opthash[:expand_tabs]      = true; end
	if !( flags & MKD_NODIVQUOTE      ).nonzero? then opthash[:divquotes]        = true; end
	if !( flags & MKD_NOALPHALIST     ).nonzero? then opthash[:alphalists]       = true; end
	if !( flags & MKD_NODLIST         ).nonzero? then opthash[:definition_lists] = true; end

	return opthash
end

Instance Method Details

- (Object) filter_html

# Backward-compatible method: return true if the object's :escape_html option was # set.



161
162
163
# File 'lib/bluecloth.rb', line 161

def filter_html
	return self.options[:escape_html]
end

- (Object) filter_html=(arg)

# Backward-compatible method: raises an appropriate error notifying the user that # BlueCloth2 doesn't support this option.

Raises:

  • (NotImplementedError)


168
169
170
171
172
173
# File 'lib/bluecloth.rb', line 168

def filter_html=( arg )
	raise NotImplementedError,
		"Sorry, this version of BlueCloth no longer supports toggling of HTML filtering" +
		"via #filter_html=. You now must create the BlueCloth object with the :escape_html" +
		"option set to true instead."
end

- (Hash) header

Return the hash of Pandoc-style headers from the parsed document. If there were no headers, or the BlueCloth object was not constructed with the :pandoc_headers option enabled, an empty Hash is returned.

markdown = "%title My Essay\n%author Me\n%date Today\n\nSome stuff..."
bc = BlueCloth.new( markdown, :pandoc_headers => true )
# =>
bc.header
# =>

Returns:

  • (Hash)


# File 'ext/bluecloth.c'

/*
 *  call-seq:
 *     bluecloth.header   -> hash
 *
 *  Return the hash of 
 *  {Pandoc-style headers}[http://johnmacfarlane.net/pandoc/README.html#title-blocks]
 *  from the parsed document. If there were no headers, or the BlueCloth object was not
 *  constructed with the :pandoc_headers option enabled, an empty Hash is returned.
 *
 *     markdown = "%title My Essay\n%author Me\n%date Today\n\nSome stuff..."
 *     bc = BlueCloth.new( markdown, :pandoc_headers => true )
 *     # => 
 *     bc.header
 *     # => 
 */
static VALUE
bluecloth_header( VALUE self ) 

- (Object) inspect

# Return a human-readable representation of the object suitable for debugging.



147
148
149
150
151
152
153
154
155
156
# File 'lib/bluecloth.rb', line 147

def inspect
	return "#<%s:0x%x text: %p; options: %p>" % [
		self.class.name,
		self.object_id / 2,
		self.text.length > INSPECT_TEXT_LENGTH ?
			self.text[ 0, INSPECT_TEXT_LENGTH - 5] + '[...]' :
			self.text,
		self.options,
	]
end

- (String) to_html

Transform the document into HTML.

Returns:

  • (String)


# File 'ext/bluecloth.c'

/*
 *  call-seq:
 *     bluecloth.to_html   -> string
 *
 *  Transform the document into HTML.
 *
 */
static VALUE
bluecloth_to_html( VALUE self )