asciidoctor-plantuml 0.0.6 → 0.0.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 34593810e67a089ee25f0e4ebfb7e14a0b307c18
4
- data.tar.gz: df526472cb4c78416f747fca5718d3800df0ce3f
3
+ metadata.gz: 7718a31ed4888ccbacef471e5e5fda271b371336
4
+ data.tar.gz: 7cf588d1ff45b671122be55961f007dcce2fdbb7
5
5
  SHA512:
6
- metadata.gz: bba398cd880168512cb502dabeb581f5fcd8b060450507693efab5d02dc8ab07fe78544d32553f99ee03be963cb103a4e5f4442410bb340af046d6af9472741c
7
- data.tar.gz: 1efcbd60cc11db47b30d6050e1fa9ca8abcd1f5c523628f545fada264f17055af493eb433400c99d99d531a21288eac027be84d38d23d1dd7a12c0fc6d3906f9
6
+ metadata.gz: bc542c286ccb6e1a9379560d3e3caa6dee8087de7f4f3d14cd0d43f27cef34df5e75a1051c9185f4376b9ba82b23f1a24656456ee90682024c81c322976f2f4a
7
+ data.tar.gz: 8fce5b5f981e13e0fd4dcad119b73a6685e64a58ad2ec2678e849bb2b58776cc98ea8288c8670346ea5c96dc0086b1ff23b8ca565f5a09128b290675efdf716b
@@ -32,226 +32,223 @@ module Asciidoctor
32
32
  yield(configuration)
33
33
  end
34
34
 
35
- module Processor
35
+ class Processor
36
36
 
37
37
  FORMATS = ["png", "svg", "txt"]
38
38
  DEFAULT_FORMAT = FORMATS[0]
39
39
 
40
- def valid_format?(format)
41
- FORMATS.include?(format)
42
- end
40
+ class << self
41
+ def valid_format?(format)
42
+ FORMATS.include?(format)
43
+ end
43
44
 
44
- def server_url
45
- PlantUml::configuration.url
46
- end
45
+ def server_url
46
+ PlantUml::configuration.url
47
+ end
47
48
 
48
- def txt_enabled?
49
- PlantUml::configuration.txt_enable
50
- end
49
+ def txt_enabled?
50
+ PlantUml::configuration.txt_enable
51
+ end
51
52
 
52
- def png_enabled?
53
- PlantUml::configuration.png_enable
54
- end
53
+ def png_enabled?
54
+ PlantUml::configuration.png_enable
55
+ end
55
56
 
56
- def svg_enabled?
57
- PlantUml::configuration.svg_enable
58
- end
57
+ def svg_enabled?
58
+ PlantUml::configuration.svg_enable
59
+ end
59
60
 
60
- def enabled?
61
- txt_enabled? || png_enabled? || svg_enabled?
62
- end
61
+ def enabled?
62
+ txt_enabled? || png_enabled? || svg_enabled?
63
+ end
63
64
 
64
- def plantuml_content(code, attrs = {})
65
+ def plantuml_content(code, attrs = {})
65
66
 
66
- format = attrs["format"] || DEFAULT_FORMAT
67
+ format = attrs["format"] || DEFAULT_FORMAT
67
68
 
68
- if !enabled?
69
- return plantuml_disabled_content(code, attrs)
70
- end
69
+ if !enabled?
70
+ return plantuml_disabled_content(code, attrs)
71
+ end
71
72
 
72
- if !valid_uri?(server_url)
73
- return plantuml_server_unavailable_content(server_url, attrs)
74
- end
73
+ if !valid_uri?(server_url)
74
+ return plantuml_server_unavailable_content(server_url, attrs)
75
+ end
75
76
 
76
- case format
77
- when "png"
78
- plantuml_img_content(code, format, attrs)
79
- when "txt"
80
- if txt_enabled?
81
- plantuml_txt_content(code, format, attrs)
77
+ case format
78
+ when "png"
79
+ plantuml_img_content(code, format, attrs)
80
+ when "txt"
81
+ if txt_enabled?
82
+ plantuml_txt_content(code, format, attrs)
83
+ else
84
+ plantuml_invalid_content(format, attrs)
85
+ end
86
+ when "svg"
87
+ plantuml_img_content(code, format, attrs)
82
88
  else
83
89
  plantuml_invalid_content(format, attrs)
84
90
  end
85
- when "svg"
86
- plantuml_img_content(code, format, attrs)
87
- else
88
- plantuml_invalid_content(format, attrs)
89
91
  end
