Class: Middleman::Renderers::MiddlemanKramdownEmailHtmlConverter

Inherits:
Kramdown::Converter::Html
  • Object
show all
Defined in:
lib/middleman-newsletter/kramdown.rb

Constant Summary collapse

BLANK_LINE =
%(<div class="paragraph-break"><br></div>)
TABLE_PREAMBLE =
%(<table class="module" role="module" data-type="text" border="0" cellpadding="0" cellspacing="0"><tbody><tr><td><div>)
TABLE_POSTFIX =
%(<br></div>#{BLANK_LINE}</td></tr></tbody></table>)
IMG_TABLE_PREFIX =
%(<table class="module" role="module" data-type="image" border="0" cellpadding="0" cellspacing="0" width="100%"><tbody><tr><td>)
IMG_TABLE_POSTFIX =
%(#{BLANK_LINE}</td></tr></tbody></table>)
TD_TABLE_PREFIX =
%(<table cellpadding="0" cellspacing="0" align="left" border="0" bgcolor=""><tbody><tr>)
TD_TABLE_POSTFIX =
%(</tr></tbody></table>)

Instance Method Summary collapse

Instance Method Details

#convert_br(_el, _indent) ⇒ Object



64
65
66
67
68
69
70
71
72
73
74
# File 'lib/middleman-newsletter/kramdown.rb', line 64

def convert_br(_el, _indent)
  if @stack.last&.type == :p
    if @stack.last&.options[:transparent]
      "<br/>"
    else
      "</span></div>\n<div><span>"
    end
  else
    ''
  end
end

#convert_img(el, indent) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/middleman-newsletter/kramdown.rb', line 47

def convert_img(el, indent)
  attr = el.attr.dup
  attr['class'] = ["max-width", *attr['class']].compact.join(' ')
  link = attr.delete('src')
  # Request from SEO meeting -- strip alt tag for emails
  attr.delete('alt')
  # Attempt to manually run the asset host extension, since it's dependent on pulling data
  # from Rack normally, and we're directly rendering our content.
  if scope.extensions[:asset_host]
    uri = ::Middleman::Util.parse_uri(link)
    if uri.relative? && uri.host.nil?
      link = scope.extensions[:asset_host].rewrite_url(link, ::Pathname.new('/'), '')
    end
  end
  scope.image_tag(link, attr)
end

#convert_p(el, indent) ⇒ Object



29
30
31
32
33
34
35
36
37
# File 'lib/middleman-newsletter/kramdown.rb', line 29

def convert_p(el, indent)
  if el.options[:transparent]
    inner(el, indent)
  elsif el.children.size == 1 && el.children.first.type == :img
    convert_standalone_image(el, indent)
  else
    "#{(' ' * indent)}#{TABLE_PREAMBLE}#{format_as_block_html("span", el.attr, inner(el, indent), indent)}#{TABLE_POSTFIX}"
  end
end

#convert_standalone_image(el, indent) ⇒ Object



42
43
44
45
# File 'lib/middleman-newsletter/kramdown.rb', line 42

def convert_standalone_image(el, indent)
  image = el.children.first
  "#{(' ' * indent)}#{IMG_TABLE_PREFIX}#{convert_img(image, indent)}#{IMG_TABLE_POSTFIX}"
end

#convert_table(el, indent) ⇒ Object



76
77
78
79
80
81
82
83
84
85
# File 'lib/middleman-newsletter/kramdown.rb', line 76

def convert_table(el, indent)
  if el.type == :table
    attr = el.attr.dup
    attr['class'] = ['module', *attr['class']].compact.join(' ')
    attr['data-type'] = 'table'
    format_as_indented_block_html(el.type, attr, inner(el, indent), indent) + '<br>'
  else
    super(el, indent)
  end
end

#convert_td(el, indent) ⇒ Object



94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/middleman-newsletter/kramdown.rb', line 94

def convert_td(el, indent)
  res = inner(el, indent)
  type = :td

  count = @stack.last.children.length
  width = (90.0 / count).to_s
  width = '%.1f%%' % width

  attr = el.attr.dup
  alignment = @stack[-3].options[:alignment][@stack.last.children.index(el)]

  if alignment != :default
    attr['style'] = (attr.key?('style') ? "#{attr['style']}; " : '') + "text-align: #{alignment}"
  end
  attr['style'] = (attr.key?('style') ? "#{attr['style']}; " : '') + "width: #{width}"

  res = "<table><tbody><tr><td>#{res}</td></tr></tbody></table>"

  format_as_block_html(:td, attr, res.empty? ? entity_to_str(ENTITY_NBSP) : res, indent)
end

#convert_thead(el, indent) ⇒ Object



90
91
92
# File 'lib/middleman-newsletter/kramdown.rb', line 90

def convert_thead(el, indent)
  format_as_block_html(:tr, el.attr, inner(el, indent), indent)
end