Skip to content

Commit

Permalink
Use the redcarpet gem to format petition emails
Browse files Browse the repository at this point in the history
This allows the moderators more flexibility when formatting other
parliamentary business emails for maximum effect as often they
are missed by people viewing the petition.
  • Loading branch information
pixeltrix committed Nov 25, 2017
1 parent 81c57cc commit ba4025a
Show file tree
Hide file tree
Showing 16 changed files with 150 additions and 11 deletions.
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ gem 'logstash-logger'
gem 'jbuilder'
gem 'paperclip'
gem 'maxminddb'
gem 'redcarpet'

gem 'aws-sdk', '~> 2.0'

Expand Down
2 changes: 2 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,7 @@ GEM
rake (>= 0.8.7)
thor (>= 0.18.1, < 2.0)
rake (12.3.0)
redcarpet (3.4.0)
rb-fsevent (0.10.2)
rb-inotify (0.9.10)
ffi (>= 0.5.0, < 2)
Expand Down Expand Up @@ -337,6 +338,7 @@ DEPENDENCIES
puma
rails (= 4.2.10)
rake
redcarpet
rspec-rails
sass-rails (~> 5.0)
shoulda-matchers
Expand Down
66 changes: 66 additions & 0 deletions app/assets/stylesheets/petitions/views/_petition-show.scss
Original file line number Diff line number Diff line change
Expand Up @@ -73,3 +73,69 @@
margin-top: 10px;
}
}

.about-item-other-business {
ul {
list-style-type: disc;
padding-left: 20px;
}

ol {
list-style-type: decimal;
padding-left: 20px;

@include ie-lte(7) {
padding-left: 28px;
}
}

ul, ol {
margin-top: em(5, 16);
margin-bottom: em(10, 16);

@include media(tablet) {
margin-top: em(10, 16);
margin-bottom: em(10, 16);
}

& li {
margin: 0 0 em(2, 16);

@include media(tablet) {
margin: 0 0 em(2, 19);
}
}
}

h3 {
margin: em(15, 16) 0 em(5, 16);

@include media(tablet) {
margin: em(15, 19) 0 em(5, 19);
}
}

p {
margin: 0 0 em(10, 16);

@include media(tablet) {
margin: 0 0 em(10, 19);
}
}

.panel-indent {
margin: 0;
}

hr {
height: 0px;
border: none;
border-top: 1px solid $grey-2;

margin: em(15, 16) 0;

@include media(tablet) {
margin: em(15, 19) 0;
}
}
}
47 changes: 47 additions & 0 deletions app/helpers/markdown_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
require 'redcarpet/render_strip'

module MarkdownHelper
HTML_DEFAULTS = {
escape_html: false, filter_html: false,
hard_wrap: true, xhtml: true, safe_links_only: true,
no_styles: true, no_images: true, no_links: false,
with_toc_data: false, prettify: false, link_attributes: {}
}

PARSER_DEFAULTS = {
no_intra_emphasis: true, tables: false, fenced_code_blocks: false,
autolink: true, disable_indented_code_blocks: false, strikethrough: true,
lax_spacing: false, space_after_headers: true, superscript: true,
underline: false, highlight: false, quote: false, footnotes: false
}

def markdown_to_html(markup, options = {})
markdown_parser(html_renderer(options), options).render(markup).html_safe
end

def markdown_to_text(markup, options = {})
markdown_parser(text_renderer, options).render(markup).html_safe
end

private

def html_renderer(options)
Redcarpet::Render::HTML.new(options_for_renderer(options))
end

def text_renderer
Redcarpet::Render::StripDown.new
end

def markdown_parser(renderer, options)
Redcarpet::Markdown.new(renderer, options_for_parser(options))
end

def options_for_parser(options)
PARSER_DEFAULTS.merge(options.slice(*PARSER_DEFAULTS.keys))
end

def options_for_renderer(options)
HTML_DEFAULTS.merge(options.slice(*HTML_DEFAULTS.keys))
end
end
2 changes: 1 addition & 1 deletion app/mailers/application_mailer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ class ApplicationMailer < ActionMailer::Base
default_url_options[:protocol] = Site.email_protocol
default from: ->(email){ Site.email_from }

helper :date_time, :rejection, :auto_link
helper :date_time, :rejection, :auto_link, :markdown

layout 'default_mail'
end
2 changes: 1 addition & 1 deletion app/views/archived/petition_mailer/email_creator.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<p>You recently created the petition “<%= @petition.action %>”:<br />
<%= link_to nil, archived_petition_url(@petition) %></p>

<%= auto_link(simple_format(h(@email.body))) %>
<%= markdown_to_html(@email.body) %>