90
- end
91
92
 
92
- def plantuml_txt_content(code, format, attrs = {})
93
- begin
94
- url = gen_url(code, format)
95
- open(url) do |f|
96
- plantuml_ascii_content(f.read, format, attrs)
93
+ # Compression code used to generate PlantUML URLs. Taken directly from the
94
+ # Transcoder class in the PlantUML java code.
95
+ def gen_url(text, format)
96
+ result = ""
97
+ compressedData = Zlib::Deflate.deflate(text)
98
+ compressedData.chars.each_slice(3) do |bytes|
99
+ #print bytes[0], ' ' , bytes[1] , ' ' , bytes[2]
100
+ b1 = bytes[0].nil? ? 0 : (bytes[0].ord & 0xFF)
101
+ b2 = bytes[1].nil? ? 0 : (bytes[1].ord & 0xFF)
102
+ b3 = bytes[2].nil? ? 0 : (bytes[2].ord & 0xFF)
103
+ result += append3bytes(b1, b2, b3)
97
104
  end
98
- rescue
99
- plantuml_img_content(code, format, attrs)
105
+ join_paths(server_url, "/#{format}/", result).to_s
100
106
  end
101
- end
102
-
103
- def plantuml_ascii_content(code, format, attrs = {})
104
- content = "<div class=\"listingblock\">"
105
- content += "<div class=\"content\">"
106
- content += "<pre "
107
- content +="id=\"#{attrs['id']}\" " if attrs['id']
108
- content +="class=\"plantuml\">\n"
109
- content += code
110
- content +="</pre>"
111
- content += "</div>"
112
- content += "</div>"
113
- end
114
-
115
- def plantuml_img_content(code, format, attrs = {})
116
- content = "<div class=\"imageblock\">"
117
- content += "<div class=\"content\">"
118
- content += "<img "
119
- content +="id=\"#{attrs['id']}\" " if attrs['id']
120
- content +="class=\"plantuml\" "
121
- content +="width=\"#{attrs['width']}\" " if attrs['width']
122
- content +="height=\"#{attrs['height']}\" " if attrs['height']
123
- content +="alt=\"#{attrs['alt']}\" " if attrs['alt']
124
- content +="src=\"#{gen_url(code, format)}\" />"
125
- content += "</div>"
126
- content += "</div>"
127
- end
128
-
129
- def plantuml_invalid_content(format, attrs = {})
130
- content = "<div class=\"listingblock\">"
131
- content += "<div class=\"content\">"
132
- content += "<pre "
133
- content +="id=\"#{attrs['id']}\" " if attrs['id']
134
- content +="class=\"plantuml plantuml-error\"> "
135
- content += "PlantUML Error: Invalid format \"#{format}\""
136
- content +="</pre>"
137
- content += "</div>"
138
- content += "</div>"
139
- end
140
107
 
141
- def plantuml_server_unavailable_content(url, attrs = {})
142
- content = "<div class=\"listingblock\">"
143
- content += "<div class=\"content\">"
144
- content += "<pre "
145
- content +="id=\"#{attrs['id']}\" " if attrs['id']
146
- content +="class=\"plantuml plantuml-error\"> "
147
- content += "PlantUML Error: cannot connect to PlantUML server at \"#{url}\""
148
- content +="</pre>"
149
- content += "</div>"
150
- content += "</div>"
151
- end
152
-
153
- def plantuml_disabled_content(code, attrs = {})
154
- content = "<div class=\"listingblock\">"
155
- content += "<div class=\"content\">"
156
- content += "<pre "
157
- content +="id=\"#{attrs['id']}\" " if attrs['id']
158
- content +="class=\"plantuml plantuml-error\">\n"
159
- content += code
160
- content +="</pre>"
161
- content += "</div>"
162
- content += "</div>"
163
- end
108
+ private
164
109
 
110
+ def plantuml_txt_content(code, format, attrs = {})
111
+ begin
112
+ url = gen_url(code, format)
113
+ open(url) do |f|
114
+ plantuml_ascii_content(f.read, format, attrs)
115
+ end
116
+ rescue
117
+ plantuml_img_content(code, format, attrs)
118
+ end
119
+ end
165
120
 
