Class: Slippery::Processors::GraphvizDot

Inherits:
Object
  • Object
show all
Defined in:
lib/slippery/processors/graphviz_dot.rb

Overview

Turn embedded dot files into embedded SVGs

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(selector = '.language-dot') ⇒ GraphvizDot

Returns a new instance of GraphvizDot.



7
8
9
# File 'lib/slippery/processors/graphviz_dot.rb', line 7

def initialize(selector = '.language-dot')
  @selector = selector
end

Class Method Details

.call(doc) ⇒ Object



11
12
13
# File 'lib/slippery/processors/graphviz_dot.rb', line 11

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

Instance Method Details

#call(doc) ⇒ Object



15
16
17
18
19
# File 'lib/slippery/processors/graphviz_dot.rb', line 15

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

#copy_width_height(node) ⇒ Object



34
35
36
37
38
39
40
41
42
43
# File 'lib/slippery/processors/graphviz_dot.rb', line 34

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

#create_svg_from_dotObject



21
22
23
24
25
# File 'lib/slippery/processors/graphviz_dot.rb', line 21

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

#dot_to_hexp(dot_source) ⇒ Object



27
28
29
30
31
32
# File 'lib/slippery/processors/graphviz_dot.rb', line 27

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