FEATURE: displays events in mails

FEATURE: displays events in mails

diff --git a/plugin.rb b/plugin.rb
index b328f24..bde348d 100644
--- a/plugin.rb
+++ b/plugin.rb
@@ -449,5 +449,26 @@ after_initialize do
     class Jobs::ExportCsvFile
       prepend ExportPostEventCsvReportExtension
     end
+
+    on(:reduce_cooked) do |fragment, post|
+      if SiteSetting.discourse_post_event_enabled
+        fragment.css('.discourse-post-event').each do |event_node|
+          starts_at = event_node['data-start']
+          ends_at = event_node['data-end']
+          dates = "#{starts_at} (UTC)"
+          if ends_at
+            dates = "#{dates} → #{ends_at} (UTC)"
+          end
+
+          event_name = event_node['data-name'] || post.topic.title
+          event_node.replace <<~TXT
+            <div style='border:1px solid #dedede'>
+              <p><a href="#{Discourse.base_url}#{post.url}">#{event_name}</a></p>
+              <p>#{dates}</p>
+            </div>
+          TXT
+        end
+      end
+    end
   end
 end
diff --git a/spec/lib/pretty_text_spec.rb b/spec/lib/pretty_text_spec.rb
new file mode 100644
index 0000000..6ea09c1
--- /dev/null
+++ b/spec/lib/pretty_text_spec.rb
@@ -0,0 +1,87 @@
+# frozen_string_literal: true
+
+require 'rails_helper'
+
+describe PrettyText do
+  before do
+    freeze_time
+  end
+
+  describe 'An event is displayed in an email' do
+    before do
+      SiteSetting.calendar_enabled = true
+      SiteSetting.discourse_post_event_enabled = true
+    end
+
+    let(:user) { Fabricate(:user, admin: true) }
+
+    context 'The event has no name' do
+      let(:post_1) {
+        create_post(
+          user: user,
+          raw: <<~TXT.strip
+            [event start="2018-05-08"]
+            [/event]
+          TXT
+        )
+      }
+
+      it 'displays the topic title' do
+        cooked = PrettyText.cook(post_1.raw)
+
+        expect(PrettyText.format_for_email(cooked, post_1)).to match_html(<<~HTML)
+          <div style='border:1px solid #dedede'>
+            <p><a href="#{Discourse.base_url}#{post_1.url}">#{post_1.topic.title}</a></p>
+            <p>2018-05-08 (UTC)</p>
+          </div>
+        HTML
+      end
+    end
+
+    context 'The event has a name' do
+      let(:post_1) {
+        create_post(
+          user: user,
+          raw: <<~TXT.strip
+            [event start="2018-05-08" name="Pancakes event"]
+            [/event]
+          TXT
+        )
+      }
+
+      it 'displays the topic title' do
+        cooked = PrettyText.cook(post_1.raw)
+
+        expect(PrettyText.format_for_email(cooked, post_1)).to match_html(<<~HTML)
+          <div style='border:1px solid #dedede'>
+            <p><a href="#{Discourse.base_url}#{post_1.url}">Pancakes event</a></p>
+            <p>2018-05-08 (UTC)</p>
+          </div>
+        HTML
+      end
+    end
+
+    context 'The event has an end date' do
+      let(:post_1) {
+        create_post(
+          user: user,
+          raw: <<~TXT.strip
+            [event start="2018-05-08" end="2018-06-22"]
+            [/event]
+          TXT
+        )
+      }
+
+      it 'displays the topic title' do
+        cooked = PrettyText.cook(post_1.raw)
+
+        expect(PrettyText.format_for_email(cooked, post_1)).to match_html(<<~HTML)
+          <div style='border:1px solid #dedede'>
+            <p><a href="#{Discourse.base_url}#{post_1.url}">#{post_1.topic.title}</a></p>
+            <p>2018-05-08 (UTC) → 2018-06-22 (UTC)</p>
+          </div>
+        HTML
+      end
+    end
+  end
+end

GitHub sha: c8438b5f

Shouldn’t this be a class and use colours from our stylesheets with variables?

that’s the first time I style a mail to be honest and I thought inline was the way to go

Oh in mail I believe you have to inline it, yes. I thought this was for a post.