From b4577a130159c2638b51318cb5645c39ec85ff46 Mon Sep 17 00:00:00 2001 From: EuAndreh Date: Fri, 16 Oct 2020 06:31:28 -0300 Subject: Add GraphvizBlock plugin --- _plugins/graphviz-block.rb | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 _plugins/graphviz-block.rb (limited to '_plugins') diff --git a/_plugins/graphviz-block.rb b/_plugins/graphviz-block.rb new file mode 100644 index 0000000..4ddd2ee --- /dev/null +++ b/_plugins/graphviz-block.rb @@ -0,0 +1,36 @@ +require 'tempfile' +require 'digest' + +OUTPUT_DIR = '/images/graphviz' + +module Jekyll + class GraphvizBlock < Liquid::Block + def initialize(tag_name, options, tokens) + super + opts_list = options.split('|').map { |s| s.strip } + @name = opts_list[0] + @alt = opts_list[1] + end + + def render(context) + source = super + site = context.registers[:site] + FileUtils.mkdir_p(File.join(site.source, OUTPUT_DIR)) + + filename = "#{@name}-#{Digest::SHA256.hexdigest(source + @alt)}.svg" + site_path = File.join(OUTPUT_DIR, filename) + os_path = File.join(site.source, site_path) + + f = Tempfile.new('graphviz-input') + f.write(source) + f.close + `dot -Tsvg #{f.path} > #{os_path}` + f.unlink + + site.static_files << Jekyll::StaticFile.new(site, site.source, OUTPUT_DIR, filename) + "\"#{@alt}\"" + end + end +end + +Liquid::Template.register_tag('graphviz', Jekyll::GraphvizBlock) -- cgit v1.2.3