Class: RedCloth

Inherits:
String
  • Object
show all
Defined in:
lib/junebug/ext/redcloth/base.rb,
lib/junebug/ext/redcloth/docbook.rb,
lib/junebug/ext/redcloth/textile.rb,
lib/junebug/ext/redcloth/markdown.rb

Constant Summary collapse

VERSION =
'3.0.4'
DEFAULT_RULES =

let each class add to this array

[]
TEXTILE_RULES =
[:refs_textile, :block_textile_table, :block_textile_lists, :block_textile_defs,
:block_textile_prefix, :inline_textile_image, :inline_textile_link,
:inline_textile_code, :inline_textile_span, :glyphs_textile,
:inline_textile_autolink_urls, :inline_textile_autolink_emails]
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]
DOCBOOK_RULES =
[:refs_docbook, :block_docbook_table, :block_docbook_lists, :block_docbook_simple_lists,
		                 :block_docbook_defs, :block_docbook_prefix, :inline_docbook_image, :inline_docbook_link,
:inline_docbook_code, :inline_docbook_glyphs, :inline_docbook_span,
:inline_docbook_wiki_words, :inline_docbook_wiki_links, :inline_docbook_autolink_urls,
:inline_docbook_autolink_emails]
DOCBOOK_GLYPHS =

Elements to handle

[
    [ /([^\s\[{(>])\'/, '\1’' ], # single closing
    [ /\'(?=\s|s\b|[#{PUNCT}])/, '’' ], # single closing
    [ /\'/, '‘' ], # single opening
#   [ /([^\s\[{(])?"(\s|:|$)/, '\1”\2' ], # double closing
    [ /([^\s\[{(>])"/, '\1”' ], # double closing
    [ /"(?=\s|[#{PUNCT}])/, '”' ], # double closing
    [ /"/, '“' ], # double opening
    [ /\b( )?\.{3}/, '\1…' ], # ellipsis
    [ /(\.\s)?\s?--\s?/, '\1—' ], # em dash
    [ /\s->\s/, ' → ' ], # right arrow
    [ /\s-\s/, ' – ' ], # en dash
    [ /(\d+) ?x ?(\d+)/, '\1×\2' ], # dimension sign
    [ /\b ?[(\[]TM[\])]/i, '™' ], # trademark
    [ /\b ?[(\[]R[\])]/i, '®' ], # registered
    [ /\b ?[(\[]C[\])]/i, '©' ] # copyright
]

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


83
84
85
86
# File 'lib/junebug/ext/redcloth/base.rb', line 83

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

Instance Attribute Details

#filter_classesObject

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

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



38
39
40
# File 'lib/junebug/ext/redcloth/base.rb', line 38

def filter_classes
  @filter_classes
end

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

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



38
39
40
# File 'lib/junebug/ext/redcloth/base.rb', line 38

def filter_html
  @filter_html
end

#filter_idsObject

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

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



38
39
40
# File 'lib/junebug/ext/redcloth/base.rb', line 38

def filter_ids
  @filter_ids
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’)

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



38
39
40
# File 'lib/junebug/ext/redcloth/base.rb', line 38

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.



47
48
49
# File 'lib/junebug/ext/redcloth/base.rb', line 47

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


59
60
61
# File 'lib/junebug/ext/redcloth/base.rb', line 59

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.



69
70
71
# File 'lib/junebug/ext/redcloth/base.rb', line 69

def no_span_caps
  @no_span_caps
end

#rulesObject

Establishes the markup predence.



74
75
76
# File 'lib/junebug/ext/redcloth/base.rb', line 74

def rules
  @rules
end

Instance Method Details

#to_docbook(*rules) ⇒ Object

Generates HTML from the Textile contents.

r = RedCloth.new( "And then? She *fell*!" )
r.to_docbook
  #=>"And then? She <emphasis role=\"strong\">fell</emphasis>!"


55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
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
109
110
111
# File 'lib/junebug/ext/redcloth/docbook.rb', line 55

def to_docbook( *rules )
    @stack = Array.new
    @ids = Array.new
    @references = Array.new
    @automatic_content_ids = Array.new

    rules = DEFAULT_RULES if rules.empty?
    # make our working copy
    text = self.dup
    
    @urlrefs = {}
    @shelf = []
    @rules = rules.collect do |rule|
        case rule
        when :docbook
            DOCBOOK_RULES
        else
            rule
        end
    end.flatten
    
    # standard clean up
    incoming_entities text 
    clean_white_space text 

    # start processor
    @pre_list = []
    pre_process_docbook text

    no_docbook text
    docbook_rip_offtags text
    docbook_hard_break text

    refs text
    docbook_blocks text
    inline text
    
    smooth_offtags text
    retrieve text
    
    post_process_docbook text
    clean_html text if filter_html
    text.strip!
    
    text << "\n"
    @stack.each_with_index {|sect,index| text << "</sect#{@stack.size-index}>\n"}
    text << "</chapter>" if @chapter
    
    if (@references - @ids).size > 0
      text << %{<chapter label="86" id="chapter-86"><title>To Come</title>}
      (@references - @ids).each {|name| text << %!<sect1 id="#{name}"><title>#{name.split('-').map {|t| t.capitalize}.join(' ')}</title><remark>TK</remark></sect1>\n!}
      text << "</chapter>"
    end
    
    text

end

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


95
96
97
98
99
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
# File 'lib/junebug/ext/redcloth/base.rb', line 95

def to_html( *rules )
    rules = DEFAULT_RULES if rules.empty?
    # make our working copy
    text = self.dup
    
	return "" if text == ""

    @urlrefs = {}
    @shelf = []
    @rules = rules.collect do |rule|
        case rule
        when :markdown
            MARKDOWN_RULES
        when :textile
            TEXTILE_RULES
        else
            rule
        end
    end.flatten

    # standard clean up
    @pre_list = []
    pre_process text
    DEFAULT_RULES.each {|ruleset| send("#{ruleset}_pre_process", text) if private_methods.include? "#{ruleset}_pre_process"}
    incoming_entities text 
    clean_white_space text 

    # start processor
    no_textile text
    rip_offtags text
    hard_break text
    unless @lite_mode
        refs text
        blocks text
    end
    inline text
    smooth_offtags text
    retrieve text

    post_process text
    DEFAULT_RULES.each {|ruleset| send("#{ruleset}_post_process", text) if private_methods.include? "#{ruleset}_post_process"}

    clean_html text if filter_html

    return text.strip

end