Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ gem "govuk-components", "~> 6"
gem "govuk_design_system_formbuilder", "~> 6"

# Our own custom markdown renderer
gem "govuk-forms-markdown", github: "govuk-forms/govuk-forms-markdown", tag: "0.9.0"
gem "govuk-forms-markdown", github: "govuk-forms/govuk-forms-markdown", tag: "0.10.0"

# For compiling our frontend assets
gem "vite_rails"
Expand Down
8 changes: 4 additions & 4 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@ GIT

GIT
remote: https://github.com/govuk-forms/govuk-forms-markdown.git
revision: 84c6b2296accca578e7b77e7d1730472d9c9e7af
tag: 0.9.0
revision: 3ca93546da82c3d39fb087e0e94891bafb2f9142
tag: 0.10.0
specs:
govuk-forms-markdown (0.9.0)
govuk-forms-markdown (0.10.0)
redcarpet (~> 3.6)

GEM
Expand Down Expand Up @@ -898,7 +898,7 @@ CHECKSUMS
google-protobuf (4.35.0-x86_64-linux-musl) sha256=be0218520d77b2aee898b363514b03819f6f63f9c041ae0d0d79b4ce5247bffd
googleapis-common-protos-types (1.23.0) sha256=992e740a523794d9fc5f29a504465d8fc737aaa16c930fe7228e3346860faf0a
govuk-components (6.3.0) sha256=9e10aba5f88f9bead1ac792814e3551aadf17813080a74a17d69c6e0446bd88d
govuk-forms-markdown (0.9.0)
govuk-forms-markdown (0.10.0)
govuk_design_system_formbuilder (6.2.0) sha256=d8cd1543776b7b15ba3d1774f16e3b8458c59446b0cad56ccbbc1e517a575e93
govuk_notify_rails (3.0.0) sha256=e0e1b8b0a7c47f90963034f290fb2982652aa8051a733c25c178680d44f2d7aa
hana (1.3.7) sha256=5425db42d651fea08859811c29d20446f16af196308162894db208cac5ce9b0d
Expand Down
9 changes: 9 additions & 0 deletions app/helpers/email_format_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,20 @@ def convert_newlines_to_html(text)
text.gsub("\n", "<br/>")
end

def convert_newlines_to_markdown(text)
text.gsub("\n", " \n")
end

def normalize_whitespace_and_convert_to_html(text)
output = normalize_whitespace(text)
convert_newlines_to_html(output)
end

def normalize_whitespace_and_convert_to_markdown(text)
output = normalize_whitespace(text)
convert_newlines_to_markdown(output)
end

def markdown_to_html(markdown)
HtmlMarkdownSanitizer.new.render_scrubbed_markdown(markdown, allow_headings: false, for_email: true)
end
Expand Down
11 changes: 11 additions & 0 deletions app/lib/markdown_handler.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
module MarkdownHandler
def self.erb
@erb ||= ActionView::Template.registered_template_handler(:erb)
end

def self.call(template)
erb.call(template)
end
end

ActionView::Template.register_template_handler :md, MarkdownHandler
61 changes: 15 additions & 46 deletions app/lib/ses_email_formatter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,57 +12,42 @@ def initialize(submission_reference:, steps:, confirmation_email:)
@confirmation_email = confirmation_email
end

def build_question_answers_section_html(heading_level: 3)
def build_question_answers_section_markdown(heading_level: 3)
@steps.map { |step|
[prep_question_title_html(step, heading_level),
prep_answer_text_html(step)].join
}.join(H_RULE)
end

def build_question_answers_section_plain_text
@steps.map { |step|
[prep_question_title_plain_text(step),
prep_answer_text_plain_text(step)].join("\n\n")
[prep_question_title_markdown(step, heading_level),
prep_answer_text_markdown(step)].join("\n\n")
}.join(H_RULE_PLAIN_TEXT)
end

private

def prep_question_title_html(step, heading_level)
def prep_question_title_markdown(step, heading_level)
case heading_level
when 3
"<h3 style=\"font-size: 21px; line-height: 25px; font-weight: bold; color: #0B0C0C;\">#{prep_question_title_plain_text(step)}</h3>"
"### #{step.question.question_text}"
when 4
"<h4 style=\"font-size: 19px; line-height: 25px; font-weight: bold; color: #0B0C0C;\">#{prep_question_title_plain_text(step)}</h4>"
"#### #{step.question.question_text}"
else
raise FormattingError, "unsupported heading level: #{heading_level}"
end
end

