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

Contributors

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

Version

$Id: bluecloth.rb,v 20ece393cda0 2011/02/10 01:37:48 ged $

License

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

Constant Summary collapse

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
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)

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#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, 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.



184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
# File 'ext/bluecloth.c', line 184

static VALUE
bluecloth_initialize( int argc, VALUE *argv, VALUE self ) {
  if ( !bluecloth_check_ptr(self) ) {
    MMIOT *document;
    VALUE text, optflags, fullhash, opthash = Qnil;
    int flags = 0;
    VALUE utf8text = Qnil;

    rb_scan_args( argc, argv, "02", &text, &opthash );

    /* Default empty string and options */
    if ( argc == 0 ) {
      text = rb_str_new( "", 0 );
    }

    /* One arg could be either the text or the opthash, so shift the args if appropriate */
    else if ( argc == 1 && (TYPE(text) == T_HASH || TYPE(text) == T_FIXNUM) ) {
      opthash = text;
      text = rb_str_new( "", 0 );
    }
    else {
      text = rb_obj_dup( rb_obj_as_string(text) );
    }

    /* Merge the options hash with the defaults and turn it into a flags int */
    if ( NIL_P(opthash) ) opthash = rb_hash_new();
    optflags = rb_funcall( bluecloth_cBlueCloth, rb_intern("flags_from_opthash"), 1, opthash );
    fullhash = rb_funcall( bluecloth_cBlueCloth, rb_intern("opthash_from_flags"), 1, optflags );

    flags = NUM2INT( optflags );

#ifdef M17N_SUPPORTED
    bluecloth_debug( "Bytes before utf8ification: %s",
      RSTRING_PTR(rb_funcall(text, rb_intern("dump"), 0, Qnil)) );
    utf8text = rb_str_export_to_enc( rb_str_dup(text), rb_utf8_encoding() );
    DATA_PTR( self ) = document = bluecloth_alloc( utf8text, flags );
#else
    DATA_PTR( self ) = document = bluecloth_alloc( text, flags );
#endif /* M17N_SUPPORTED */

    if ( !mkd_compile(document, flags) )
      rb_raise( rb_eRuntimeError, "Failed to compile markdown" );

    OBJ_FREEZE( text );
    rb_iv_set( self, "@text", text );
    OBJ_FREEZE( fullhash );
    rb_iv_set( self, "@options", fullhash );

    OBJ_INFECT( self, text );
  }

  return self;
}

Instance Attribute Details

#optionsObject (readonly)

The options hash that describes the options in effect when the object was created

#textObject (readonly)

The original Markdown text the object was constructed with

Class Method Details

.discount_versionString

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

Returns:

  • (String)


146
147
148
149
# File 'ext/bluecloth.c', line 146

static VALUE
bluecloth_s_discount_version( VALUE klass ) {
  return rb_str_new2( markdown_version );
}

.flags_from_opthash(opthash = {}) ⇒ Object

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

.opthash_from_flags(flags = 0) ⇒ Object

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

#filter_htmlObject

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

#filter_html=(arg) ⇒ Object

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

#headerHash Also known as: pandoc_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)


296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
# File 'ext/bluecloth.c', line 296

static VALUE
bluecloth_header( VALUE self ) {
  MMIOT *document = bluecloth_get_ptr( self );
  char *field;
  VALUE fieldstring, headers = rb_hash_new();

  bluecloth_debug( "Fetching pandoc headers for document %p", document );

  if ( (field = mkd_doc_title(document)) ) {
    fieldstring = rb_str_new2( field );
    OBJ_INFECT( fieldstring, self );
    rb_hash_aset( headers, ID2SYM(rb_intern("title")), fieldstring );
  }
  if ( (field = mkd_doc_author(document)) ) {
    fieldstring = rb_str_new2( field );
    OBJ_INFECT( fieldstring, self );
    rb_hash_aset( headers, ID2SYM(rb_intern("author")), fieldstring );
  }
  if ( (field = mkd_doc_date(document)) ) {
    fieldstring = rb_str_new2( field );
    OBJ_INFECT( fieldstring, self );
    rb_hash_aset( headers, ID2SYM(rb_intern("date")), fieldstring );
  }

  return headers;
}

#inspectObject

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

#to_htmlString

Transform the document into HTML.

Returns:

  • (String)


246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
# File 'ext/bluecloth.c', line 246

static VALUE
bluecloth_to_html( VALUE self ) {
  MMIOT *document = bluecloth_get_ptr( self );
  char *output;
  int length;
  VALUE result = Qnil;

  bluecloth_debug( "Compiling document %p", document );

  if ( (length = mkd_document( document, &output )) != EOF ) {
#ifdef M17N_SUPPORTED
    VALUE orig_encoding = rb_obj_encoding( rb_iv_get(self, "@text") );
    VALUE utf8_result = rb_enc_str_new( output, strlen(output), rb_utf8_encoding() );
    result = rb_str_encode( utf8_result, orig_encoding, 0, Qnil );
    bluecloth_debug( "Bytes after un-utf8ification (if necessary): %s",
      RSTRING_PTR(rb_funcall(result, rb_intern("dump"), 0, Qnil)) );
#else
    result = rb_str_new2( output );
#endif /* M17N_SUPPORTED */

    OBJ_INFECT( result, self );

    return result;
  } else {
    return Qnil;
  }
}