forked from yjpark/jekyll-plantuml
-
Notifications
You must be signed in to change notification settings - Fork 0
/
plantuml.rb
68 lines (57 loc) · 1.92 KB
/
plantuml.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# Title: PlantUML Code Blocks for Jekyll
# Author: YJ Park ([email protected])
# https://github.com/yjpark/jekyll-plantuml
# Description: Integrate PlantUML intu Jekyll and Octopress.
#
# Syntax:
# {% plantuml %}
# plantuml code
# {% endplantuml %}
#
require 'open3'
require './plugins/raw'
module Jekyll
class PlantUMLFile < StaticFile
def write(dest)
true
end
end
class PlantUMLBlock < Liquid::Block
def render(context)
site = context.registers[:site]
code = @nodelist.join
puts "\nPlantUML configuration:"
if !site.config['plantuml_background_color'].nil?
background_color = "skinparam backgroundColor " + site.config["plantuml_background_color"]
puts "\tbackground_color = " + background_color
code = background_color + code
end
folder = "/images/plantuml/"
folderpath = site.dest + folder
if File.exist?(folderpath)
puts "PlantUML image path already exist.\n"
else
cmd = "mkdir -p " + folderpath
puts "Create PlantUML image path:\n\t" + cmd
result, status = Open3.capture2e(cmd)
puts " -->\t" + status.inspect() + "\t" + result
end
filename = Digest::MD5.hexdigest(code) + ".png"
plantuml_jar = File.expand_path(site.config['plantuml_jar'])
filepath = site.dest + folder + filename
if File.exist?(filepath)
puts "PlantUML image already exist: " + filepath + "\n"
else
cmd = "java -jar " + plantuml_jar + " -pipe > " + filepath
result, status = Open3.capture2e(cmd, :stdin_data=>code)
puts " -->\t" + status.inspect() + "\t" + result
end
site.static_files << Jekyll::PlantUMLFile.new(site, site.dest, folder, filename)
source = "<center>"
source += "<img src='" + folder + filename + "'>"
source += "</center>"
source
end
end
end
Liquid::Template.register_tag('plantuml', Jekyll::PlantUMLBlock)