def prep_answer_text_html(step)
def prep_answer_text_markdown(step)
if step.is_selection_with_none_of_the_above_answer?
prep_html_none_of_the_above_answer_text(step)
prep_none_of_the_above_answer_text_markdown(step)
else
"<p>#{convert_newlines_to_html(prep_answer_text(step))}</p>"
prep_answer_text(step)
end
rescue StandardError
raise FormattingError, "could not format answer for question step #{step.id}"
end

def prep_html_none_of_the_above_answer_text(step)
def prep_none_of_the_above_answer_text_markdown(step)
[
"<p>#{convert_newlines_to_html(prep_answer_text(step))}</p>",
"<h4>#{convert_newlines_to_html(prep_none_of_the_above_question_text(step))}</h4>",
"<p>#{convert_newlines_to_html(prep_none_of_the_above_answer_text(step))}</p>",
]
end

def prep_question_title_plain_text(step)
step.question.question_text
end

def prep_none_of_the_above_question_text(step)
step.question.none_of_the_above_question_text
prep_answer_text(step),
"#### #{step.question.none_of_the_above_question_text}",
prep_none_of_the_above_answer_markdown(step),
].join("\n\n")
end

def prep_answer_text(step)
Expand All @@ -75,7 +60,7 @@ def prep_answer_text(step)
raise FormattingError, "could not format answer for question step #{step.id}"
end

def prep_none_of_the_above_answer_text(step)
def prep_none_of_the_above_answer_markdown(step)
answer = step.question.none_of_the_above_answer

return skipped_question_text if answer.blank?
Expand All @@ -85,22 +70,6 @@ def prep_none_of_the_above_answer_text(step)
raise FormattingError, "could not format none of the above answer for question step #{step.id}"
end

def prep_answer_text_plain_text(step)
if step.is_selection_with_none_of_the_above_answer?
prep_plain_text_none_of_the_above_answer_text(step)
else
prep_answer_text(step)
end
end

def prep_plain_text_none_of_the_above_answer_text(step)
[
prep_answer_text(step),
prep_none_of_the_above_question_text(step),
prep_none_of_the_above_answer_text(step),
].join("\n\n")
end

def sanitize(text)
text
.then { normalize_whitespace _1 }
Expand Down
8 changes: 2 additions & 6 deletions app/models/submission.rb
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,8 @@ def mode_object
Mode.new(mode)
end

def answer_content_for_email_html(heading_level:, locale: :en, confirmation_email: false)
ses_email_formatter(locale, confirmation_email).build_question_answers_section_html(heading_level:)
end

def answer_content_for_email_plain_text(locale: :en, confirmation_email: false)
ses_email_formatter(locale, confirmation_email).build_question_answers_section_plain_text
def answer_content_for_email_markdown(heading_level:, locale: :en, confirmation_email: false)
ses_email_formatter(locale, confirmation_email).build_question_answers_section_markdown(heading_level:)
end

private
Expand Down
45 changes: 45 additions & 0 deletions app/views/aws_ses_form_submission_mailer/_content.md.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<% if @submission.preview? %>
<%= I18n.t("mailer.submission.preview") %>

<% end %>
<%= I18n.t("mailer.form_name", form_name: @submission.form.name) %>

<%= I18n.t("mailer.submission.time", time: format_time(@submission.submission_time), date: format_date(@submission.submission_time) ) %>

<% if @welsh_submission %>
<%= I18n.t("mailer.submission.welsh_submission") %>

<% end %>
<%= I18n.t("mailer.submission.reference", submission_reference: @submission.reference) %>

<% if @submission.payment_url.present? %>
<%= I18n.t("mailer.submission.payment") %>

<% end %>
^<%= I18n.t("mailer.check_before_using") %>^

---

## <%= I18n.t("mailer.submission.answers_submitted") %>

<% if @csv_filename.present? %>
<%= I18n.t("mailer.submission.csv_file", filename: @csv_filename) %>

<% end %>
<% if @json_filename.present? %>
<%= I18n.t("mailer.submission.json_file", filename: @json_filename) %>

<% end %>
<%= @submission.answer_content_for_email_markdown(heading_level: 3) %>

---

^^^

## <%= I18n.t("mailer.cannot_reply.heading") %>

<%= I18n.t("mailer.cannot_reply.contact_form_filler_markdown") %>

<%= I18n.t("mailer.cannot_reply.contact_forms_team_markdown") %>

^^^
Original file line number Diff line number Diff line change
@@ -1,54 +1 @@
<% if @submission.preview? %>
<p>
<%= I18n.t("mailer.submission.preview") %>
</p>
<% end %>

<p><%= I18n.t("mailer.form_name", form_name: @submission.form.name) %></p>

