mjml-rb 0.2.7 → 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: 6422177e2bbfc66cd23bdd9a88d8621fff3a8194059eba47e5934054d03e88ed
4
- data.tar.gz: 312008cba1935cce89440bba65c6b9a78145357c2f13291bdeaeec741f7d831c
3
+ metadata.gz: eda55f6bd0a60b7dda864d5602f5840ef4d9e971f72250f4a9617a39a922d1cc
4
+ data.tar.gz: 96502df7049ccf73728219d6fa1c20f1674f42f13d210688eca3cd5f238802b1
5
5
  SHA512:
6
- metadata.gz: 72e7039838d3f29a4d50bf47206e9022f14ad0c433b654909699e3d804e7eae7cfb737c97a140420477eade8457b094826d15f227693c046847c67cd481c59d8
7
- data.tar.gz: 6030143fd5f9d10fe16249397150815d8d842079cf925e7059deb805b94f866df2c4605c681e004e88ec9af4c27ab34ca6ac6107bd00ebd52d6e0df051cecd6a
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"]
@@ -15,19 +15,19 @@ module MjmlRb
15
15
  "font-size" => "13px"
16
16
  }.freeze
17
17
 
18
- HEAD_STYLE = <<~CSS.freeze
19
- @media only screen and (max-width:480px) {
20
- table.mj-full-width-mobile { width: 100% !important; }
21
- td.mj-full-width-mobile { width: auto !important; }
22
- }
23
- CSS
24
-
25
18
  def tags
26
19
  TAGS
27
20
  end
28
21
 
29
- def head_style
30
- HEAD_STYLE
22
+ def head_style(breakpoint)
23
+ lower_breakpoint = make_lower_breakpoint(breakpoint)
24
+
25
+ <<~CSS
26
+ @media only screen and (max-width:#{lower_breakpoint}) {
27
+ table.mj-full-width-mobile { width: 100% !important; }
28
+ td.mj-full-width-mobile { width: auto !important; }
29
+ }
30
+ CSS
31
31
  end
32
32
 
33
33
  def head_style_tags
@@ -178,6 +178,13 @@ module MjmlRb
178
178
  else "0"
179
179
  end
180
180
  end
181
+
182
+ def make_lower_breakpoint(breakpoint)
183
+ matched = breakpoint.to_s.match(/[0-9]+/)
184
+ return breakpoint if matched.nil?
185
+
186
+ "#{matched[0].to_i - 1}px"
187
+ end
181
188
  end
182
189
  end
183
190
  end
@@ -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.7".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.7
4
+ version: 0.2.10
5
5
  platform: ruby
6
6
  authors:
7
7
  - Andrei Andriichuk