Sha256: 90c7c3aab9203cb22ff5c6d11b079534005782e1014d3fafce689548c1630f46
Contents?: true
Size: 1.89 KB
Versions: 1
Compression:
Stored size: 1.89 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.gsub(/(?:.*\n){6}/, '') 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
1 entries across 1 versions & 1 rubygems
Version | Path |
---|---|
jekyll-diagrams-0.2.0 | lib/jekyll/diagrams/graphviz.rb |