Class: Diaspora::MessageRenderer::Processor

Inherits:
Object
  • Object
show all
Includes:
ActionView::Helpers::TagHelper
Defined in:
lib/diaspora/message_renderer.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(message, options) ⇒ Processor


21
22
23
24
# File 'lib/diaspora/message_renderer.rb', line 21

def initialize message, options
  @message = message
  @options = options
end

Instance Attribute Details

#messageObject (readonly)

Returns the value of attribute message


19
20
21
# File 'lib/diaspora/message_renderer.rb', line 19

def message
  @message
end

#optionsObject (readonly)

Returns the value of attribute options


19
20
21
# File 'lib/diaspora/message_renderer.rb', line 19

def options
  @options
end

Class Method Details

.process(message, options, &block) ⇒ Object


11
12
13
14
15
16
# File 'lib/diaspora/message_renderer.rb', line 11

def process message, options, &block
  return '' if message.blank? # Optimize for empty message
  processor = new message, options
  processor.instance_exec(&block)
  processor.message
end

Instance Method Details

#append_and_truncateObject


30
31
32
33
34
35
36
37
# File 'lib/diaspora/message_renderer.rb', line 30

def append_and_truncate
  if options[:truncate]
    @message = message.truncate options[:truncate]-options[:append].to_s.size
  end

  message << options[:append].to_s
  message << options[:append_after_truncate].to_s
end

#escapeObject


40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/diaspora/message_renderer.rb', line 40

def escape
  if options[:escape]
    # TODO: On Rails 4 port change this to ERB::Util.html_escape_once
    # and remove the include
    @message = escape_once message

    # Special case Hex entities since escape_once
    # doesn't catch them.
    # TODO: Watch for https://github.com/rails/rails/pull/9102
    # on whether this can be removed
    @message = message.gsub(/&amp;(#[xX][\dA-Fa-f]{1,4});/, '&\1;')
  end
end

#make_mentions_plain_textObject


86
87
88
# File 'lib/diaspora/message_renderer.rb', line 86

def make_mentions_plain_text
  @message = Diaspora::Mentionable.format message, [], plain_text: true
end

#markdownifyObject


59
60
61
62
63
64
# File 'lib/diaspora/message_renderer.rb', line 59

def markdownify
  renderer = Diaspora::Markdownify::HTML.new options[:markdown_render_options]
  markdown = Redcarpet::Markdown.new renderer, options[:markdown_options]

  @message = markdown.render message
end

#process_newlinesObject

In very clear cases, let newlines become <br /> tags Grabbed from Github flavored Markdown


68
69
70
71
72
# File 'lib/diaspora/message_renderer.rb', line 68

def process_newlines
  message.gsub(/^[\w\<][^\n]*\n+/) do |x|
    x =~ /\n{2}/ ? x : (x.strip!; x << " \n")
  end
end

#render_mentionsObject


74
75
76
77
78
79
80
81
82
83
84
# File 'lib/diaspora/message_renderer.rb', line 74

def render_mentions
  unless options[:disable_hovercards] || options[:mentioned_people].empty?
    @message = Diaspora::Mentionable.format message, options[:mentioned_people]
  end

  if options[:disable_hovercards] || options[:link_all_mentions]
    @message = Diaspora::Mentionable.filter_for_aspects message, nil
  else
    make_mentions_plain_text
  end
end

#render_tagsObject


90
91
92
# File 'lib/diaspora/message_renderer.rb', line 90

def render_tags
  @message = Diaspora::Taggable.format_tags message, no_escape: !options[:escape_tags]
end

#squishObject


26
27
28
# File 'lib/diaspora/message_renderer.rb', line 26

def squish
  @message = message.squish if options[:squish]
end

#strip_markdownObject


54
55
56
57
# File 'lib/diaspora/message_renderer.rb', line 54

def strip_markdown
  renderer = Redcarpet::Markdown.new Redcarpet::Render::StripDown, options[:markdown_options]
  @message = renderer.render(message).strip
end