Class: Diaspora::MessageRenderer::Processor

Inherits:
Object
  • Object
show all
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

Returns a new instance of Processor.


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

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

Instance Attribute Details

#messageObject (readonly)

Returns the value of attribute message


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

def message
  @message
end

#optionsObject (readonly)

Returns the value of attribute options


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

def options
  @options
end

Class Method Details

.normalize(message) ⇒ Object


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

def normalize message
  message.gsub(/[\u202a\u202b]#[\u200e\u200f\u202d\u202e](\S+)\u202c/u, "#\\1")
end

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


13
14
15
16
17
18
# File 'lib/diaspora/message_renderer.rb', line 13

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


36
37
38
39
40
41
42
43
44
# File 'lib/diaspora/message_renderer.rb', line 36

def append_and_truncate
  if options[:truncate]
    # TODO: Remove .dup when upgrading to Rails 6.x.
    @message = @message.truncate(options[:truncate] - options[:append].to_s.size).dup
  end

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

#camo_urlsObject


92
93
94
# File 'lib/diaspora/message_renderer.rb', line 92

def camo_urls
  @message = Diaspora::Camo.from_markdown(@message)
end

100
101
102
103
104
105
# File 'lib/diaspora/message_renderer.rb', line 100

def diaspora_links
  @message = @message.gsub(DiasporaFederation::Federation::DiasporaUrlParser::DIASPORA_URL_REGEX) {|match_str|
    guid = Regexp.last_match(3)
    Regexp.last_match(2) == "post" && Post.exists?(guid: guid) ? AppConfig.url_to("/posts/#{guid}") : match_str
  }
end

#escapeObject


46
47
48
49
50
# File 'lib/diaspora/message_renderer.rb', line 46

def escape
  if options[:escape]
    @message = ERB::Util.html_escape_once message
  end
end

#make_mentions_plain_textObject


84
85
86
# File 'lib/diaspora/message_renderer.rb', line 84

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

#markdownifyObject


57
58
59
60
61
62
# File 'lib/diaspora/message_renderer.rb', line 57

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

  @message = markdown.render message
end

#normalizeObject


96
97
98
# File 'lib/diaspora/message_renderer.rb', line 96

def normalize
  @message = self.class.normalize(@message)
end

#process_newlinesObject

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


66
67
68
69
70
# File 'lib/diaspora/message_renderer.rb', line 66

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

#render_mentionsObject


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

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_people message, []
  else
    make_mentions_plain_text
  end
end

#render_tagsObject


88
89
90
# File 'lib/diaspora/message_renderer.rb', line 88

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

#squishObject


32
33
34
# File 'lib/diaspora/message_renderer.rb', line 32

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

#strip_markdownObject


52
53
54
55
# File 'lib/diaspora/message_renderer.rb', line 52

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