-
Notifications
You must be signed in to change notification settings - Fork 0
/
jekyll-plantuml.rb
115 lines (94 loc) · 2.71 KB
/
jekyll-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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
require 'open3'
require 'fileutils'
module Jekyll
module Converters
class PlantUML < Jekyll::Converter
attr_accessor :config
safe true
priority :normal
def initialize(config)
@config = config
end
def matches(ext)
ext =~ /^\.(wsd|pu|puml|plantuml|iuml)$/i
end
def output_ext(ext)
".#{extension}"
end
def convert(content)
cache.getset(content) do
cmd = "java -Djava.awt.headless=true #{java_args} -jar #{plantuml_jar} -t#{type} #{plantuml_args} -pipe"
result, status = Open3.capture2(cmd, :stdin_data=>content, :binmode=>true)
result
end
end
def plantuml_jar
File.expand_path(config['plantuml']['plantuml_jar'] || 'plantuml.jar')
end
def type
config['plantuml']['type'] || 'svg'
end
def plantuml_args
config['plantuml']['plantuml_args'] || ''
end
def java_args
config['plantuml']['java_args'] || ''
end
# Support all types otlined in "Types of Output File" table
# https://plantuml.com/command-line
def extension
config['plantuml']['extension'] || begin
ext = type.split(':', 2).first
case ext
when 'braille'
ext = 'png'
when 'txt'
ext = 'atxt'
end
ext
end
end
def cache
@@cache ||= Jekyll::Cache.new("Jekyll::Converters::PlantUML")
end
end
end
end
module Jekyll
module Generators
class PlantUML < Jekyll::Generator
attr_accessor :site
safe true
priority :normal
def initialize(site)
@site = site
end
def generate(site)
@site = site
site.pages.concat(pages)
site.static_files -= plantuml_files
end
private
# An array of potential Jekyll::Pages to add
def pages
plantuml_files.map { |static_file| page_from_static_file(static_file) }
end
# An array of Jekyll::StaticFile's with a site-defined markdown extension
def plantuml_files
site.static_files.select { |file| converter.matches(file.extname) }
end
# Given a Jekyll::StaticFile, returns the file as a Jekyll::Page
def page_from_static_file(static_file)
base = static_file.instance_variable_get("@base")
dir = static_file.instance_variable_get("@dir")
name = static_file.instance_variable_get("@name")
page = Jekyll::Page.new(site, base, dir, name)
page.data["layout"] = nil
return page
end
def converter
@converter ||= site.find_converter_instance(Jekyll::Converters::PlantUML)
end
end
end
end