haml2html 0.1.2 → 0.1.3

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: bcf730acaf73c99940361793046e483d462069800940912362b1b645d7c2bebc
4
- data.tar.gz: cd4b1cf2bcbc28e1a80ccc074bc965919d5b3c6d9ec38d2f5398d8bbfd37db2a
3
+ metadata.gz: 54fab4cd9a6853ba2f3bb3568c17a18f1667b7507a2de432966bea62e6a218e1
4
+ data.tar.gz: 11e567f5d534e34e2f1a88029f94e20847d13953cdd995e169a65246aaba8ed9
5
5
  SHA512:
6
- metadata.gz: f6a33a3a82682bca7ad2a5707f5e68c16c468f1d14e9bc7a9d25a3fa3362c8ebf5df756c38a0548dafa144c93aa43e37ab55e286d286b89ff5595f78893bdff1
7
- data.tar.gz: 8e08fe063629fa885a1596b4456268706611f0f1807b92b7658138829d04a8fceb777d010389963481b7d539136d99d96696f8658546df44bf309f7db4e7e2b8
6
+ metadata.gz: e5506405f5757513488fd1771d86adbee964c08d89d5f42f54e1236e6a2e32f1d871ca29c9ee7db83687629325d241416b222e279253f1a464b603ef4c0425af
7
+ data.tar.gz: bed4c344c88cf6cbd41d89f59398c15ea724a63afb2b91c852f1a1d3402d676dda2be37d6a85414173437fad54064dd4642591e1547d2d38403ba1e38abdb976
data/CHANGELOG.md CHANGED
@@ -1,5 +1,9 @@
1
1
  # Changelog
2
2
 
3
+ ## 0.1.3
4
+
5
+ - Fix generated ERB for Haml attributes with mixed static and dynamic classes.
6
+
3
7
  ## 0.1.2
4
8
 
5
9
  - Raise the minimum supported Ruby version to 3.2.
@@ -2,6 +2,7 @@
2
2
 
3
3
  require "cgi"
4
4
  require "haml"
5
+ require "ripper"
5
6
 
6
7
  require_relative "diagnostic"
7
8
 
@@ -79,6 +80,7 @@ module Haml2html
79
80
  if node.children.empty? && !blank?(inline)
80
81
  return "#{open}#{inline}#{close}\n"
81
82
  end
83
+ return "#{open}#{close}\n" if node.children.empty?
82
84
 
83
85
  "#{open}\n#{emit_children(node.children, indent + 1)}#{spaces(indent)}#{close}\n"
84
86
  end
@@ -98,13 +100,18 @@ module Haml2html
98
100
 
99
101
  def emit_attributes(node)
100
102
  value = node.value
101
- attrs = static_attributes(value)
103
+ attrs = value[:attributes].dup
102
104
  diagnose_object_ref(node, value)
103
105
 
104
106
  dynamic = dynamic_attributes_expression(value[:dynamic_attributes])
105
- return attrs unless dynamic
107
+ return static_attributes(attrs) unless dynamic
106
108
 
107
- "#{attrs}#{dynamic_attributes(dynamic)}"
109
+ if (simple_attrs = simple_dynamic_attributes(dynamic))
110
+ dynamic_class = simple_attrs.delete("class")
111
+ return "#{static_attributes(attrs, dynamic_class: dynamic_class)}#{simple_attrs.map { |name, attr_value| dynamic_attribute(name, attr_value) }.join}"
112
+ end
113
+
114
+ "#{static_attributes(attrs)}#{dynamic_attributes(dynamic)}"
108
115
  end
109
116
 
110
117
  def emit_script(node, indent)
@@ -177,14 +184,61 @@ module Haml2html
177
184
  end
178
185
  end
179
186
 
180
- def static_attributes(value)
181
- value[:attributes].sort.map do |name, attr_value|
182
- %( #{name}="#{escape_attr(interpolate_text(attr_value.to_s))}")
187
+ def static_attributes(attrs, dynamic_class: nil)
188
+ emitted_attrs = attrs.dup
189
+ emitted_attrs["class"] = "" if dynamic_class && !emitted_attrs.key?("class")
190
+
191
+ emitted_attrs.sort.map do |name, attr_value|
192
+ value = escape_attr(interpolate_text(attr_value.to_s))
193
+ value = value.empty? ? dynamic_class : "#{value} #{dynamic_class}" if name == "class" && dynamic_class
194
+ %( #{name}="#{value}")
183
195
  end.join
184
196
  end
185
197
 
186
198
  def dynamic_attributes(dynamic)
187
- %(<%== tag.attributes(**#{dynamic}).then { |attrs| attrs.empty? ? "" : " \#{attrs}" } %>)
199
+ %(<%== (_haml2html_attrs = tag.attributes(**#{dynamic})).empty? ? "" : " \#{_haml2html_attrs}" %>)
200
+ end
201
+
202
+ def dynamic_attribute(name, value)
203
+ %( #{name}="#{value}")
204
+ end
205
+
206
+ def simple_dynamic_attributes(dynamic)
207
+ program = Ripper.sexp(dynamic)
208
+ hash = program&.dig(1, 0)
209
+ return nil unless hash&.first == :hash
210
+
211
+ associations = hash.dig(1, 1)
212
+ return nil unless associations.is_a?(Array)
213
+
214
+ associations.each_with_object({}) do |association, attrs|
215
+ return nil unless association&.first == :assoc_new
216
+
217
+ label = association[1]
218
+ return nil unless label&.first == :@label
219
+
220
+ value = simple_dynamic_attribute_value(association[2])
221
+ return nil unless value
222
+
223
+ attrs[label[1].delete_suffix(":")] = value
224
+ end
225
+ end
226
+
227
+ def simple_dynamic_attribute_value(node)
228
+ case node&.first
229
+ when :string_literal
230
+ escape_attr(string_literal_content(node))
231
+ when :vcall
232
+ ident = node.dig(1, 1)
233
+ return nil unless ident
234
+
235
+ "<%= #{ident} %>"
236
+ end
237
+ end
238
+
239
+ def string_literal_content(node)
240
+ content = node.dig(1, 1, 1)
241
+ content.to_s
188
242
  end
189
243
 
190
244
  def dynamic_attributes_expression(dynamic)
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Haml2html
4
- VERSION = "0.1.2"
4
+ VERSION = "0.1.3"
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: haml2html
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.1.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Kyle
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2026-05-07 00:00:00.000000000 Z
11
+ date: 2026-05-08 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: haml