mjml-rb 0.2.8 → 0.2.11
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/mjml-rb/components/button.rb +1 -1
- data/lib/mjml-rb/components/head.rb +5 -2
- data/lib/mjml-rb/renderer.rb +32 -2
- data/lib/mjml-rb/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: e37a1df063fbb80fad16b364acfd5f80623edeeabab31d3abd7264b486a287d7
|
|
4
|
+
data.tar.gz: 7added84353440e4750caf6c1c343baa50f23bdac800b5418a8432dd4f48e950
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: ffc75edaa21c23dedaeacd1ef032fa7ac4fa561f3719b9aa6314e8c6ac14a08419d22f2dfe2bcce699b680d47280ad2c7743595115ac01337bfc79176080c2fd
|
|
7
|
+
data.tar.gz: 91ff2e86ed6d191d8aec6fb438de9624d99150225f0626d8b88a26d3c8ae0d4c5e29f707498a27e6841a8740e80d4a254115a6387a959cab5d98a3f94eb44d31
|
|
@@ -101,7 +101,7 @@ module MjmlRb
|
|
|
101
101
|
)
|
|
102
102
|
link_attrs = { "style" => link_style }
|
|
103
103
|
if tag == "a"
|
|
104
|
-
link_attrs["href"] =
|
|
104
|
+
link_attrs["href"] = href
|
|
105
105
|
link_attrs["name"] = a["name"]
|
|
106
106
|
link_attrs["rel"] = a["rel"]
|
|
107
107
|
link_attrs["title"] = a["title"]
|
|
@@ -46,8 +46,11 @@ module MjmlRb
|
|
|
46
46
|
context[:preview] = raw_inner(node).strip
|
|
47
47
|
when "mj-style"
|
|
48
48
|
css = raw_inner(node)
|
|
49
|
-
|
|
50
|
-
|
|
49
|
+
if node.attributes["inline"] == "inline"
|
|
50
|
+
context[:inline_styles] << css
|
|
51
|
+
else
|
|
52
|
+
context[:head_styles] << css
|
|
53
|
+
end
|
|
51
54
|
when "mj-font"
|
|
52
55
|
name = node.attributes["name"]
|
|
53
56
|
href = node.attributes["href"]
|
data/lib/mjml-rb/renderer.rb
CHANGED
|
@@ -85,7 +85,7 @@ module MjmlRb
|
|
|
85
85
|
head_styles = ([DOCUMENT_RESET_CSS] + unique_strings(context[:head_styles])).join("\n")
|
|
86
86
|
head_raw = Array(context[:head_raw]).join("\n")
|
|
87
87
|
before_doctype = context[:before_doctype].to_s
|
|
88
|
-
|
|
88
|
+
font_tags = build_font_tags(content, context[:inline_styles], context[:fonts])
|
|
89
89
|
preview_block = preview.empty? ? "" : %(<div style="display:none;max-height:0;overflow:hidden;opacity:0;">#{escape_html(preview)}</div>)
|
|
90
90
|
html_attributes = { "lang" => context[:lang], "dir" => context[:dir] }
|
|
91
91
|
body_style = style_join(
|
|
@@ -101,7 +101,7 @@ module MjmlRb
|
|
|
101
101
|
<meta charset="utf-8">
|
|
102
102
|
<meta name="viewport" content="width=device-width, initial-scale=1">
|
|
103
103
|
<title>#{escape_html(title)}</title>
|
|
104
|
-
#{
|
|
104
|
+
#{font_tags}
|
|
105
105
|
<style type="text/css">#{head_styles}</style>
|
|
106
106
|
#{head_raw}
|
|
107
107
|
</head>
|
|
@@ -117,6 +117,36 @@ module MjmlRb
|
|
|
117
117
|
before_doctype.empty? ? html : "#{before_doctype}\n#{html}"
|
|
118
118
|
end
|
|
119
119
|
|
|
120
|
+
def build_font_tags(content, inline_styles, fonts)
|
|
121
|
+
used_urls = Array(fonts).filter_map do |name, url|
|
|
122
|
+
next if name.nil? || name.empty? || url.nil? || url.empty?
|
|
123
|
+
next unless font_used?(content, inline_styles, name)
|
|
124
|
+
|
|
125
|
+
url
|
|
126
|
+
end.uniq
|
|
127
|
+
return "" if used_urls.empty?
|
|
128
|
+
|
|
129
|
+
links = used_urls.map { |url| %(<link href="#{escape_attr(url)}" rel="stylesheet" type="text/css">) }.join("\n")
|
|
130
|
+
imports = used_urls.map { |url| "@import url(#{url});" }.join("\n")
|
|
131
|
+
|
|
132
|
+
<<~HTML.chomp
|
|
133
|
+
<!--[if !mso]><!-->
|
|
134
|
+
#{links}
|
|
135
|
+
<style type="text/css">
|
|
136
|
+
#{imports}
|
|
137
|
+
</style>
|
|
138
|
+
<!--<![endif]-->
|
|
139
|
+
HTML
|
|
140
|
+
end
|
|
141
|
+
|
|
142
|
+
def font_used?(content, inline_styles, font_name)
|
|
143
|
+
escaped_name = Regexp.escape(font_name)
|
|
144
|
+
content_regex = /"[^"]*font-family:[^"]*#{escaped_name}[^"]*"/mi
|
|
145
|
+
inline_regex = /font-family:[^;}]*#{escaped_name}/mi
|
|
146
|
+
|
|
147
|
+
content.to_s.match?(content_regex) || Array(inline_styles).any? { |style| style.to_s.match?(inline_regex) }
|
|
148
|
+
end
|
|
149
|
+
|
|
120
150
|
def render_children(node, context, parent:)
|
|
121
151
|
with_inherited_mj_class(context, node) do
|
|
122
152
|
node.children.map { |child| render_node(child, context, parent: parent) }.join("\n")
|
data/lib/mjml-rb/version.rb
CHANGED