Class: Diaspora::MessageRenderer

Inherits:
Object
  • Object
show all
Defined in:
lib/diaspora/message_renderer.rb

Overview

Takes a raw message text and converts it to various desired target formats, respecting all possible formatting options supported by Diaspora

Defined Under Namespace

Classes: Processor

Constant Summary collapse

DEFAULTS =
{mentioned_people: [],
link_all_mentions: false,
disable_hovercards: false,
truncate: false,
append: nil,
append_after_truncate: nil,
squish: false,
escape: true,
escape_tags: false,
markdown_options: {
  autolink: true,
  fenced_code_blocks:  true,
  space_after_headers: true,
  strikethrough: true,
  tables: true,
  no_intra_emphasis: true,
},
markdown_render_options: {
  filter_html: true,
  hard_wrap: true,
  safe_links_only: true
}}.freeze

Instance Method Summary collapse

Constructor Details

#initialize(raw_message, opts = {}) ⇒ MessageRenderer

Returns a new instance of MessageRenderer

Options Hash (opts):

  • :mentioned_people (Array<Person>) — default: []

    List of people allowed to mention

  • :link_all_mentions (Boolean) — default: false

    Whether to link all mentions. This makes plain links to profiles for people not in :mentioned_people

  • :disable_hovercards (Boolean) — default: true

    Render all mentions as profile links. This implies :link_all_mentions and ignores :mentioned_people

  • :truncate (#to_i, Boolean) — default: false

    Truncate message to the specified length

  • :append (String) — default: nil

    Append text to the end of the (truncated) message, counts into truncation length

  • :append_after_truncate (String) — default: nil

    Append text to the end of the (truncated) message, doesn't count into the truncation length

  • :squish (Boolean) — default: false

    Squish the message, that is remove all surrounding and consecutive whitespace

  • :escape (Boolean) — default: true

    Escape HTML relevant characters in the message. Note that his option is ignored in the plaintext renderers.

  • :escape_tags (Boolean) — default: false

    Escape HTML relevant characters in tags when rendering them

  • :markdown_options (Hash)

    Override default options passed to Redcarpet

  • :markdown_render_options (Hash)

    Override default options passed to the Redcarpet renderer


147
148
149
150
# File 'lib/diaspora/message_renderer.rb', line 147

def initialize raw_message, opts={}
  @raw_message = raw_message
  @options = DEFAULTS.deep_merge opts
end

Instance Method Details

#html(opts = {}) ⇒ Object


172
173
174
175
176
177
178
179
180
# File 'lib/diaspora/message_renderer.rb', line 172

def html opts={}
  process(opts) {
    escape
    render_mentions
    render_tags
    squish
    append_and_truncate
  }.html_safe
end

#markdownified(opts = {}) ⇒ Object


183
184
185
186
187
188
189
190
191
192
# File 'lib/diaspora/message_renderer.rb', line 183

def markdownified opts={}
  process(opts) {
    process_newlines
    markdownify
    render_mentions
    render_tags
    squish
    append_and_truncate
  }.html_safe
end

#plain_text(opts = {}) ⇒ Object


153
154
155
156
157
158
159
# File 'lib/diaspora/message_renderer.rb', line 153

def plain_text opts={}
  process(opts) {
    make_mentions_plain_text
    squish
    append_and_truncate
  }
end

#plain_text_without_markdown(opts = {}) ⇒ Object


162
163
164
165
166
167
168
169
# File 'lib/diaspora/message_renderer.rb', line 162

def plain_text_without_markdown opts={}
  process(opts) {
    make_mentions_plain_text
    strip_markdown
    squish
    append_and_truncate
  }
end

#rawObject


219
220
221
# File 'lib/diaspora/message_renderer.rb', line 219

def raw
  @raw_message
end

#title(opts = {}) ⇒ Object

Get a short summary of the message

Options Hash (opts):

  • :length (Integer) — default: 20 | first heading

    Truncate the title to this length. If not given defaults to 20 and to not truncate if a heading is found.


199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
# File 'lib/diaspora/message_renderer.rb', line 199

def title opts={}
  # Setext-style header
  heading = if /\A(?<setext_content>.{1,200})\n(?:={1,200}|-{1,200})(?:\r?\n|$)/ =~ @raw_message.lstrip
    setext_content
  # Atx-style header
  elsif /\A\#{1,6}\s+(?<atx_content>.{1,200}?)(?:\s+#+)?(?:\r?\n|$)/ =~ @raw_message.lstrip
    atx_content
  end

  heading &&= heading.strip

  if heading && opts[:length]
    heading.truncate opts[:length]
  elsif heading
    heading
  else
    plain_text_without_markdown squish: true, truncate: opts.fetch(:length, 20)
  end
end

#to_sObject


223
224
225
# File 'lib/diaspora/message_renderer.rb', line 223

def to_s
  plain_text
end