Sha256: dfc7d0d1a53965f5f3f62394cd55bac2c0078ad14a4be66dcb88500f4b46eab5

Contents?: true

Size: 1.06 KB

Versions: 3

Compression:

Stored size: 1.06 KB

Contents

require 'tempfile'

module Slippery
  module Processors
    # Turn embedded dot files into embedded SVGs
    class GraphvizDot
      def initialize(selector = '.language-dot')
        @selector = selector
      end

      def self.call(doc)
        self.new.call(doc)
      end

      def call(doc)
        doc
          .rewrite(@selector, &create_svg_from_dot)
          .rewrite('polygon[fill=white][stroke=white]') { [] }
      end

      def create_svg_from_dot
        ->(node) do
          dot_to_hexp(node.text).process(copy_width_height(node))
        end
      end

      def dot_to_hexp(dot_source)
        file = Tempfile.new(['slippery','.dot'])
        file << dot_source
        file.close
        Hexp.parse(`dot #{file.path} -Tsvg`)
      end

      def copy_width_height(node)
        ->(svg) do
          return svg unless node[:width] || node[:height]
          [:width, :height].each do |attr|
            svg = svg.remove_attr(attr)
            svg = svg.attr(attr, node[attr]) if node[attr]
          end
          svg
        end
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
slippery-0.1.0 lib/slippery/processors/graphviz_dot.rb
slippery-0.0.3 lib/slippery/processors/graphviz_dot.rb
slippery-0.0.1 lib/slippery/processors/graphviz_dot.rb