166
- # Compression code used to generate PlantUML URLs. Taken directly from the
167
- # Transcoder class in the PlantUML java code.
168
- def gen_url(text, format)
169
- result = ""
170
- compressedData = Zlib::Deflate.deflate(text)
171
- compressedData.chars.each_slice(3) do |bytes|
172
- #print bytes[0], ' ' , bytes[1] , ' ' , bytes[2]
173
- b1 = bytes[0].nil? ? 0 : (bytes[0].ord & 0xFF)
174
- b2 = bytes[1].nil? ? 0 : (bytes[1].ord & 0xFF)
175
- b3 = bytes[2].nil? ? 0 : (bytes[2].ord & 0xFF)
176
- result += append3bytes(b1, b2, b3)
121
+ def plantuml_ascii_content(code, format, attrs = {})
122
+ content = "<div class=\"listingblock\">"
123
+ content += "<div class=\"content\">"
124
+ content += "<pre "
125
+ content +="id=\"#{attrs['id']}\" " if attrs['id']
126
+ content +="class=\"plantuml\">\n"
127
+ content += code
128
+ content +="</pre>"
129
+ content += "</div>"
130
+ content += "</div>"
177
131
  end
178
- join_paths(server_url, "/#{format}/", result).to_s
179
- end
180
132
 
181
- def encode6bit(b)
182
- if b < 10
183
- return ('0'.ord + b).chr
133
+ def plantuml_img_content(code, format, attrs = {})
134
+ content = "<div class=\"imageblock\">"
135
+ content += "<div class=\"content\">"
136
+ content += "<img "
137
+ content +="id=\"#{attrs['id']}\" " if attrs['id']
138
+ content +="class=\"plantuml\" "
139
+ content +="width=\"#{attrs['width']}\" " if attrs['width']
140
+ content +="height=\"#{attrs['height']}\" " if attrs['height']
141
+ content +="alt=\"#{attrs['alt']}\" " if attrs['alt']
142
+ content +="src=\"#{gen_url(code, format)}\" />"
143
+ content += "</div>"
144
+ content += "</div>"
184
145
  end
185
- b = b - 10
186
- if b < 26
187
- return ('A'.ord + b).chr
146
+
147
+ def plantuml_invalid_content(format, attrs = {})
148
+ content = "<div class=\"listingblock\">"
149
+ content += "<div class=\"content\">"
150
+ content += "<pre "
151
+ content +="id=\"#{attrs['id']}\" " if attrs['id']
152
+ content +="class=\"plantuml plantuml-error\"> "
153
+ content += "PlantUML Error: Invalid format \"#{format}\""
154
+ content +="</pre>"
155
+ content += "</div>"
156
+ content += "</div>"
188
157
  end
189
- b = b - 26
190
- if b < 26
191
- return ('a'.ord + b).chr
158
+
159
+ def plantuml_server_unavailable_content(url, attrs = {})
160
+ content = "<div class=\"listingblock\">"
161
+ content += "<div class=\"content\">"
162
+ content += "<pre "
163
+ content +="id=\"#{attrs['id']}\" " if attrs['id']
164
+ content +="class=\"plantuml plantuml-error\"> "
165
+ content += "PlantUML Error: cannot connect to PlantUML server at \"#{url}\""
166
+ content +="</pre>"
167
+ content += "</div>"
168
+ content += "</div>"
192
169
  end
193
- b = b - 26
194
- if b == 0
195
- return '-'
170
+
171
+ def plantuml_disabled_content(code, attrs = {})
172
+ content = "<div class=\"listingblock\">"
173
+ content += "<div class=\"content\">"
174
+ content += "<pre "
175
+ content +="id=\"#{attrs['id']}\" " if attrs['id']
176
+ content +="class=\"plantuml plantuml-error\">\n"
177
+ content += code
178
+ content +="</pre>"
179
+ content += "</div>"
180
+ content += "</div>"
196
181
  end
197
- if b == 1
198
- return '_'
182
+
183
+ def encode6bit(b)
184
+ if b < 10
185
+ return ('0'.ord + b).chr
186
+ end
187
+ b = b - 10
188
+ if b < 26
189
+ return ('A'.ord + b).chr
190
+ end
191
+ b = b - 26
192
+ if b < 26
193
+ return ('a'.ord + b).chr
194
+ end
195
+ b = b - 26
196
+ if b == 0
197
+ return '-'
198
+ end
199
+ if b == 1
200
+ return '_'
201
+ end
202
+ return '?'
199
203
  end
200
- return '?'
201
- end
202
204
 
