Skip to content

Instantly share code, notes, and snippets.

@ryo88c
Created September 1, 2014 09:06
Show Gist options
  • Save ryo88c/bf871824ea3aced03ea0 to your computer and use it in GitHub Desktop.
Save ryo88c/bf871824ea3aced03ea0 to your computer and use it in GitHub Desktop.
Dot drawer plugin on Jekyll and Octopress.
# Title: Dot drawer
# Author: Ryo HAYASHI <ryo88c@gmail.com>
#
# Syntax:
# {% dot %}
# dotCode
# {% enddot %}
require 'open3'
require './plugins/raw'
require 'fileutils'
module Jekyll
class DotBlock < Liquid::Block
def render(context)
site = context.registers[:site]
config = site.config['dot']
dotCode = @nodelist.join
if config['tmp_folder'].nil?
tmpRoot = '_dot'
else
tmpRoot = File.expand_path(config['tmp_folder'])
end
imgFolderPath = tmpRoot + "/images/"
if !File.exist?(imgFolderPath)
FileUtils::mkdir_p imgFolderPath
puts "Create Dot image folder: " + imgFolderPath + "\n"
end
if config['dot_bin'].nil?
dotPath = 'dot'
else
dotPath = File.expand_path(config['dot_bin'])
end
if !File.exist?(dotPath)
throw Exception("Can't find Dot binary: " + dotPath);
end
fileName = Digest::MD5.hexdigest(dotCode)
imgFilePath = imgFolderPath + fileName + ".png"
dotFilePath = tmpRoot + "/" + fileName + ".dot"
if File.exist?(imgFilePath)
puts "Dot image already exist: " + imgFilePath + "\n"
else
cmd = "touch " + dotFilePath + "; cat <<EOT > " + dotFilePath + "\n" + dotCode;
result, status = Open3.capture2e(cmd)
puts "Create " + dotFilePath + " is " + status.inspect() + " > " + result
cmd = dotPath + " -Tpng " + dotFilePath + " -o " + imgFilePath
result, status = Open3.capture2e(cmd)
puts "Create " + imgFilePath + " is " + status.inspect() + " > " + result
end
site.static_files << Jekyll::StaticFile.new(site, tmpRoot, '/images/', fileName + ".png")
source = "<img src='/images/" + fileName + ".png'>"
end
end
end
Liquid::Template.register_tag('dot', Jekyll::DotBlock)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment