Class: RedCloth::TextileDoc

Inherits:
String
  • Object
show all
Defined in:
lib/redcloth/textile_doc.rb,
ext/redcloth_scan/redcloth_scan.c

Defined Under Namespace

Classes: ParseError

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(string, restrictions = []) ⇒ TextileDoc

Returns a new RedCloth object, based on string, observing any restrictions specified.

r = RedCloth.new( "h1. A *bold* man" )
  #=> "h1. A *bold* man"
r.to_html
  #=>"<h1>A <b>bold</b> man</h1>"


67
68
69
70
# File 'lib/redcloth/textile_doc.rb', line 67

def initialize( string, restrictions = [] )
  restrictions.each { |r| method("#{r}=").call( true ) }
  super( string )
end

Instance Attribute Details

#filter_classesObject

Accessors for setting security restrictions.

This is a nice thing if you're using RedCloth for formatting in public places (e.g. Wikis) where you don't want users to abuse HTML for bad things.

If :filter_html is set, HTML which wasn't created by the Textile processor will be escaped. Alternatively, if :sanitize_html is set, HTML can pass through the Textile processor but unauthorized tags and attributes will be removed.

If :filter_styles is set, it will also disable the style markup specifier. ('red')

If :filter_classes is set, it will also disable class attributes. ('!(classname)image!')

If :filter_ids is set, it will also disable id attributes. ('!(classname#id)image!')



25
26
27
# File 'lib/redcloth/textile_doc.rb', line 25

def filter_classes
  @filter_classes
end

#filter_htmlObject

Accessors for setting security restrictions.

This is a nice thing if you're using RedCloth for formatting in public places (e.g. Wikis) where you don't want users to abuse HTML for bad things.

If :filter_html is set, HTML which wasn't created by the Textile processor will be escaped. Alternatively, if :sanitize_html is set, HTML can pass through the Textile processor but unauthorized tags and attributes will be removed.

If :filter_styles is set, it will also disable the style markup specifier. ('red')

If :filter_classes is set, it will also disable class attributes. ('!(classname)image!')

If :filter_ids is set, it will also disable id attributes. ('!(classname#id)image!')



25
26
27
# File 'lib/redcloth/textile_doc.rb', line 25

def filter_html
  @filter_html
end

#filter_idsObject

Accessors for setting security restrictions.

This is a nice thing if you're using RedCloth for formatting in public places (e.g. Wikis) where you don't want users to abuse HTML for bad things.

If :filter_html is set, HTML which wasn't created by the Textile processor will be escaped. Alternatively, if :sanitize_html is set, HTML can pass through the Textile processor but unauthorized tags and attributes will be removed.

If :filter_styles is set, it will also disable the style markup specifier. ('red')

If :filter_classes is set, it will also disable class attributes. ('!(classname)image!')

If :filter_ids is set, it will also disable id attributes. ('!(classname#id)image!')



25
26
27
# File 'lib/redcloth/textile_doc.rb', line 25

def filter_ids
  @filter_ids
end

#filter_stylesObject

Accessors for setting security restrictions.

This is a nice thing if you're using RedCloth for formatting in public places (e.g. Wikis) where you don't want users to abuse HTML for bad things.

If :filter_html is set, HTML which wasn't created by the Textile processor will be escaped. Alternatively, if :sanitize_html is set, HTML can pass through the Textile processor but unauthorized tags and attributes will be removed.

If :filter_styles is set, it will also disable the style markup specifier. ('red')

If :filter_classes is set, it will also disable class attributes. ('!(classname)image!')

If :filter_ids is set, it will also disable id attributes. ('!(classname#id)image!')



25
26
27
# File 'lib/redcloth/textile_doc.rb', line 25

def filter_styles
  @filter_styles
end

#hard_breaksObject

Deprecated accessor for toggling hard breaks.

Traditional RedCloth converted single newlines to HTML break tags, but later versions required :hard_breaks be set to enable this behavior. :hard_breaks is once again the default.



35
36
37
# File 'lib/redcloth/textile_doc.rb', line 35

def hard_breaks
  @hard_breaks
end

#lite_modeObject

Accessor for toggling lite mode.

In lite mode, block-level rules are ignored. This means that tables, paragraphs, lists, and such aren't available. Only the inline markup for bold, italics, entities and so on.

r = RedCloth.new( "And then? She *fell*!", [:lite_mode] )
r.to_html
#=> "And then? She <strong>fell</strong>!"


47
48
49
# File 'lib/redcloth/textile_doc.rb', line 47

def lite_mode
  @lite_mode
end

#no_span_capsObject

Accessor for toggling span caps.

