Class: Formol::Formatters::Redcarpet::Formatter

Inherits:
Object
  • Object
show all
Defined in:
lib/formol/formatters/redcarpet/formatter.rb

Instance Method Summary collapse

Constructor Details

#initializeFormatter

Returns a new instance of Formatter.



8
9
10
11
12
13
14
15
16
# File 'lib/formol/formatters/redcarpet/formatter.rb', line 8

def initialize
  @renderer = SyntaxHighlighter.new(:hard_wrap    => true,
                                    :filter_html  => true)
  @processor = ::Redcarpet::Markdown.new(@renderer, :fenced_code_blocks   => true,
                                                    :no_intra_emphasis    => true,
                                                    :autolink             => true,
                                                    :space_after_headers  => false,
                                                    :superscript          => false)
end

Instance Method Details

#blockquote(text) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/formol/formatters/redcarpet/formatter.rb', line 31

def blockquote(text)
  quoted_text = ""
  
  # this is needed to highlight work in quotes
  text.split("\n").each_with_index do |line, index|
    quoted_text << '> ' if index == 0 || (line.strip.blank? || line.start_with?('>'))
    quoted_text << line
    quoted_text << "\n"
  end
  
  quoted_text
end

#quote(author_name, text) ⇒ Object



22
23
24
25
26
27
28
29
# File 'lib/formol/formatters/redcarpet/formatter.rb', line 22

def quote(author_name, text)
  wrote = I18n.t('formol.posts.wrote')
  
  text_with_author = "**#{author_name} #{wrote}:**\n\n"
  text_with_author << text
  
  blockquote(text_with_author)
end

#to_html(text) ⇒ Object



18
19
20
# File 'lib/formol/formatters/redcarpet/formatter.rb', line 18

def to_html(text)
  @processor.render(text).html_safe
end