Sha256: 5e7f114469073e1111ca5765cc1cce96e2825e5c5cb11c1091c2ddc1b2920751
Contents?: true
Size: 1.88 KB
Versions: 2
Compression:
Stored size: 1.88 KB
Contents
require 'open3' module Jekyll module Diagrams class GraphvizBlock < Liquid::Block def initialize(tag_name, text, tokens) super @tag_name = tag_name @engine = text.strip end def render(context) config = context.registers[:site].config["diagrams"] @renderer = GraphvizRenderer.new(config["graphviz"]) render_diagram(super) end private def render_diagram(code) code = case @tag_name when 'graphviz' code when 'graph' "graph {\n#{code}\n}" when 'digraph' "digraph {\n#{code}\n}" else raise "unknown liquid tag name: #{@tag_name}" end @renderer.render(code, @engine) end end class GraphvizRenderer DEFAULTS = { 'engine' => 'dot', 'options' => '-Tsvg' } def initialize(config) @config = DEFAULTS.merge(config) end def render(code, engine = DEFAULTS['engine']) cmd = "#{@config['engine']} #{@config['options']}" svg, status = Open3.capture2( cmd, stdin_data: code, binmode: true ) if !status.success? raise "Non-zero exit status '#{cmd}': #{status}" end svg.force_encoding 'UTF-8' strip_metainfo(svg) wrap_div(svg) end private def strip_metainfo(svg) svg.sub(/([^\n]*\n){6}/, '') .sub(%[xmlns="http://www.w3.org/2000/svg"], '') .sub(%[xmlns:xlink="http://www.w3.org/1999/xlink"], '') end def wrap_div(svg) "<div class='graphviz'>#{svg}</div>" end end end end Liquid::Template.register_tag('graphviz', Jekyll::Diagrams::GraphvizBlock) Liquid::Template.register_tag('graph', Jekyll::Diagrams::GraphvizBlock) Liquid::Template.register_tag('digraph', Jekyll::Diagrams::GraphvizBlock)
Version data entries
2 entries across 2 versions & 1 rubygems
Version | Path |
---|---|
jekyll-diagrams-0.2.4 | lib/jekyll/diagrams/graphviz.rb |
jekyll-diagrams-0.2.3 | lib/jekyll/diagrams/graphviz.rb |