203
- def append3bytes(b1, b2, b3)
204
- c1 = b1 >> 2
205
- c2 = ((b1 & 0x3) << 4) | (b2 >> 4)
206
- c3 = ((b2 & 0xF) << 2) | (b3 >> 6)
207
- c4 = b3 & 0x3F
208
- return encode6bit(c1 & 0x3F).chr +
209
- encode6bit(c2 & 0x3F).chr +
210
- encode6bit(c3 & 0x3F).chr +
211
- encode6bit(c4 & 0x3F).chr
212
- end
205
+ def append3bytes(b1, b2, b3)
206
+ c1 = b1 >> 2
207
+ c2 = ((b1 & 0x3) << 4) | (b2 >> 4)
208
+ c3 = ((b2 & 0xF) << 2) | (b3 >> 6)
209
+ c4 = b3 & 0x3F
210
+ return encode6bit(c1 & 0x3F).chr +
211
+ encode6bit(c2 & 0x3F).chr +
212
+ encode6bit(c3 & 0x3F).chr +
213
+ encode6bit(c4 & 0x3F).chr
214
+ end
213
215
 
214
- # Make a call to the PlantUML server with the simplest diagram possible to
215
- # check if the server is available or not.
216
- def check_server(check_url)
217
- response = Net::HTTP.get_response(URI(check_url))
218
- return response.is_a?(Net::HTTPSuccess)
219
- rescue
220
- return false
221
- end
216
+ # Make a call to the PlantUML server with the simplest diagram possible to
217
+ # check if the server is available or not.
218
+ def check_server(check_url)
219
+ response = Net::HTTP.get_response(URI(check_url))
220
+ return response.is_a?(Net::HTTPSuccess)
221
+ rescue
222
+ return false
223
+ end
222
224
 
223
- def valid_uri?(uri)
224
- !(uri =~ /\A#{URI::regexp(['http', 'https'])}\z/).nil?
225
- end
225
+ def valid_uri?(uri)
226
+ !(uri =~ /\A#{URI::regexp(['http', 'https'])}\z/).nil?
227
+ end
226
228
 
227
- def create_plantuml_block(parent, content)
228
- Asciidoctor::Block.new parent, :pass, :content_model => :raw,
229
- :source => content, :subs => :default
230
- end
229
+ def join_paths(*paths, separator: '/')
230
+ paths = paths.compact.reject(&:empty?)
231
+ last = paths.length - 1
232
+ paths.each_with_index.map { |path, index|
233
+ expand_path(path, index, last, separator)
234
+ }.join
235
+ end
231
236
 
232
- def join_paths(*paths, separator: '/')
233
- paths = paths.compact.reject(&:empty?)
234
- last = paths.length - 1
235
- paths.each_with_index.map { |path, index|
236
- expand_path(path, index, last, separator)
237
- }.join
238
- end
237
+ def expand_path(path, current, last, separator)
238
+ if path.start_with?(separator) && current != 0
239
+ path = path[1..-1]
240
+ end
239
241
 
240
- def expand_path(path, current, last, separator)
241
- if path.start_with?(separator) && current != 0
242
- path = path[1..-1]
243
- end
242
+ unless path.end_with?(separator) || current == last
243
+ path = [path, separator]
244
+ end
244
245
 
245
- unless path.end_with?(separator) || current == last
246
- path = [path, separator]
246
+ path
247
247
  end
248
-
249
- path
250
248
  end
251
249
  end
252
250
 
253
251
  class BlockProcessor < Asciidoctor::Extensions::BlockProcessor
254
- include Processor
255
252
 
256
253
  def process(parent, target, attrs)
257
254
 
@@ -265,11 +262,19 @@ module Asciidoctor
265
262
  lines += ["@enduml"]
266
263
  end
267
264
 
268
- content = plantuml_content(lines.join("\n"), attrs)
265
+ content = Processor.plantuml_content(lines.join("\n"), attrs)
269
266
 
270
267
  return create_plantuml_block(parent, content)
271
268
 
272
269
  end
270
+
271
+ private
272
+
273
+ def create_plantuml_block(parent, content)
274
+ Asciidoctor::Block.new parent, :pass, :content_model => :raw,
275
+ :source => content, :subs => :default
276
+ end
277
+
273
278
  end
274
279
 
275
280
  end
@@ -1,5 +1,5 @@
1
1
  module Asciidoctor
2
2
  module PlantUML
3
- VERSION = "0.0.6"
3
+ VERSION = "0.0.7"
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: asciidoctor-plantuml
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.6
4
+ version: 0.0.7
5
5
  platform: ruby
6
6
  authors:
7
7
  - Horacio Sanson
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-12-06 00:00:00.000000000 Z
11
+ date: 2017-01-22 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler