mjml-rb 0.2.8 → 0.2.10

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: b6cd1e0e7d201fa71910c7ecf5aa470f2053a948bae4b08f004bc44398f30155
4
- data.tar.gz: 36a71f158c1ad304f53352e2b4657f2b2e253471edd4011b7e8270eca6895e73
3
+ metadata.gz: eda55f6bd0a60b7dda864d5602f5840ef4d9e971f72250f4a9617a39a922d1cc
4
+ data.tar.gz: 96502df7049ccf73728219d6fa1c20f1674f42f13d210688eca3cd5f238802b1
5
5
  SHA512:
6
- metadata.gz: 04bb951bdcebff861f813ee50fe54015510061a7ed83d3f563e5404eaa0128796450cf2b3bfa8943d4916b9fae4a33d2d33236b31be90156033f8be7c9074940
7
- data.tar.gz: 312d94578cb0e649335e85cd73eaf8bcdd3ad93163cdda32917a572ebcff3dc1adee2d36f9956969b1eec959b89919708ac5aa6eaf1371bc500e5899e766ace2
6
+ metadata.gz: e89bc6aba6bce27d9054d54fe0d7c4f869d8e96af642d0a922392362b1a8b7022c29a1f48dfc1d8f1e1e6011cdbdb494d71222859acf0177702149ee5bc23578
7
+ data.tar.gz: f0b2d84a9730658bbf60e9de11f1b2f8e896537eef8a4dd915e4472058e5852d1cde2aeee36876c02726496ae2ccedfd9dc795d781587309d8aa945fcc5dfb2c
@@ -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
- context[:head_styles] << css
50
- context[:inline_styles] << css if node.attributes["inline"] == "inline"
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"]
@@ -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
- font_links = context[:fonts].values.uniq.map { |href| %(<link href="#{escape_attr(href)}" rel="stylesheet" type="text/css">) }.join("\n")
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
- #{font_links}
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")
@@ -1,3 +1,3 @@
1
1
  module MjmlRb
2
- VERSION = "0.2.8".freeze
2
+ VERSION = "0.2.10".freeze
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mjml-rb
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.8
4
+ version: 0.2.10
5
5
  platform: ruby
6
6
  authors:
7
7
  - Andrei Andriichuk