Skip to content

Commit 63429d2

Browse files
committed
#476 Add support for PlantUML -debugsvek
1 parent 5cce3f9 commit 63429d2

File tree

7 files changed

+74
-13
lines changed

7 files changed

+74
-13
lines changed

docs/modules/ROOT/pages/diagram_types/plantuml.adoc

+8-1
Original file line numberDiff line numberDiff line change
@@ -26,5 +26,12 @@ PlantUML requires Java version 1.8 or higher to be installed.
2626
|size-limit |4096 |The maximum dimensions (width and height) of generated diagrams.
2727
|includedir |unspecified |sets a common directory for puml includes (plantuml.include.path)
2828
|preprocess |true |Preprocess PlantUML code before rendering the diagram.
29-
|options |unspecified |a comma separate list of flags that modify the image rendering. Currently only `smetana` is supported which enables the Smetana layout engine.
29+
|===
30+
31+
== Options
32+
[cols=">,<",options="header"]
33+
|===
34+
|Name |Description
35+
|debug |Emit debug files when generating images.
36+
|smetana |Enable the Smetana layout engine.
3037
|===

lib/asciidoctor-diagram/diagram_processor.rb

+14-4
Original file line numberDiff line numberDiff line change
@@ -194,17 +194,27 @@ def create_image_block(parent, source, format, converter)
194194

195195
options = converter.collect_options(source)
196196
result = converter.convert(source, format, options)
197-
198-
result.force_encoding(params[:encoding])
197+
if result.is_a? Hash
198+
image = result[:result]
199+
extra = result[:extra]
200+
else
201+
image = result
202+
extra = {}
203+
end
204+
image.force_encoding(params[:encoding])
199205

200206
metadata = source.create_image_metadata
201207
metadata[:options] = options
202208

203209
allow_image_optimisation = !source.global_opt('nooptimise')
204-
result, metadata[:width], metadata[:height] = params[:decoder].post_process_image(result, allow_image_optimisation)
210+
image, metadata[:width], metadata[:height] = params[:decoder].post_process_image(image, allow_image_optimisation)
205211

206212
FileUtils.mkdir_p(File.dirname(image_file)) unless Dir.exist?(File.dirname(image_file))
207-
File.open(image_file, 'wb') {|f| f.write result}
213+
File.open(image_file, 'wb') {|f| f.write image}
214+
215+
extra.each do |name, data|
216+
File.open(image_file + ".#{name}", 'wb') {|f| f.write data}
217+
end
208218

209219
if use_cache
210220
FileUtils.mkdir_p(File.dirname(metadata_file)) unless Dir.exist?(File.dirname(metadata_file))

lib/asciidoctor-diagram/plantuml/converter.rb

+37-1
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@ def collect_options(source)
4545
theme = source.attr('theme', nil)
4646
options[:theme] = theme if theme
4747

48+
options[:debug] = true if source.opt('debug')
49+
4850
options
4951
end
5052

@@ -117,6 +119,10 @@ def convert(source, format, options)
117119
end
118120
end
119121

122+
if options[:debug]
123+
headers['X-PlantUML-Debug'] = 'true'
124+
end
125+
120126
response = Java.send_request(
121127
:url => '/plantuml',
122128
:body => code,
@@ -127,7 +133,37 @@ def convert(source, format, options)
127133
raise Java.create_error("PlantUML image generation failed", response)
128134
end
129135

130-
response[:body]
136+
if response[:headers]['content-type'] =~ /multipart\/form-data;\s*boundary=(.*)/
137+
boundary = $1
138+
parts = {}
139+
140+
multipart_data = StringIO.new(response[:body])
141+
while true
142+
multipart_data.readline
143+
marker = multipart_data.readline
144+
if marker.start_with? "--#{boundary}--"
145+
break
146+
elsif marker.start_with? "--#{boundary}"
147+
part = Java.parse_body(multipart_data)
148+
if part[:headers]['content-disposition'] =~ /form-data;\s*name="([^"]*)"/
149+
if $1 == 'image'
150+
parts[:result] = part[:body]
151+
else
152+
parts[:extra] ||= {}
153+
parts[:extra][$1] = part[:body]
154+
end
155+
else
156+
raise "Unexpected multipart content disposition"
157+
end
158+
else
159+
raise "Unexpected multipart boundary"
160+
end
161+
end
162+
163+
parts
164+
else
165+
response[:body]
166+
end
131167
end
132168
end
133169

Binary file not shown.
Binary file not shown.

lib/asciidoctor-diagram/util/java.rb

+15-7
Original file line numberDiff line numberDiff line change
@@ -58,25 +58,34 @@ def self.parse_response(io)
5858
resp[:code] = status_line_parts[1].to_i
5959
resp[:reason] = status_line_parts[2]
6060

61+
resp.merge! parse_body(io)
62+
63+
resp
64+
end
65+
66+
def self.parse_body(io)
67+
body = {}
68+
69+
io.set_encoding Encoding::US_ASCII
6170
headers = {}
6271
until (header = io.readline(CRLF).strip).empty?
6372
key, value = header.split ':', 2
64-
headers[key] = value.strip
73+
headers[key.downcase] = value.strip
6574
end
6675

67-
resp[:headers] = headers
76+
body[:headers] = headers
6877

69-
content_length = headers['Content-Length']
78+
content_length = headers['content-length']
7079
if content_length
7180
io.set_encoding Encoding::BINARY
72-
resp[:body] = io.read(content_length.to_i)
81+
body[:body] = io.read(content_length.to_i)
7382
end
7483

75-
resp
84+
body
7685
end
7786

7887
def self.create_error(prefix_msg, response)
79-
content_type = response[:headers]['Content-Type'] || 'text/plain'
88+
content_type = response[:headers]['content-type'] || 'text/plain'
8089
if content_type.start_with? 'application/json'
8190
json = JSON.parse(response[:body].force_encoding(Encoding::UTF_8))
8291
ruby_bt = Kernel.caller(2)
@@ -97,7 +106,6 @@ def self.java
97106
@java_exe
98107
end
99108

100-
private
101109
def self.find_java
102110
case ::Asciidoctor::Diagram::Platform.os
103111
when :windows

0 commit comments

Comments
 (0)