Textile places `span' tags around capitalized words by default, but this wreaks havoc on Wikis. If :no_span_caps is set, this will be suppressed.



57
58
59
# File 'lib/redcloth/textile_doc.rb', line 57

def no_span_caps
  @no_span_caps
end

#sanitize_htmlObject

Accessors for setting security restrictions.

This is a nice thing if you're using RedCloth for formatting in public places (e.g. Wikis) where you don't want users to abuse HTML for bad things.

If :filter_html is set, HTML which wasn't created by the Textile processor will be escaped. Alternatively, if :sanitize_html is set, HTML can pass through the Textile processor but unauthorized tags and attributes will be removed.

If :filter_styles is set, it will also disable the style markup specifier. ('red')

If :filter_classes is set, it will also disable class attributes. ('!(classname)image!')

If :filter_ids is set, it will also disable id attributes. ('!(classname#id)image!')



25
26
27
# File 'lib/redcloth/textile_doc.rb', line 25

def sanitize_html
  @sanitize_html
end

Instance Method Details

#html_escObject

(self, str, level)



24256
24257
24258
24259
24260
24261
24262
24263
24264
24265
24266
24267
24268
24269
24270
24271
24272
24273
24274
24275
24276
24277
24278
24279
24280
24281
24282
24283
24284
24285
24286
24287
24288
24289
24290
24291
24292
24293
24294
24295
24296
24297
24298
24299
24300
24301
24302
24303
24304
24305
24306
24307
24308
24309
24310
24311
24312
24313
24314
# File 'ext/redcloth_scan/redcloth_scan.c', line 24256

static VALUE
redcloth_html_esc(int argc, VALUE* argv, VALUE self) //(self, str, level)
{
  VALUE str, level;
  
  rb_scan_args(argc, argv, "11", &str, &level);
  
  VALUE new_str = STR_NEW2("");
  if (str == Qnil)
    return new_str;
    
  StringValue(str);
  
  if (RSTRING_LEN(str) == 0)
    return new_str;
  
  char *ts = RSTRING_PTR(str), *te = RSTRING_PTR(str) + RSTRING_LEN(str);
  char *t = ts, *t2 = ts;
  const char *ch = NULL;
  if (te <= ts) return Qnil;

  while (t2 < te) {
    ch = NULL;
    
    // normal + pre
    switch (*t2)
    {
      case '&':  ch = "amp";    break;
      case '>':  ch = "gt";     break;
      case '<':  ch = "lt";     break;
    }
    
    // normal (non-pre)
    if (level != SYM_escape_preformatted) {
      switch (*t2)
      {
        case '\n': ch = "br";     break;
        case '"' : ch = "quot";   break;
        case '\'': 
          ch = (level == SYM_escape_attributes) ? "apos" : "squot";
          break;
      }
    }
    
    if (ch != NULL)
    {
      if (t2 > t)
        rb_str_cat(new_str, t, t2-t);
      rb_str_concat(new_str, rb_funcall(self, rb_intern(ch), 1, rb_hash_new()));
      t = t2 + 1;
    }

    t2++;
  }
  if (t2 > t)
    rb_str_cat(new_str, t, t2-t);
  
  return new_str;
}

#latex_esc(str) ⇒ Object

Converts special characters into LaTeX entities.



24319
24320
24321
24322
24323
24324
24325
24326
24327
24328
24329
24330
24331
24332
24333
24334
24335
24336
24337
24338
24339
24340
24341
24342
24343
24344
24345
24346
24347
24348
24349
24350
24351
24352
24353
24354
24355
24356
24357
24358
24359
24360
24361
24362
24363
24364
24365
24366
24367
24368
24369
24370
24371
24372
24373
# File 'ext/redcloth_scan/redcloth_scan.c', line 24319

static VALUE
redcloth_latex_esc(VALUE self, VALUE str)
{  
  VALUE new_str = STR_NEW2("");
  
  if (str == Qnil)
    return new_str;
    
  StringValue(str);
  
  if (RSTRING_LEN(str) == 0)
    return new_str;
  
  char *ts = RSTRING_PTR(str), *te = RSTRING_PTR(str) + RSTRING_LEN(str);
  char *t = ts, *t2 = ts;
  const char *ch = NULL;
  if (te <= ts) return Qnil;

  while (t2 < te) {
    ch = NULL;
    
    switch (*t2) 
    { 
      case '{':  ch = "#123";   break;
      case '}':  ch = "#125";   break;
      case '\\': ch = "#92";    break;
      case '#':  ch = "#35";    break;
      case '$':  ch = "#36";    break;
      case '%':  ch = "#37";    break;
      case '&':  ch = "amp";    break;
      case '_':  ch = "#95";    break;
      case '^':  ch = "circ";   break;
      case '~':  ch = "tilde";  break;
      case '<':  ch = "lt";     break;
      case '>':  ch = "gt";     break;
      case '\n': ch = "#10";    break;
    }

    if (ch != NULL)
    {
      if (t2 > t)
        rb_str_cat(new_str, t, t2-t);
      VALUE opts = rb_hash_new();
      rb_hash_aset(opts, ID2SYM(rb_intern("text")), STR_NEW2(ch));
      rb_str_concat(new_str, rb_funcall(self, rb_intern("entity"), 1, opts));
      t = t2 + 1;
    }

    t2++;
  }
  if (t2 > t)
    rb_str_cat(new_str, t, t2-t);
  
  return new_str;
}

#toObject

Transforms a Textile document with formatter



24378
24379
24380
# File 'ext/redcloth_scan/redcloth_scan.c', line 24378

static VALUE
redcloth_to(self, formatter)
VALUE self, formatter;

#to_html(*rules) ⇒ Object

Generates HTML from the Textile contents.

RedCloth.new( "And then? She *fell*!" ).to_html
  #=>"<p>And then? She <strong>fell</strong>!</p>"


78
79
80
81
82
# File 'lib/redcloth/textile_doc.rb', line 78

def to_html( *rules )
  apply_rules(rules)
  
  to(RedCloth::Formatters::HTML)
end

#to_latex(*rules) ⇒ Object

Generates LaTeX from the Textile contents.

RedCloth.new( "And then? She *fell*!" ).to_latex
  #=> "And then? She \\textbf{fell}!\n\n"


90
91
92
93
94
# File 'lib/redcloth/textile_doc.rb', line 90

def to_latex( *rules )
  apply_rules(rules)
  
  to(RedCloth::Formatters::LATEX)
end