Skip to content

Commit

Permalink
Restructured classes because rdoc couldn't handle modules in classes.
Browse files Browse the repository at this point in the history
I had modules inside classes and rdoc didn't like that (and it was bad form anyway), so converted the RedCloth class into a module.  Breaks complete backward compatibility with extensions if the extension was declared inside the class directly rather than included.
  • Loading branch information
jgarber committed Jul 17, 2008
1 parent 5c35ecd commit 8ba72f3
Show file tree
Hide file tree
Showing 12 changed files with 131 additions and 114 deletions.
3 changes: 1 addition & 2 deletions CHANGELOG
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
= RedCloth 4.0
=== 15th Feb, 2007

* New SuperRedCloth (RedCloth 4.0) is a total rewrite using Ragel for the parsing.
* Markdown support has been removed.
* Single newlines become <br> tags, just as in traditional RedCloth and other Textile parsers.
* HTML special characters are automatically escaped inside code signatures, like Textile 2. This means you can simply write @<br />@ and the symbols are escaped whereas in RedCloth 3 you had to write @&lt;br /&gt;@ to make the code fragment readable.
* The restrictions parameter is observed just like previous versions (except :hard_breaks is now the default).
* Arguments to RedCloth#to_html are called so extensions from the versions are compatible.
* Arguments to RedCloth#to_html are called so extensions made for prior versions can work. Note: extensions need to be included rather than defined directly within the RedCloth class as was previously possible.
* Custom block tags can be implemented as in the previous version, though the means of implementing them differs.
* HTML embedded in the Textile input does not often need to be escaped from Textile parsing.
* The parser will not wrap lines that begin with a space in paragraph tags.
Expand Down
2 changes: 1 addition & 1 deletion README
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ Author:: Jason Garber
Copyright:: (c) 2008 Jason Garber
License:: MIT

(See http://hobix.com/textile/ for a Textile reference.)
(See http://redcloth.org/textile/ for a Textile reference.)

= RedCloth

Expand Down
2 changes: 1 addition & 1 deletion Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ require 'rake/rdoctask'
require 'rake/testtask'
require 'fileutils'
include FileUtils
require 'lib/version'
require 'lib/redcloth/version'

NAME = RedCloth::NAME
SUMMARY = RedCloth::DESCRIPTION
Expand Down
2 changes: 1 addition & 1 deletion ext/redcloth_scan/redcloth.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

/* variable defs */
#ifndef redcloth_scan_c
extern VALUE super_ParseError, super_RedCloth;
extern VALUE super_ParseError, mRedCloth, super_RedCloth;
extern int SYM_escape_preformatted;
#endif

Expand Down
8 changes: 5 additions & 3 deletions ext/redcloth_scan/redcloth_scan.rl
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
#include <ruby.h>
#include "redcloth.h"

VALUE super_ParseError, super_RedCloth, super_HTML, super_LATEX;
VALUE mRedCloth, super_ParseError, super_RedCloth, super_HTML, super_LATEX;
int SYM_escape_preformatted;

%%{
Expand Down Expand Up @@ -509,8 +509,10 @@ redcloth_to(self, formatter)

void Init_redcloth_scan()
{
/* The RedCloth parser. See the README for Textile syntax. */
super_RedCloth = rb_define_class("RedCloth", rb_cString);
mRedCloth = rb_define_module("RedCloth");
/* A Textile document that can be converted to other formats. See
the README for Textile syntax. */
super_RedCloth = rb_define_class_under(mRedCloth, "TextileDoc", rb_cString);
rb_define_method(super_RedCloth, "to", redcloth_to, 1);
super_ParseError = rb_define_class_under(super_RedCloth, "ParseError", rb_eException);
/* Escaping */
Expand Down
119 changes: 15 additions & 104 deletions lib/redcloth.rb
Original file line number Diff line number Diff line change
@@ -1,113 +1,24 @@
require 'redcloth_scan'

$:.unshift(File.dirname(__FILE__))

require 'formatters/base'
require 'formatters/html'
require 'formatters/latex'
require 'version'

class RedCloth
#
# 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. ('{color: 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!')
#
attr_accessor :filter_html, :sanitize_html, :filter_styles, :filter_classes, :filter_ids

#
# 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. The
# accessor is deprecated and will be removed in a
# future version.
#
attr_accessor :hard_breaks

# 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>!"
#
attr_accessor :lite_mode
require 'redcloth_scan'
require 'redcloth/version'
require 'redcloth/textile_doc'
require 'redcloth/formatters/base'
require 'redcloth/formatters/html'
require 'redcloth/formatters/latex'

#
# 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.
#
attr_accessor :no_span_caps
module RedCloth

# 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>"
#
def initialize( string, restrictions = [] )
restrictions.each { |r| method("#{r}=").call( true ) }
super( string )
# A convenience method for creating a new TextileDoc. See
# RedCloth::TextileDoc.
def self.new( *args, &block )
RedCloth::TextileDoc.new( *args, &block )
end

#
# Generates HTML from the Textile contents.
#
# RedCloth.new( "And then? She *fell*!" ).to_html
# #=>"<p>And then? She <strong>fell</strong>!</p>"
#
def to_html( *rules )
apply_rules(rules)

to(RedCloth::Formatters::HTML)
end

#
# Generates LaTeX from the Textile contents.
#
# RedCloth.new( "And then? She *fell*!" ).to_latex
# #=> "And then? She \\textbf{fell}!\n\n"
#
def to_latex( *rules )
apply_rules(rules)

to(RedCloth::Formatters::LATEX)
end

private
def apply_rules(rules)
rules.each do |r|
method(r).call(self) if self.respond_to?(r)
end
# Include extension modules (if any) in TextileDoc.
def self.include(*args)
RedCloth::TextileDoc.send(:include, *args)
end

end

File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
105 changes: 105 additions & 0 deletions lib/redcloth/textile_doc.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
module RedCloth
class TextileDoc < String
#
# 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. ('{color: 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!')
#
attr_accessor :filter_html, :sanitize_html, :filter_styles, :filter_classes, :filter_ids

#
# 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. The
# accessor is deprecated and will be removed in a
# future version.
#
attr_accessor :hard_breaks

# 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>!"
#
attr_accessor :lite_mode

#
# 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.
#
attr_accessor :no_span_caps

# 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>"
#
def initialize( string, restrictions = [] )
restrictions.each { |r| method("#{r}=").call( true ) }
super( string )
end

#
# Generates HTML from the Textile contents.
#
# RedCloth.new( "And then? She *fell*!" ).to_html
# #=>"<p>And then? She <strong>fell</strong>!</p>"
#
def to_html( *rules )
apply_rules(rules)

to(RedCloth::Formatters::HTML)
end

#
# Generates LaTeX from the Textile contents.
#
# RedCloth.new( "And then? She *fell*!" ).to_latex
# #=> "And then? She \\textbf{fell}!\n\n"
#
def to_latex( *rules )
apply_rules(rules)

to(RedCloth::Formatters::LATEX)
end

private
def apply_rules(rules)
rules.each do |r|
method(r).call(self) if self.respond_to?(r)
end
end
end
end
4 changes: 2 additions & 2 deletions lib/version.rb → lib/redcloth/version.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
class RedCloth #:nodoc:
module VERSION #:nodoc:
module RedCloth
module VERSION
MAJOR = 4
MINOR = 0
TINY = 0
Expand Down

0 comments on commit 8ba72f3

Please sign in to comment.