Welcome to mirror list, hosted at ThFree Co, Russian Federation.

message_renderer.rb « diaspora « lib - github.com/diaspora/diaspora.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 2f9ef423f5cf8e4baccba987026e3e4a0684ae24 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
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
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
# frozen_string_literal: true

module Diaspora
  # Takes a raw message text and converts it to
  # various desired target formats, respecting
  # all possible formatting options supported
  # by Diaspora
  class MessageRenderer
    class Processor
      class << self
        private :new

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

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

      attr_reader :message, :options

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

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

      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

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

      def strip_markdown
        # Footnotes are not supported in text-only outputs (mail, crossposts etc)
        stripdown_options = options[:markdown_options].except(:footnotes)
        renderer = Redcarpet::Markdown.new Redcarpet::Render::StripDown, stripdown_options
        @message = renderer.render(message).strip
      end

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

        @message = markdown.render message
      end

      # In very clear cases, let newlines become <br /> tags
      # Grabbed from Github flavored Markdown
      def process_newlines
        message.gsub(/^[\w\<][^\n]*\n+/) do |x|
          x =~ /\n{2}/ ? x : (x.strip!; x << " \n")
        end
      end

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

        if options[:disable_hovercards]
          @message = Diaspora::Mentionable.filter_people(message, [], absolute_links: true)
        else
          make_mentions_plain_text
        end
      end

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

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

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

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

      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
    end

    DEFAULTS = {mentioned_people:        [],
                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,
                  footnotes:           true,
                  tables:              true,
                  no_intra_emphasis:   true
                },
                markdown_render_options: {
                  filter_html:     true,
                  hard_wrap:       true,
                  safe_links_only: true
                }}.freeze

    delegate :empty?, :blank?, :present?, to: :raw

    # @param [String] text Raw input text
    # @param [Hash] opts Global options affecting output
    # @option opts [Array<Person>] :mentioned_people ([]) List of people
    #   allowed to mention
    # @option opts [Boolean] :disable_hovercards (true) Render all mentions
    #   as absolute profile links. This ignores :mentioned_people
    # @option opts [#to_i, Boolean] :truncate (false) Truncate message to
    #   the specified length
    # @option opts [String] :append (nil) Append text to the end of
    #   the (truncated) message, counts into truncation length
    # @option opts [String] :append_after_truncate (nil) Append text to the end
    #   of the (truncated) message, doesn't count into the truncation length
    # @option opts [Boolean] :squish (false) Squish the message, that is
    #   remove all surrounding and consecutive whitespace
    # @option opts [Boolean] :escape (true) Escape HTML relevant characters
    #   in the message. Note that his option is ignored in the plaintext
    #   renderers.
    # @option opts [Boolean] :escape_tags (false) Escape HTML relevant
    #   characters in tags when rendering them
    # @option opts [Hash] :markdown_options Override default options passed
    #   to Redcarpet
    # @option opts [Hash] :markdown_render_options Override default options
    #   passed to the Redcarpet renderer
    def initialize(text, opts={})
      @text = text
      @options = DEFAULTS.deep_merge opts
    end

    # @param [Hash] opts Override global output options, see {#initialize}
    def plain_text opts={}
      process(opts) {
        make_mentions_plain_text
        diaspora_links
        squish
        append_and_truncate
      }
    end

    # @param [Hash] opts Override global output options, see {#initialize}
    def plain_text_without_markdown opts={}
      process(opts) {
        make_mentions_plain_text
        diaspora_links
        strip_markdown
        squish
        append_and_truncate
      }
    end

    # @param [Hash] opts Override global output options, see {#initialize}
    def plain_text_for_json opts={}
      process(opts) {
        normalize
        diaspora_links
        camo_urls if AppConfig.privacy.camo.proxy_markdown_images?
      }
    end

    # @param [Hash] opts Override global output options, see {#initialize}
    def html opts={}
      process(opts) {
        escape
        normalize
        diaspora_links
        render_mentions
        render_tags
        squish
        append_and_truncate
      }.html_safe # rubocop:disable Rails/OutputSafety
    end

    # @param [Hash] opts Override global output options, see {#initialize}
    def markdownified opts={}
      process(opts) {
        process_newlines
        normalize
        diaspora_links
        camo_urls if AppConfig.privacy.camo.proxy_markdown_images?
        markdownify
        render_mentions
        render_tags
        squish
        append_and_truncate
      }.html_safe # rubocop:disable Rails/OutputSafety
    end

    def markdownified_for_mail
      process(disable_hovercards: true) {
        process_newlines
        normalize
        diaspora_links
        camo_urls if AppConfig.privacy.camo.proxy_markdown_images?
        render_mentions
        markdownify(Diaspora::Markdownify::Email)
        squish
        append_and_truncate
      }.html_safe # rubocop:disable Rails/OutputSafety
    end

    # Get a short summary of the message
    # @param [Hash] opts Additional options
    # @option opts [Integer] :length (70) Truncate the title to
    #   this length. If not given defaults to 70.
    def title opts={}
      # Setext-style header
      heading = if /\A(?<setext_content>.{1,200})\n(?:={1,200}|-{1,200})(?:\r?\n|$)/ =~ @text.lstrip
                  setext_content
                # Atx-style header
                elsif /\A\#{1,6}\s+(?<atx_content>.{1,200}?)(?:\s+#+)?(?:\r?\n|$)/ =~ @text.lstrip
                  atx_content
                end

      heading &&= self.class.new(heading).plain_text_without_markdown

      if heading
        heading.truncate opts.fetch(:length, 70)
      else
        plain_text_without_markdown squish: true, truncate: opts.fetch(:length, 70)
      end
    end

    # Extracts all the urls from the raw message and return them in the form of a string
    # Different URLs are seperated with a space
    def urls
      @urls ||= Twitter::Extractor.extract_urls(plain_text_without_markdown).map {|url|
        Addressable::URI.parse(url).normalize.to_s
      }
    end

    def raw
      @text
    end

    def to_s
      plain_text
    end

    private

    def process(opts, &block)
      Processor.process(@text, @options.deep_merge(opts), &block)
    end
  end
end