<p>
<%= I18n.t("mailer.submission.time", time: format_time(@submission.submission_time), date: format_date(@submission.submission_time) ) %>
</p>
<p>
<% if @welsh_submission %>
<%= I18n.t("mailer.submission.welsh_submission") %>
<% end %>
</p>
<p>
<%= I18n.t("mailer.submission.reference", submission_reference: @submission.reference) %>
</p>

<% if @submission.payment_url.present? %>
<p>
<%= I18n.t("mailer.submission.payment") %>
</p>
<% end %>

<p style="margin: 0 0 20px 0; border-left: 10px solid #B1B4B6; padding: 15px;">
<%= I18n.t("mailer.check_before_using") %>
</p>

<hr style="border: 0; height: 1px; background: #B1B4B6; margin: 30px 0 30px 0;">

<h2 style="font-size: 27px; line-height: 35px; font-weight: bold; color: #0B0C0C;"><%= I18n.t("mailer.submission.answers_submitted") %></h2>

<% if @csv_filename.present? %>
<p><%= I18n.t("mailer.submission.csv_file", filename: @csv_filename) %></p>
<% end %>

<% if @json_filename.present? %>
<p><%= I18n.t("mailer.submission.json_file", filename: @json_filename) %></p>
<% end %>

<%= @submission.answer_content_for_email_html(heading_level: 3).html_safe %>

<hr style="border: 0; height: 1px; background: #B1B4B6; margin: 30px 0 30px 0;">

<div
style="margin: 0 0 20px 0; border-left: 10px solid #B1B4B6;
padding: 15px 0 0.1px 15px; font-size: 19px; line-height: 25px;"
>
<h2 style="font-size: 27px; line-height: 35px; font-weight: bold; color: #0B0C0C;"><%= I18n.t("mailer.cannot_reply.heading") %></h2>
<%= I18n.t("mailer.cannot_reply.contact_form_filler_html").html_safe %>
<%= I18n.t("mailer.cannot_reply.contact_forms_team_html").html_safe %>
</div>
<%= GovukFormsMarkdown.render_for_email(render(partial: "content", formats: [:md])).html_safe %>
Original file line number Diff line number Diff line change
@@ -1,39 +1 @@
<% if @submission.preview? %>
<%= I18n.t("mailer.submission.preview") %>

<% end %>
<%= I18n.t("mailer.form_name", form_name: @submission.form.name) %>

<%= I18n.t("mailer.submission.time", time: format_time(@submission.submission_time), date: format_date(@submission.submission_time) ) %>

<% if @welsh_submission %>
<%= I18n.t("mailer.submission.welsh_submission") %>
<% end %>

<%= I18n.t("mailer.submission.reference", submission_reference: @submission.reference) %>

<% if @submission.payment_url.present? %>
<%= I18n.t("mailer.submission.payment") %>

<% end %>
<%= I18n.t("mailer.check_before_using") %>

---
<%= I18n.t("mailer.submission.answers_submitted") %>

<% if @csv_filename.present? %>
<%= I18n.t("mailer.submission.csv_file", filename: @csv_filename) %>
<% end %>

<% if @json_filename.present? %>
<%= I18n.t("mailer.submission.json_file", filename: @json_filename) %>
<% end %>

<%= @submission.answer_content_for_email_plain_text %>

---

<%= I18n.t("mailer.cannot_reply.heading") %>

<%= I18n.t("mailer.cannot_reply.contact_form_filler_plain") %>
<%= I18n.t("mailer.cannot_reply.contact_forms_team_plain") %>
<%= GovukFormsMarkdown.render_plain_text(render(partial: "content", formats: [:md])).html_safe %>
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<% if @mode.preview? %>
<%= I18n.t("mailer.submission_batch.preview.#{@mode.tag}") %>

<% end %>

<%= I18n.t("mailer.form_name", form_name: @form_name) %>

<% if @filenames.size > 1 %>
<%= I18n.t("mailer.submission_batch.daily.multiple_files_explainer", date: @date) %>

<% else %>
<%= I18n.t("mailer.submission_batch.daily.single_file_explainer", date: @date) %>

<% end %>

<% @filenames.each do |filename| %>

- <%= filename %>

<% end %>

^<%= I18n.t("mailer.check_before_using") %>^

<%= I18n.t("mailer.submission_batch.submissions_are_split").html_safe %>

<%= I18n.t("mailer.submission_batch.file_upload_questions") %>

---

^^^

## <%= I18n.t("mailer.cannot_reply.heading") %>

<%= I18n.t("mailer.cannot_reply.contact_forms_team_markdown").html_safe %>

^^^
Loading