class Jekyll::GraphvizBlock

Constants

DEFAULT_GRAPH_NAME
DIV_CLASS_ATTR
DOT_CMD
DOT_EXEC
DOT_EXTS
DOT_OPTS
DOT_PATH

Public Class Methods

new(tag_name, markup, tokens) click to toggle source
Calls superclass method
# File lib/octopress-graphviz-tag.rb, line 27
def initialize(tag_name, markup, tokens)
  super
  @tag_name = tag_name

  @title = markup or ""
  @title.strip!

  @src = ""
end

Public Instance Methods

add_desc_attrs(svg) click to toggle source
# File lib/octopress-graphviz-tag.rb, line 97
def add_desc_attrs(svg)
  svg.sub!("<svg", %[<svg aria-label="#{CGI::escapeHTML @title}"])
  svg.sub!("<svg", %[<svg role="img"])

  return svg
end
filter_for_inline_svg(code) click to toggle source
# File lib/octopress-graphviz-tag.rb, line 55
def filter_for_inline_svg(code)
  code = remove_declarations code
  code = remove_xmlns_attrs code
  code = add_desc_attrs code
  code = insert_desc_elements code
  code = wrap_with_div code
  code = remove_generated_comment code
  code
end
generate_svg(code) click to toggle source
# File lib/octopress-graphviz-tag.rb, line 70
def generate_svg code
  Open3.popen3( DOT_CMD ) do |stdin, stdout, stderr|
    stdout.binmode
    stdin.print code
    stdin.close

    err = stderr.read
    if not (err.nil? || err.strip.empty?)
      raise "Error from #{DOT_CMD}:\n#{err}"
    end

    svg = stdout.read
    svg.force_encoding 'UTF-8'

    return svg
  end
end
insert_desc_elements(svg) click to toggle source
# File lib/octopress-graphviz-tag.rb, line 104
def insert_desc_elements(svg)
  inserted_elements =  %[<title>#{CGI::escapeHTML @title}</title>\n]
  inserted_elements << %[<desc>#{CGI::escapeHTML @src}</desc>\n]
  svg.sub!(/(<svg [^>]*>)/, "\\1\n#{inserted_elements}")

  return svg
end
remove_declarations(svg) click to toggle source
# File lib/octopress-graphviz-tag.rb, line 88
def remove_declarations(svg)
  svg.sub(/<!DOCTYPE .+?>/im,'').sub(/<\?xml .+?\?>/im, '')
end
remove_generated_comment(code) click to toggle source

Remove version banner

# File lib/octopress-graphviz-tag.rb, line 66
def remove_generated_comment(code)
  code.sub(/<!-- Generated by graphviz version(.*?)-->/im, '')
end
remove_xmlns_attrs(svg) click to toggle source
# File lib/octopress-graphviz-tag.rb, line 92
def remove_xmlns_attrs(svg)
  svg.sub(%[xmlns="http://www.w3.org/2000/svg"], '')
    .sub(%[xmlns:xlink="http://www.w3.org/1999/xlink"], '')
end
render(context) click to toggle source
Calls superclass method
# File lib/octopress-graphviz-tag.rb, line 37
def render(context)
  code = super
  title = if @title.empty? then DEFAULT_GRAPH_NAME else @title end

  case @tag_name
  when 'graphviz'   then render_graphviz code
  when 'graph'      then render_graph 'graph', title, code
  when 'digraph'    then render_graph 'digraph', title, code
  else raise "unknown liquid tag name: #{@tag_name}"
  end
end
render_graph(type, title, code) click to toggle source
# File lib/octopress-graphviz-tag.rb, line 116
def render_graph(type, title, code)
  render_graphviz %[#{type} "#{title}" { #{code} }]
end
render_graphviz(code) click to toggle source
# File lib/octopress-graphviz-tag.rb, line 49
def render_graphviz(code)
  @src = code
  svg = generate_svg code
  filter_for_inline_svg svg
end
wrap_with_div(svg) click to toggle source
# File lib/octopress-graphviz-tag.rb, line 112
def wrap_with_div(svg)
  %[<div class="#{DIV_CLASS_ATTR}">#{svg}</div>]
end