Sha256: 7b6822ca6c931ad4efb16c5424267767e96f21723b4f74341ba0ceaf291e31f3

Contents?: true

Size: 1.39 KB

Versions: 2

Compression:

Stored size: 1.39 KB

Contents

module SketchilyHelper
  def sketchily_show(uri, options = {})
    if options[:bkgd_color] || options[:bkgd_file]
      require 'nokogiri'
      doc = Nokogiri::XML(Base64.decode64(uri)) {|xml| xml.noblanks}

      if options[:bkgd_file]
        data_uri = "data:image/svg+xml;base64,#{Base64.strict_encode64(open(options[:bkgd_file]).read)}"
        bkgd_file_node = Nokogiri::XML::Node.new 'g', doc
        Nokogiri::XML::Builder.with(bkgd_file_node) do |xml|
          xml.title 'Background Image Layer'
          xml.image :id => 'background_image', :width => '100%', :height => '100%', :preserveAspectRatio => 'xMinYMin', 'xlink:href' => data_uri
        end
        doc.at('g').before(bkgd_file_node)
      end

      if options[:bkgd_color]
        bkgd_color_node = Nokogiri::XML::Node.new 'g', doc
        Nokogiri::XML::Builder.with(bkgd_color_node) do |xml|
          xml.title 'Background Color Layer'
          xml.rect :id => 'background_color', :width => '100%', :height => '100%', :fill => "##{options[:bkgd_color]}"
        end
        doc.at('g').before(bkgd_color_node)
      end
print doc.to_xml
      uri = Base64.strict_encode64(doc.to_xml)
    end

    "<img src='data:image/svg+xml;base64,#{uri}' #{"width='#{options[:width]}'" if options[:width]} #{"height='#{options[:height]}'" if options[:height]}/>".html_safe
  end

  def svg_show(uri, options = {})
    sketchily_show(uri, options)
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
sketchily-1.4.1 app/helpers/sketchily_helper.rb~
sketchily-1.4.0 app/helpers/sketchily_helper.rb