Sha256: 772ce557b48a631fdfd8d236f0b6fa81f8b63c55c3d1e3a2e9251e1413e23db0

Contents?: true

Size: 1.57 KB

Versions: 2

Compression:

Stored size: 1.57 KB

Contents

require 'open3'
require 'tempfile'

module Jekyll
  module Diagrams
    class DiagBlock < Liquid::Block
      def initialize(tag_name, text, tokens)
        super
        @tag_name = tag_name
      end

      def render(context)
        config = context.registers[:site].config["diagrams"]

        @renderer = DiagRenderer.new(config['diag'])

        render_diagram(super)
      end

      private

      def render_diagram(code)
        @renderer.render(code, @tag_name)
      end
    end

    class DiagRenderer
      DEFAULTS = {
        'options' => '-Tsvg --nodoctype'
      }

      def initialize(config)
        @config = DEFAULTS.merge(config)
      end

      def render(code, engine)
        tmp = Temfile.new(engine).path
        dest = "#{tmp}.svg"
        
        File.write(tmp, code)
        cmd = "#{engine} #{@config['options']} #{tmp} -o #{dest}"
        ret, status = Open3.capture2(cmd, binmode: true)

        if !status.success?
          raise "Non-zero exit status '#{cmd}': #{status}"
        end
        
        svg = File.read(dest).force_encoding 'UTF-8'

        wrap_div(svg, engine)
      end

      private

      def wrap_div(svg, engine)
        "<div class='diag #{engine}'>#{svg}</div>"
      end
    end
  end
end

Liquid::Template.register_tag('blockdiag', Jekyll::Diagrams::DiagBlock)
Liquid::Template.register_tag('seqdiag', Jekyll::Diagrams::DiagBlock)
Liquid::Template.register_tag('actdiag', Jekyll::Diagrams::DiagBlock)
Liquid::Template.register_tag('nwdiag', Jekyll::Diagrams::DiagBlock)

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
jekyll-diagrams-0.2.1 lib/jekyll/diagrams/diag.rb
jekyll-diagrams-0.2.0 lib/jekyll/diagrams/diag.rb