-
Notifications
You must be signed in to change notification settings - Fork 78
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use the redcarpet gem to format petition emails
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
Showing
16 changed files
with
150 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |