Class: RedCloth

Inherits:
String
  • Object
show all
Defined in:
lib/kitabu/redcloth.rb

Direct Known Subclasses

BlackCloth

Constant Summary collapse

VERSION =
'3.0.4'
DEFAULT_RULES =
[:textile, :markdown]

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new RedCloth object, based on string and enforcing all the included restrictions.

r = RedCloth.new( "h1. A <b>bold</b> man", [:filter_html] )
r.to_html
  #=>"<h1>A &lt;b&gt;bold&lt;/b&gt; man</h1>"


88
89
90
91
# File 'lib/kitabu/redcloth.rb', line 88

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

Instance Attribute Details

#filter_htmlObject

Two accessor 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.

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



19
20
21
# File 'lib/kitabu/redcloth.rb', line 19

def filter_html
  @filter_html
end

#filter_stylesObject

Two accessor 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.

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



19
20
21
# File 'lib/kitabu/redcloth.rb', line 19

def filter_styles
  @filter_styles
end

#hard_breaksObject

Accessor for toggling hard breaks.

If :hard_breaks is set, single newlines will be converted to HTML break tags. This is the default behavior for traditional RedCloth.



28
29
30
# File 'lib/kitabu/redcloth.rb', line 28

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>!"


40
41
42
# File 'lib/kitabu/redcloth.rb', line 40

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.



50
51
52
# File 'lib/kitabu/redcloth.rb', line 50

def no_span_caps
  @no_span_caps
end

#rulesObject

Establishes the markup predence. Available rules include:

Textile Rules

The following textile rules can be set individually. Or add the complete set of rules with the single :textile rule, which supplies the rule set in the following precedence:

refs_textile

Textile references (i.e. [hobix]hobix.com/)

block_textile_table

Textile table block structures

block_textile_lists

Textile list structures

block_textile_prefix

Textile blocks with prefixes (i.e. bq., h2., etc.)

inline_textile_image

Textile inline images

inline_textile_link

Textile inline links

inline_textile_span

Textile inline spans

glyphs_textile

Textile entities (such as em-dashes and smart quotes)

Markdown

refs_markdown

Markdown references (for example: [hobix]: hobix.com/)

block_markdown_setext

Markdown setext headers

block_markdown_atx

Markdown atx headers

block_markdown_rule

Markdown horizontal rules

block_markdown_bq

Markdown blockquotes

block_markdown_lists

Markdown lists

inline_markdown_link

Markdown links



79
80
81
# File 'lib/kitabu/redcloth.rb', line 79

def rules
  @rules
end

Instance Method Details

#to_html(*rules) ⇒ Object

Generates HTML from the Textile contents.

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


100
101
102
103
104
105
106
107
108
109
110
111
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
140
141
142
143
144
145
146
147
148
# File 'lib/kitabu/redcloth.rb', line 100

def to_html( *rules )
    rules = DEFAULT_RULES if rules.empty?
    # make our working copy
    text = self.dup
    
    @urlrefs = {}
    @shelf = []
    textile_rules = [:refs_textile, :block_textile_table, :block_textile_lists,
                     :block_textile_prefix, :inline_textile_image, :inline_textile_link,
                     :inline_textile_code, :inline_textile_span, :glyphs_textile]
    markdown_rules = [:refs_markdown, :block_markdown_setext, :block_markdown_atx, :block_markdown_rule,
                      :block_markdown_bq, :block_markdown_lists, 
                      :inline_markdown_reflink, :inline_markdown_link]
    @rules = rules.collect do |rule|
        case rule
        when :markdown
            markdown_rules
        when :textile
            textile_rules
        else
            rule
        end
    end.flatten

    # standard clean up
    incoming_entities text 
    clean_white_space text 

    # start processor
    @pre_list = []
    rip_offtags text
    no_textile text
    hard_break text 
    unless @lite_mode
        refs text
        blocks text
    end
    inline text
    smooth_offtags text

    retrieve text

    text.gsub!( /<\/?notextile>/, '' )
    text.gsub!( /x%x%/, '&#38;' )
    clean_html text if filter_html
    text.strip!
    text

end