Sha256: 44d5b5cf5d5702f52781c3d77f7aed28a9da22f253a1d6fee365883daf9f7283

Contents?: true

Size: 1.57 KB

Versions: 1

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[@tag_name])

        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='#{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

1 entries across 1 versions & 1 rubygems

Version Path
jekyll-diagrams-0.1.0 lib/jekyll/diagrams/diag.rb