about summary refs log tree commit diff
path: root/app/lib/plain_text_formatter.rb
diff options
context:
space:
mode:
authorStarfall <us@starfall.systems>2022-03-30 12:33:18 -0500
committerStarfall <us@starfall.systems>2022-03-30 12:33:18 -0500
commitf7491de676298b8f78084c00f0026f8cf36d92fc (patch)
tree0ac29d1598efeb2a0de9bd1b54ae7590e88479da /app/lib/plain_text_formatter.rb
parentf37056e6c351a08d09c3986586cc7d27bdea85ab (diff)
parent363773d0e9ffa9f4efc564603327f225193a2bf1 (diff)
Update to Mastodon 2.5.0
Merge remote-tracking branch 'glitch/main'
Diffstat (limited to 'app/lib/plain_text_formatter.rb')
-rw-r--r--app/lib/plain_text_formatter.rb30
1 files changed, 30 insertions, 0 deletions
diff --git a/app/lib/plain_text_formatter.rb b/app/lib/plain_text_formatter.rb
new file mode 100644
index 000000000..08aa29696
--- /dev/null
+++ b/app/lib/plain_text_formatter.rb
@@ -0,0 +1,30 @@
+# frozen_string_literal: true
+
+class PlainTextFormatter
+  include ActionView::Helpers::TextHelper
+
+  NEWLINE_TAGS_RE = /(<br \/>|<br>|<\/p>)+/.freeze
+
+  attr_reader :text, :local
+
+  alias local? local
+
+  def initialize(text, local)
+    @text  = text
+    @local = local
+  end
+
+  def to_s
+    if local?
+      text
+    else
+      strip_tags(insert_newlines).chomp
+    end
+  end
+
+  private
+
+  def insert_newlines
+    text.gsub(NEWLINE_TAGS_RE) { |match| "#{match}\n" }
+  end
+end