Sha256: cea1fefdf959997ef7ee4169352c823bd6d43041831cc6b722f6820ea9ee884d

Contents?: true

Size: 1018 Bytes

Versions: 2

Compression:

Stored size: 1018 Bytes

Contents

module Jekyll
  module Diagrams
    class GraphvizBlock < Block
      CONFIGRATIONS = {
        'K' => 'default_layout',
        'G' => 'graph_attributes',
        'N' => 'node_attributes',
        'E' => 'edge_attributes'
      }.freeze

      def render_svg(code, config)
        command = build_command(config)

        svg = render_with_stdin_stdout(command, code)
        svg.sub!(/^<\?xml(([^>]|\n)*>\n?){2}/, '')
      end

      def build_command(config)
        command = 'dot -Tsvg'

        CONFIGRATIONS.each do |prefix, conf|
          next unless config.has_key?(conf)

          attrs = config[conf]

          attrs = case attrs
          when String
            attrs
          when Array
            attrs.join(" -#{prefix}")
          when Hash
            attrs.map { |k, v| "#{k}=#{v}" }.join(" -#{prefix}")
          end

          command << " -#{prefix}#{attrs}"
        end

        command
      end
    end
  end
end

Liquid::Template.register_tag(:graphviz, Jekyll::Diagrams::GraphvizBlock)

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
jekyll-diagrams-0.9.1 lib/jekyll-diagrams/graphviz.rb
jekyll-diagrams-0.9.0 lib/jekyll-diagrams/graphviz.rb