maildown 2.0.3 → 3.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/lib/maildown/ext/action_mailer.rb +45 -25
- data/lib/maildown/ext/action_view.rb +22 -0
- data/lib/maildown/handlers/markdown.rb +59 -0
- data/lib/maildown/markdown_engine.rb +20 -7
- data/lib/maildown/version.rb +1 -1
- data/lib/maildown.rb +5 -1
- data/test/dummy/app/controllers/handlers_controller.rb +6 -0
- data/test/dummy/app/mailers/user_no_layout_mailer.rb +24 -0
- data/test/dummy/app/views/handlers/plain_markdown.html.md +1 -0
- data/test/dummy/app/views/user_no_layout_mailer/contact.md.erb +1 -0
- data/test/dummy/app/views/user_no_layout_mailer/leading_whitespace.md+erb +1 -0
- data/test/dummy/app/views/user_no_layout_mailer/leading_whitespace_again.md+erb +1 -0
- data/test/dummy/app/views/user_no_layout_mailer/{welcome.md.erb → welcome.md+erb} +0 -0
- data/test/dummy/app/views/user_with_layout_mailer/welcome.md+erb +1 -0
- data/test/dummy/config/routes.rb +1 -1
- data/test/dummy/log/test.log +22765 -0
- data/test/integration/layouts_test.rb +0 -3
- data/test/integration/strip_leading_whitespace_test.rb +33 -0
- data/test/integration/template_handler_test.rb +22 -0
- data/test/test_helper.rb +0 -58
- data/test/unit/ext_action_mailer_test.rb +5 -11
- metadata +23 -11
- data/lib/maildown/md.rb +0 -38
- data/lib/tasks/maildown_tasks.rake +0 -4
- data/test/dummy/app/views/user_with_layout_mailer/welcome.md.erb +0 -1
- data/test/unit/maildown_test.rb +0 -55
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 26ca653dc43004e76229cd6ae163a8f67b3682cefa478b6daf03c5322e8877c1
|
4
|
+
data.tar.gz: e4cf385979d5adcd82a6b020065a616f936db4b7f692d81f7d90ffa908614284
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: eab147c20f6c250e74a4f018f0a1ca0dfb568ad357ab17f7bcc1745b255e1519f1a7f6af0f97e71192d33e1a610e3fdfa7c4241c03883c6b49ce570112c48f9d
|
7
|
+
data.tar.gz: 3ea0e175ea11f1abb2d706aaeed2eac4f36e5b2859b141a2a5b74526caeeb5c745e98c156caf9191ab96629a8557b3580f3502c9dbd342ddbbb70d7639d6ec9b
|
@@ -1,34 +1,54 @@
|
|
1
|
-
|
1
|
+
# frozen_string_literal: true
|
2
2
|
|
3
|
-
|
4
|
-
|
5
|
-
Mime::Type.register "text/md", :md, [], %w(md)
|
3
|
+
require 'action_mailer'
|
4
|
+
require 'action_mailer/base'
|
6
5
|
|
6
|
+
# Monkeypatch to allow mailer to auto generate text/html
|
7
|
+
#
|
8
|
+
# If you generate a mailer action, by default it will only
|
9
|
+
# render an html email:
|
10
|
+
#
|
11
|
+
# def welcome
|
12
|
+
# mail(
|
13
|
+
# to: "foo@example.com",
|
14
|
+
# reply_to: "noreply@schneems.com",
|
15
|
+
# subject: "hello world"
|
16
|
+
# )
|
17
|
+
# end
|
18
|
+
#
|
19
|
+
# You can add a format block to have it produce html
|
20
|
+
# and text emails:
|
21
|
+
#
|
22
|
+
# def welcome
|
23
|
+
# mail(
|
24
|
+
# to: "foo@example.com",
|
25
|
+
# reply_to: "noreply@schneems.com",
|
26
|
+
# subject: "hello world"
|
27
|
+
# ) do |format|
|
28
|
+
# format.text
|
29
|
+
# format.html
|
30
|
+
# end
|
31
|
+
# end
|
32
|
+
#
|
33
|
+
# For the handler to work correctly and produce both HTML
|
34
|
+
# and text emails this would need to be required similar to
|
35
|
+
# how https://github.com/plataformatec/markerb works.
|
36
|
+
#
|
37
|
+
# This monkeypatch detects when a markdown email is being
|
38
|
+
# used and generates both a markdown and text template
|
7
39
|
class ActionMailer::Base
|
8
|
-
alias :
|
9
|
-
|
10
|
-
def collect_responses(*args, &block)
|
11
|
-
responses = original_collect_responses(*args, &block)
|
12
|
-
md = ::Maildown::Md.new(responses)
|
13
|
-
if md.contains_md?
|
14
|
-
rendered_response = md.to_responses
|
40
|
+
alias :original_each_template :each_template
|
15
41
|
|
16
|
-
|
17
|
-
|
18
|
-
html = rendered_response[1]
|
42
|
+
def each_template(paths, name, &block)
|
43
|
+
templates = original_each_template(paths, name, &block)
|
19
44
|
|
20
|
-
|
21
|
-
text[:layout] = "#{layout_name}.text.erb"
|
22
|
-
text[:body] = render(text)
|
45
|
+
return templates if templates.first.handler != Maildown::Handlers::Markdown
|
23
46
|
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
end
|
47
|
+
html_template = templates.first
|
48
|
+
text_template = html_template.dup
|
49
|
+
formats = html_template.formats.dup.tap { |f| f.delete(:html) }
|
28
50
|
|
29
|
-
|
30
|
-
|
31
|
-
return responses
|
32
|
-
end
|
51
|
+
text_template.formats = formats
|
52
|
+
return [html_template, text_template]
|
33
53
|
end
|
34
54
|
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'action_view'
|
4
|
+
|
5
|
+
# This monkeypatch allows the use of `.md.erb` file extensions
|
6
|
+
# in addition to `.md+erb` and `.md`
|
7
|
+
module ActionView
|
8
|
+
class OptimizedFileSystemResolver
|
9
|
+
alias :original_extract_handler_and_format_and_variant :extract_handler_and_format_and_variant
|
10
|
+
|
11
|
+
# Different versions of rails have different
|
12
|
+
# method signatures here, path is always first
|
13
|
+
def extract_handler_and_format_and_variant(*args)
|
14
|
+
if args.first.end_with?('md.erb')
|
15
|
+
path = args.shift
|
16
|
+
path = path.gsub(/\.md\.erb\z/, '.md+erb')
|
17
|
+
args.unshift(path)
|
18
|
+
end
|
19
|
+
return original_extract_handler_and_format_and_variant(*args)
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
@@ -0,0 +1,59 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
require 'action_view'
|
3
|
+
|
4
|
+
module Maildown
|
5
|
+
module Handlers
|
6
|
+
# The handler is what allows Rails to render markdown
|
7
|
+
#
|
8
|
+
# See docs/rails_template_handler.pdf for detailed tutorial on
|
9
|
+
# using a template handler with Rails.
|
10
|
+
#
|
11
|
+
# The TLDR; is you define a handler that responds to `call` and then
|
12
|
+
# you register it against an file extension with `ActionView::Template.register_template_handler`
|
13
|
+
#
|
14
|
+
# At runtime if Rails finds a template with the same file extension that was
|
15
|
+
# registered it will use you handler and pass the template into `call` to
|
16
|
+
# render the template
|
17
|
+
module Markdown
|
18
|
+
def self.erb_handler
|
19
|
+
@erb_handler ||= ActionView::Template.registered_template_handler(:erb)
|
20
|
+
end
|
21
|
+
|
22
|
+
# Expected return value is valid ruby code wrapped in a string.
|
23
|
+
#
|
24
|
+
# This handler takes care of both text and html email templates
|
25
|
+
# by inspectig the available `"formats"` and rendering the
|
26
|
+
# markdown to HTML if one of the formats is `:html`.
|
27
|
+
def self.call(template)
|
28
|
+
# Match beginning whitespace but not newline http://rubular.com/r/uCXQ58OOC8
|
29
|
+
template.source.gsub!(/^[^\S\n]+/, ''.freeze) if Maildown.allow_indentation
|
30
|
+
|
31
|
+
compiled_source = erb_handler.call(template)
|
32
|
+
|
33
|
+
if template.formats.include?(:html)
|
34
|
+
return "Maildown::MarkdownEngine.to_html(begin;#{compiled_source}; end)"
|
35
|
+
else
|
36
|
+
return "Maildown::MarkdownEngine.to_text(begin;#{compiled_source}; end)"
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
# Allows for templates like `contact.md` or `contact.md+erb` to be rendered as
|
44
|
+
# markdown.
|
45
|
+
ActionView::Template.register_template_handler :"md+erb", Maildown::Handlers::Markdown
|
46
|
+
ActionView::Template.register_template_handler :"md", Maildown::Handlers::Markdown
|
47
|
+
|
48
|
+
# Used in conjunction with ext/action_view.rb monkey patch
|
49
|
+
# to allow for using the ".md.erb" file "extension".
|
50
|
+
#
|
51
|
+
# Rails only considers the last part of the file extension
|
52
|
+
# i.e the "erb" from "md.erb" to be an extension.
|
53
|
+
#
|
54
|
+
# The monkeypatch in `ext/action_view.rb` detects "md.erb" and converts
|
55
|
+
# it to md+erb which is an extension with a "variant". However
|
56
|
+
# to allow rails to even attempt to process the file, it needs this
|
57
|
+
# handler with ".md.erb" to be registered.
|
58
|
+
ActionView::Template.register_template_handler :"md.erb", Maildown::Handlers::Markdown
|
59
|
+
|
@@ -1,5 +1,25 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
module Maildown
|
4
|
+
# This module provides the API for Replacing the Markdown engine
|
5
|
+
#
|
6
|
+
# Maildown uses [kramdown](https://github.com/gettalong/kramdown) by default.
|
7
|
+
# Kramdown is pure ruby, so it runs the same across all ruby implementations:
|
8
|
+
# jruby, rubinius, MRI, etc. You can configure another parser if you like using
|
9
|
+
# the `Maildown::MarkdownEngine.set_html` method and pasing it a block.
|
10
|
+
#
|
11
|
+
# For example, if you wanted to use Redcarpet you could set it like this:
|
12
|
+
#
|
13
|
+
#
|
14
|
+
# Maildown::MarkdownEngine.set_html do |text|
|
15
|
+
# carpet = Redcarpet::Markdown.new(Redcarpet::Render::HTML, {})
|
16
|
+
# carpet.render(text).html_safe
|
17
|
+
# end
|
18
|
+
#
|
2
19
|
module MarkdownEngine
|
20
|
+
@maildown_markdown_engine_html_block = nil
|
21
|
+
@maildown_markdown_engine_text_block = nil
|
22
|
+
|
3
23
|
def self.to_html(string)
|
4
24
|
html_block.call(string)
|
5
25
|
end
|
@@ -16,17 +36,10 @@ module Maildown
|
|
16
36
|
set_html(&block)
|
17
37
|
end
|
18
38
|
|
19
|
-
class << self
|
20
|
-
extend Gem::Deprecate
|
21
|
-
deprecate :set, :set_html, 2017, 6
|
22
|
-
end
|
23
|
-
|
24
39
|
def self.set_text(&block)
|
25
40
|
@maildown_markdown_engine_text_block = block
|
26
41
|
end
|
27
42
|
|
28
|
-
@maildown_markdown_engine_html_block = nil
|
29
|
-
@maildown_markdown_engine_text_block = nil
|
30
43
|
def self.html_block
|
31
44
|
@maildown_markdown_engine_html_block || default_html_block
|
32
45
|
end
|
data/lib/maildown/version.rb
CHANGED
data/lib/maildown.rb
CHANGED
@@ -1,3 +1,4 @@
|
|
1
|
+
# Top level module, all module methods are used for configuration
|
1
2
|
module Maildown
|
2
3
|
@allow_indentations = false
|
3
4
|
@enable_layouts = false
|
@@ -11,14 +12,17 @@ module Maildown
|
|
11
12
|
end
|
12
13
|
|
13
14
|
def self.enable_layouts
|
15
|
+
puts "Maildown.enable_layouts is deprecated, setting this does nothing"
|
14
16
|
@enable_layouts
|
15
17
|
end
|
16
18
|
|
17
19
|
def self.enable_layouts=(enable_layouts)
|
20
|
+
puts "Maildown.enable_layouts= is deprecated, setting this does nothing"
|
18
21
|
@enable_layouts = enable_layouts
|
19
22
|
end
|
20
23
|
end
|
21
24
|
|
22
25
|
require 'maildown/markdown_engine'
|
23
|
-
require 'maildown/md'
|
24
26
|
require 'maildown/ext/action_mailer'
|
27
|
+
require 'maildown/ext/action_view'
|
28
|
+
require 'maildown/handlers/markdown'
|
@@ -8,4 +8,28 @@ class UserNoLayoutMailer < ApplicationMailer
|
|
8
8
|
subject: "hello world"
|
9
9
|
)
|
10
10
|
end
|
11
|
+
|
12
|
+
def leading_whitespace
|
13
|
+
mail(
|
14
|
+
to: "foo@example.com",
|
15
|
+
reply_to: "noreply@schneems.com",
|
16
|
+
subject: "hello world"
|
17
|
+
)
|
18
|
+
end
|
19
|
+
|
20
|
+
def leading_whitespace_again
|
21
|
+
mail(
|
22
|
+
to: "foo@example.com",
|
23
|
+
reply_to: "noreply@schneems.com",
|
24
|
+
subject: "hello world"
|
25
|
+
)
|
26
|
+
end
|
27
|
+
|
28
|
+
def contact
|
29
|
+
mail(
|
30
|
+
to: "foo@example.com",
|
31
|
+
reply_to: "noreply@schneems.com",
|
32
|
+
subject: "hello world"
|
33
|
+
)
|
34
|
+
end
|
11
35
|
end
|
@@ -0,0 +1 @@
|
|
1
|
+
Hello
|
@@ -0,0 +1 @@
|
|
1
|
+
Dual templates **rock**!
|
@@ -0,0 +1 @@
|
|
1
|
+
## Leading
|
@@ -0,0 +1 @@
|
|
1
|
+
## Leading again
|
File without changes
|
@@ -0,0 +1 @@
|
|
1
|
+
## Welcome!
|
data/test/dummy/config/routes.rb
CHANGED