Module: Maildown::Handlers::Markdown

Defined in:
lib/maildown/handlers/markdown.rb

Overview

The handler is what allows Rails to render markdown

See docs/rails_template_handler.pdf for detailed tutorial on using a template handler with Rails.

The TLDR; is you define a handler that responds to `call` and then you register it against an file extension with `ActionView::Template.register_template_handler`

At runtime if Rails finds a template with the same file extension that was registered it will use you handler and pass the template into `call` to render the template

Class Method Summary collapse

Class Method Details

.call(template, source = nil) ⇒ Object

Expected return value is valid ruby code wrapped in a string.

This handler takes care of both text and html email templates by inspectig the available `“formats”` and rendering the markdown to HTML if one of the formats is `:html`.


27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/maildown/handlers/markdown.rb', line 27

def self.call(template, source = nil)
  # The interface of template handlers changed in Rails 6.0 and the
  # source is passed as an argument. This check is here for compatibility
  # with Rails 5.0+.
  source ||= template.source

  # Match beginning whitespace but not newline
  # http://rubular.com/r/uCXQ58OOC8
  source.gsub!(/^[^\S\n]+/, ''.freeze) if Maildown.allow_indentation

  if Maildown.rails_6?
    compiled_source = erb_handler.call(template, source)
    is_html = (template.format != :text)
  else
    compiled_source = erb_handler.call(template)
    is_html = template.formats.include?(:html)
  end

  if is_html
    "Maildown::MarkdownEngine.to_html(begin;#{compiled_source}; end)"
  else
    "Maildown::MarkdownEngine.to_text(begin;#{compiled_source}; end)"
  end
end

.erb_handlerObject


18
19
20
# File 'lib/maildown/handlers/markdown.rb', line 18

def self.erb_handler
  @erb_handler ||= ActionView::Template.registered_template_handler(:erb)
end