Module: Sevgi::Derender::Attributes

Extended by:
Attributes
Included in:
Attributes
Defined in:
lib/sevgi/derender/attributes.rb

Constant Summary collapse

ATTRIBUTES_SHOULD_COME_FIRST =
%w[
id
inkscape:label
class
xmlns
xmlns:svg
xmlns:inkscape
xmlns:sodipodi
xmlns:_
      ]
.freeze
ATTRIBUTES_SHOULD_COME_LAST =
%w[
  style
].freeze

Instance Method Summary collapse

Instance Method Details

#decompile(hash) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/sevgi/derender/attributes.rb', line 21

def decompile(hash)
  pre, post = {}, {}

  ATTRIBUTES_SHOULD_COME_FIRST.each { |key| pre[key] = hash.delete(key) if hash.key?(key) }
  ATTRIBUTES_SHOULD_COME_LAST.each { |key| post[key] = hash.delete(key) if hash.key?(key) }

  {**pre, **hash, **post}
    .map do |key, value|
      key = Css.to_key(key) if key.is_a?(::String)

      if key == "style"
        "{ #{Attributes.decompile(Css.to_h!(value))} }"
      elsif value.is_a?(::String)
        Css.to_value(value)
      elsif value.is_a?(::Hash)
        "{ #{Attributes.decompile(value)} }"
      else
        value
      end => value

      key.match?(/\W/) ? "\"#{key}\": #{value}" : "#{key}: #{value}"
    end
    .join(", ")
end