<p>Thanks,<br />
<%= t("petitions.emails.signoff_prefix") %><br />
Expand Down
2 changes: 1 addition & 1 deletion app/views/archived/petition_mailer/email_creator.text.erb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ Dear <%= @signature.name %>,
You recently created the petition “<%= @petition.action %>”:
<%= archived_petition_url(@petition) %>

<%= @email.body %>
<%= markdown_to_text(@email.body) %>

Thanks,
<%= t("petitions.emails.signoff_prefix") %>
Expand Down
2 changes: 1 addition & 1 deletion app/views/archived/petition_mailer/email_signer.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<p>You recently signed the petition “<%= @petition.action %>”:<br />
<%= link_to nil, archived_petition_url(@petition) %></p>

<%= auto_link(simple_format(h(@email.body))) %>
<%= markdown_to_html(@email.body) %>

<p>Thanks,<br />
<%= t("petitions.emails.signoff_prefix") %><br />
Expand Down
2 changes: 1 addition & 1 deletion app/views/archived/petition_mailer/email_signer.text.erb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ Dear <%= @signature.name %>,
You recently signed the petition “<%= @petition.action %>”:
<%= archived_petition_url(@petition) %>

<%= @email.body %>
<%= markdown_to_text(@email.body) %>

Thanks,
<%= t("petitions.emails.signoff_prefix") %>
Expand Down
2 changes: 1 addition & 1 deletion app/views/archived/petitions/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@
<details>
<summary><span class="summary"><%= email.subject %></span></summary>
<div class="panel-indent panel-no-border">
<%= auto_link(simple_format(h(email.body)), html: { rel: 'nofollow' }) %>
<%= markdown_to_html(email.body, link_attributes: { rel: 'nofollow' }) %>
</div>
</details>
<% end %>
Expand Down
2 changes: 1 addition & 1 deletion app/views/petition_mailer/email_creator.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<p>You recently created the petition “<%= @petition.action %>”:<br />
<%= link_to nil, petition_url(@petition) %></p>

<%= auto_link(simple_format(h(@email.body))) %>
<%= markdown_to_html(@email.body) %>

<p>Thanks,<br />
<%= t("petitions.emails.signoff_prefix") %><br />
Expand Down
2 changes: 1 addition & 1 deletion app/views/petition_mailer/email_creator.text.erb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ Dear <%= @signature.name %>,
You recently created the petition “<%= @petition.action %>”:
<%= petition_url(@petition) %>

<%= @email.body %>
<%= markdown_to_text(@email.body) %>

Thanks,
<%= t("petitions.emails.signoff_prefix") %>
Expand Down
2 changes: 1 addition & 1 deletion app/views/petition_mailer/email_signer.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<p>You recently signed the petition “<%= @petition.action %>”:<br />
<%= link_to nil, petition_url(@petition) %></p>

<%= auto_link(simple_format(h(@email.body))) %>
<%= markdown_to_html(@email.body) %>

<p>Thanks,<br />
<%= t("petitions.emails.signoff_prefix") %><br />
Expand Down
2 changes: 1 addition & 1 deletion app/views/petition_mailer/email_signer.text.erb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ Dear <%= @signature.name %>,
You recently signed the petition “<%= @petition.action %>”:
<%= petition_url(@petition) %>

<%= @email.body %>
<%= markdown_to_text(@email.body) %>

Thanks,
<%= t("petitions.emails.signoff_prefix") %>
Expand Down
2 changes: 1 addition & 1 deletion app/views/petitions/_other_business_details.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<details>
<summary><span class="summary"><%= email.subject %></span></summary>
<div class="panel-indent panel-no-border">
<%= auto_link(simple_format(h(email.body)), html: { rel: 'nofollow' }) %>
<%= markdown_to_html(email.body, link_attributes: { rel: 'nofollow' }) %>
</div>
</details>
<% end %>
Expand Down
23 changes: 23 additions & 0 deletions spec/helpers/markdown_helper_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
require 'rails_helper'

RSpec.describe MarkdownHelper, type: :helper do
describe "#markdown_to_html" do
it "converts markdown to html" do
expect(helper.markdown_to_html("## Petitions: UK Government and Parliament")).to eq(%[<h2>Petitions: UK Government and Parliament</h2>\n])
end

it "autolinks urls" do
expect(helper.markdown_to_html("www.example.com")).to eq(%[<p><a href="http://www.example.com">www.example.com</a></p>\n])
end
end

describe "#markdown_to_text" do
it "converts markdown to text" do
expect(helper.markdown_to_text("## Petitions: UK Government and Parliament")).to eq(%[Petitions: UK Government and Parliament\n])
end

it "autolinks urls" do
expect(helper.markdown_to_text("www.example.com")).to eq(%[www.example.com (http://www.example.com)\n])
end
end
end

0 comments on commit ba4025a

Please sign in to comment.