asciidoctor-plantuml 0.0.14 → 0.1.0

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
  SHA256:
3
- metadata.gz: '0758c3c21b6b684c8d1659332edbb2dd21e2733973cf59e8c4317754f1697505'
4
- data.tar.gz: 5ea7379ddaf79330b11930a719b03b97df72580da00a2b291121378b5a785503
3
+ metadata.gz: 06c1cf16fb57543cec6f90c9d907894ebe6c8b8888396f5cfeb9305340f130c1
4
+ data.tar.gz: 82488d9c4c5431c9244e0ad834bfbb2ae88ef1fe38146cca7d369352f967ff4f
5
5
  SHA512:
6
- metadata.gz: 647dfb74a0015698ffb4b85c72f35c7c299a52ac7bdff85fe2d4252d891cceadd636f0008089420d91e144d39e74c88dc080234d273f81b6f3175a586156ba56
7
- data.tar.gz: cc9da76d59762c3dd34570a77d0b9c4ddb826af087798b34068d14bcbb8f5caf19f5cc075b1057b143cb419b88dc1e19117abef886ebaaa782984622e68272b6
6
+ metadata.gz: 71e3258bc87c722dc6c393129ca5b5984c4ac45f4ea3f6c7ef49907d9dc71c605a1650852bcf6b6f9201115179f3672724883eaa6db0947dca387bb1a7ecf47c
7
+ data.tar.gz: 41993506e615c6ce9180b5677d2bb40a83e18b33abae1b1d16e96ed5b0493d773a747659a4c08be24a13728ac0303e3c63f31ea194b109decec8f0148f7ef79f
@@ -6,4 +6,5 @@ require_relative 'asciidoctor_plantuml/plantuml'
6
6
 
7
7
  Asciidoctor::Extensions.register do
8
8
  block Asciidoctor::PlantUml::BlockProcessor, :plantuml
9
+ block_macro Asciidoctor::PlantUml::BlockMacroProcessor, :plantuml
9
10
  end
@@ -10,8 +10,8 @@ module Asciidoctor
10
10
  module PlantUml
11
11
  # PlantUML Configuration
12
12
  class Configuration
13
- DEFAULT_URL = ENV['PLANTUML_URL'] || ''
14
- DEFAULT_ENCODING = ENV['PLANTUML_ENCODING'] || 'legacy'
13
+ DEFAULT_URL = ENV.fetch('PLANTUML_URL', '')
14
+ DEFAULT_ENCODING = ENV.fetch('PLANTUML_ENCODING', 'legacy')
15
15
 
16
16
  attr_accessor :url, :txt_enable, :svg_enable, :png_enable, :encoding
17
17
 
@@ -78,23 +78,48 @@ module Asciidoctor
78
78
  txt_enabled? || png_enabled? || svg_enabled?
79
79
  end
80
80
 
81
- def plantuml_content_format(code, format, attrs = {})
82
- if %w[png svg txt].include?(format) &&
83
- method("#{format}_enabled?").call
84
- method("plantuml_#{format}_content").call(code, format, attrs)
81
+ def plantuml_content_format(parent, code, format, attrs = {})
82
+ content = code.read
83
+
84
+ # honor subs attributes
85
+ # e.g. replace asciidoc variables
86
+ subs = attrs['subs']
87
+ content = parent.apply_subs(content, parent.resolve_subs(subs)) if subs
88
+
89
+ # add @start... and @end... if missing
90
+ content = "@startuml\n#{content}\n@enduml" unless content =~ /^@start.*@end[a-z]*$/m
91
+
92
+ # insert global plantuml config after first line
93
+ config_path = parent.attr('plantuml-include', '', true)
94
+ begin
95
+ content = insert_config_to_content(parent, config_path, content, attrs) unless config_path.empty?
96
+ rescue StandardError => e
97
+ return plantuml_invalid_file(config_path, e.message, attrs)
98
+ end
99
+
100
+ if %w[png svg txt].include?(format) && method("#{format}_enabled?").call
101
+ method("plantuml_#{format}_content").call(content, format, attrs)
85
102
  else
86
103
  plantuml_invalid_content(format, attrs)
87
104
  end
88
105
  end
89
106
 
90
- def plantuml_content(code, attrs = {})
107
+ def plantuml_content(parent, code, attrs = {})
91
108
  format = attrs['format'] || DEFAULT_FORMAT
92
109
 
93
110
  return plantuml_disabled_content(code, attrs) unless enabled?
94
111
 
95
112
  return plantuml_server_unavailable_content(server_url, attrs) unless valid_uri?(server_url)
96
113
 
97
- plantuml_content_format(code, format, attrs)
114
+ plantuml_content_format(parent, code, format, attrs)
115
+ end
116
+
117
+ def plantuml_content_from_file(parent, source_file, attrs = {})
118
+ File.open(source_file) do |f|
119
+ return plantuml_content(parent, f, attrs)
120
+ end
121
+ rescue StandardError => e
122
+ plantuml_invalid_file(source_file, e.message, attrs)
98
123
  end
99
124
 
100
125
  # Compression code used to generate PlantUML URLs. Taken directly from
@@ -113,8 +138,25 @@ module Asciidoctor
113
138
  join_paths(server_url, "#{format}/", result).to_s
114
139
  end
115
140
 
141
+ def create_plantuml_block(parent, content, attrs)
142
+ Asciidoctor::Block.new parent, :pass, {
143
+ content_model: :raw,
144
+ source: content,
145
+ subs: :default
146
+ }.merge(attrs)
147
+ end
148
+
116
149
  private
117
150
 
151
+ def insert_config_to_content(parent, config_path, content, attrs)
152
+ File.open(config_path) do |file|
153
+ config = file.read
154
+ subs = attrs['subs']
155
+ config = parent.apply_subs(config, parent.resolve_subs(subs)) if subs
156
+ return content.dup.insert(content.index("\n"), "\n#{config}") unless config.empty?
157
+ end
158
+ end
159
+
118
160
  def plantuml_txt_content(code, format, attrs = {})
119
161
  url = gen_url(code, format)
120
162
  URI(url).open do |f|
@@ -180,6 +222,11 @@ module Asciidoctor
180
222
  _plantuml_error_content(code, attrs)
181
223
  end
182
224
 
225
+ def plantuml_invalid_file(file, error, attrs = {})
226
+ error = "PlantUML Error: Could not parse \"#{file}\": #{error}"
227
+ _plantuml_error_content(error, attrs)
228
+ end
229
+
183
230
  def _plantuml_error_content(error, attrs = {})
184
231
  content = '<div class="listingblock">'
185
232
  content += '<div class="content">'
@@ -246,7 +293,7 @@ module Asciidoctor
246
293
  end
247
294
 
248
295
  def expand_path(path, current, last, separator)
249
- path = path[1..-1] if path.start_with?(separator) && current.zero?
296
+ path = path[1..] if path.start_with?(separator) && current.zero?
250
297
 
251
298
  path = [path, separator] unless path.end_with?(separator) || current == last
252
299
 
@@ -263,25 +310,21 @@ module Asciidoctor
263
310
  content_model :simple
264
311
 
265
312
  def process(parent, target, attrs)
266
- lines = target.lines
267
-
268
- lines = ['@startuml'] + target.lines unless target.lines[0] =~ /@startuml/
269
-
270
- lines += ['@enduml'] unless target.lines[-1] =~ /@enduml/
271
-
272
- content = Processor.plantuml_content(lines.join("\n"), attrs)
273
-
274
- create_plantuml_block(parent, content, attrs)
313
+ content = Processor.plantuml_content(parent, target, attrs)
314
+ Processor.create_plantuml_block(parent, content, attrs)
275
315
  end
316
+ end
276
317
 
277
- private
318
+ # PlantUML BlockMacroProcessor
319
+ class BlockMacroProcessor < Asciidoctor::Extensions::BlockMacroProcessor
320
+ use_dsl
321
+ named :plantuml
278
322
 
279
- def create_plantuml_block(parent, content, attrs)
280
- Asciidoctor::Block.new parent, :pass, {
281
- content_model: :raw,
282
- source: content,
283
- subs: :default
284
- }.merge(attrs)
323
+ def process(parent, target, attrs)
324
+ base_dir = parent.document.base_dir
325
+ source_file = parent.document.path_resolver.system_path(target, base_dir, base_dir)
326
+ content = Processor.plantuml_content_from_file(parent, source_file, attrs)
327
+ Processor.create_plantuml_block(parent, content, attrs)
285
328
  end
286
329
  end
287
330
  end
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Asciidoctor
4
4
  module PlantUML
5
- VERSION = '0.0.14'
5
+ VERSION = '0.1.0'
6
6
  end
7
7
  end
@@ -0,0 +1,3 @@
1
+ skinparam monochrome true
2
+ skinparam backgroundColor transparent
3
+ skinparam style strictuml
@@ -0,0 +1,4 @@
1
+ @startuml
2
+ User -> (Start)
3
+ User --> (Use the application) : Label
4
+ @enduml
@@ -184,10 +184,69 @@ DOC_SVG = <<~ENDOFSTRING
184
184
  ----
185
185
  ENDOFSTRING
186
186
 
187
+ DOC_BLOCK_MACRO = <<~ENDOFSTRING
188
+ = Hello PlantUML!
189
+
190
+ .Title Of this
191
+ plantuml::test/fixtures/test.puml[]
192
+ ENDOFSTRING
193
+
194
+ DOC_BLOCK_MACRO_MISSING_FILE = <<~ENDOFSTRING
195
+ = Hello PlantUML!
196
+
197
+ .Title Of this
198
+ plantuml::test/fixtures/missing.puml[]
199
+ ENDOFSTRING
200
+
201
+ DOC_SUBS_ATTRIBUTES = <<~ENDOFSTRING
202
+ = Hello PlantUML!
203
+ :text: Label
204
+
205
+ [plantuml, format="png", subs="attributes+"]
206
+ .Title Of this
207
+ ----
208
+ User -> (Start)
209
+ User --> (Use the application) : {text}
210
+ ----
211
+ ENDOFSTRING
212
+
213
+ DOC_CONFIG_INCLUDE = <<~ENDOFSTRING
214
+ = Hello PlantUML!
215
+ :plantuml-include: test/fixtures/config.puml
216
+
217
+ [plantuml, format="png"]
218
+ .Title Of this
219
+ ----
220
+ User -> (Start)
221
+ User --> (Use the application) : Label
222
+ ----
223
+ ENDOFSTRING
224
+
225
+ DOC_CONFIG_INCLUDE_MISSING_FILE = <<~ENDOFSTRING
226
+ = Hello PlantUML!
227
+ :plantuml-include: test/fixtures/missing.puml
228
+
229
+ [plantuml, format="png"]
230
+ .Title Of this
231
+ ----
232
+ User -> (Start)
233
+ User --> (Use the application) : Label
234
+ ----
235
+ ENDOFSTRING
236
+
237
+ DOC_CONFIG_INCLUDE_MACRO_BLOCK = <<~ENDOFSTRING
238
+ = Hello PlantUML!
239
+ :plantuml-include: test/fixtures/config.puml
240
+
241
+ [plantuml, format="png"]
242
+ plantuml::test/fixtures/test.puml[]
243
+ ENDOFSTRING
244
+
187
245
  class PlantUmlTest < Test::Unit::TestCase
188
246
  GENURL = 'http://localhost:8080/plantuml/png/U9npA2v9B2efpStX2YrEBLBGjLFG20Q9Q4Bv804WIw4a8rKXiQ0W9pCviIGpFqzJmKh19p4fDOVB8JKl1QWT05kd5wq0'
189
247
  GENURL2 = 'http://localhost:8080/plantuml/png/U9npA2v9B2efpStXYdRszmqmZ8NGHh4mleAkdGAAa15G22Pc7Clba9gN0jGE00W75Cm0'
190
248
  GENURL_ENCODING = 'http://localhost:8080/plantuml/png/~1U9npA2v9B2efpStX2YrEBLBGjLFG20Q9Q4Bv804WIw4a8rKXiQ0W9pCviIGpFqzJmKh19p4fDOVB8JKl1QWT05kd5wq0'
249
+ GENURL_CONFIG = 'http://localhost:8080/plantuml/png/~1U9nDZJ4Emp08HVUSWh4PUe4ELQIktQeUW3YeiMA31NZexKEg3bc-Fly1Vp97zLxBO5lcXeeLgh2aLQKIk7OwaHdJzb7fl3oaY0P6ja34Vjeo_nOArPn-dzz62jSxN5v7r_YVZo0S-4g0hPMSqBFm23Tuuanbc8YNEDy1SzOwlG00'
191
250
  SVGGENURL = 'http://localhost:8080/plantuml/svg/~1U9npA2v9B2efpStX2YrEBLBGjLFG20Q9Q4Bv804WIw4a8rKXiQ0W9pCviIGpFqzJmKh19p4fDOVB8JKl1QWT05kd5wq0'
192
251
 
193
252
  def setup
@@ -302,6 +361,76 @@ class PlantUmlTest < Test::Unit::TestCase
302
361
  assert_equal GENURL_ENCODING, element['src']
303
362
  end
304
363
 
364
+ def test_plantuml_block_macro_processor
365
+ html = ::Asciidoctor.convert(StringIO.new(DOC_BLOCK_MACRO), backend: 'html5')
366
+ page = Nokogiri::HTML(html)
367
+
368
+ elements = page.css('img.plantuml')
369
+
370
+ assert_equal elements.size, 1
371
+
372
+ element = elements.first
373
+
374
+ assert_equal GENURL, element['src']
375
+ end
376
+
377
+ def test_should_show_file_error
378
+ html = ::Asciidoctor.convert(StringIO.new(DOC_BLOCK_MACRO_MISSING_FILE), backend: 'html5')
379
+ page = Nokogiri::HTML(html)
380
+
381
+ elements = page.css('pre.plantuml-error')
382
+ assert_equal elements.size, 1
383
+ assert_includes html, 'No such file or directory'
384
+ end
385
+
386
+ def test_plantuml_subs_attributes
387
+ html = ::Asciidoctor.convert(StringIO.new(DOC_SUBS_ATTRIBUTES), backend: 'html5')
388
+ page = Nokogiri::HTML(html)
389
+
390
+ elements = page.css('img.plantuml')
391
+
392
+ assert_equal elements.size, 1
393
+
394
+ element = elements.first
395
+
396
+ assert_equal GENURL_ENCODING, element['src']
397
+ end
398
+
399
+ def test_plantuml_config_include
400
+ html = ::Asciidoctor.convert(StringIO.new(DOC_CONFIG_INCLUDE), backend: 'html5')
401
+ page = Nokogiri::HTML(html)
402
+
403
+ elements = page.css('img.plantuml')
404
+
405
+ assert_equal elements.size, 1
406
+
407
+ element = elements.first
408
+
409
+ assert_equal GENURL_CONFIG, element['src']
410
+ end
411
+
412
+ def test_plantuml_config_include_missing_file
413
+ html = ::Asciidoctor.convert(StringIO.new(DOC_CONFIG_INCLUDE_MISSING_FILE), backend: 'html5')
414
+ page = Nokogiri::HTML(html)
415
+
416
+ elements = page.css('pre.plantuml-error')
417
+ assert_equal elements.size, 1
418
+ assert_includes html, 'No such file or directory'
419
+ end
420
+
421
+ def test_plantuml_config_include_macro_block
422
+ html = ::Asciidoctor.convert(StringIO.new(DOC_CONFIG_INCLUDE_MACRO_BLOCK), backend: 'html5')
423
+ page = Nokogiri::HTML(html)
424
+
425
+ elements = page.css('img.plantuml')
426
+
427
+ assert_equal elements.size, 1
428
+
429
+ element = elements.first
430
+
431
+ assert_equal GENURL_CONFIG, element['src']
432
+ end
433
+
305
434
  def test_plantuml_id_attribute
306
435
  html = ::Asciidoctor.convert(StringIO.new(DOC_ID), backend: 'html5')
307
436
  page = Nokogiri::HTML(html)
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.14
4
+ version: 0.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Horacio Sanson
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2021-05-07 00:00:00.000000000 Z
11
+ date: 2022-04-30 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -16,28 +16,28 @@ dependencies:
16
16
  requirements:
17
17
  - - "~>"
18
18
  - !ruby/object:Gem::Version
19
- version: '1.3'
19
+ version: '2.2'
20
20
  type: :development
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
24
  - - "~>"
25
25
  - !ruby/object:Gem::Version
26
- version: '1.3'
26
+ version: '2.2'
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: nokogiri
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
31
  - - "~>"
32
32
  - !ruby/object:Gem::Version
33
- version: '1.11'
33
+ version: 1.13.4
34
34
  type: :development
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
38
  - - "~>"
39
39
  - !ruby/object:Gem::Version
40
- version: '1.11'
40
+ version: 1.13.4
41
41
  - !ruby/object:Gem::Dependency
42
42
  name: rake
43
43
  requirement: !ruby/object:Gem::Requirement
@@ -58,35 +58,35 @@ dependencies:
58
58
  requirements:
59
59
  - - "~>"
60
60
  - !ruby/object:Gem::Version
61
- version: '1.7'
61
+ version: '1.28'
62
62
  type: :development
63
63
  prerelease: false
64
64
  version_requirements: !ruby/object:Gem::Requirement
65
65
  requirements:
66
66
  - - "~>"
67
67
  - !ruby/object:Gem::Version
68
- version: '1.7'
68
+ version: '1.28'
69
69
  - !ruby/object:Gem::Dependency
70
70
  name: test-unit
71
71
  requirement: !ruby/object:Gem::Requirement
72
72
  requirements:
73
73
  - - "~>"
74
74
  - !ruby/object:Gem::Version
75
- version: '3.3'
75
+ version: '3.5'
76
76
  type: :development
77
77
  prerelease: false
78
78
  version_requirements: !ruby/object:Gem::Requirement
79
79
  requirements:
80
80
  - - "~>"
81
81
  - !ruby/object:Gem::Version
82
- version: '3.3'
82
+ version: '3.5'
83
83
  - !ruby/object:Gem::Dependency
84
84
  name: asciidoctor
85
85
  requirement: !ruby/object:Gem::Requirement
86
86
  requirements:
87
87
  - - ">="
88
88
  - !ruby/object:Gem::Version
89
- version: 1.5.6
89
+ version: 2.0.17
90
90
  - - "<"
91
91
  - !ruby/object:Gem::Version
92
92
  version: 3.0.0
@@ -96,7 +96,7 @@ dependencies:
96
96
  requirements:
97
97
  - - ">="
98
98
  - !ruby/object:Gem::Version
99
- version: 1.5.6
99
+ version: 2.0.17
100
100
  - - "<"
101
101
  - !ruby/object:Gem::Version
102
102
  version: 3.0.0
@@ -110,11 +110,14 @@ files:
110
110
  - lib/asciidoctor-plantuml.rb
111
111
  - lib/asciidoctor_plantuml/plantuml.rb
112
112
  - lib/asciidoctor_plantuml/version.rb
113
+ - test/fixtures/config.puml
114
+ - test/fixtures/test.puml
113
115
  - test/test_plantuml.rb
114
116
  homepage: https://github.com/hsanson/asciidoctor-plantuml
115
117
  licenses:
116
118
  - MIT
117
- metadata: {}
119
+ metadata:
120
+ rubygems_mfa_required: 'true'
118
121
  post_install_message:
119
122
  rdoc_options: []
120
123
  require_paths:
@@ -123,7 +126,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
123
126
  requirements:
124
127
  - - ">="
125
128
  - !ruby/object:Gem::Version
126
- version: '2.5'
129
+ version: '2.6'
127
130
  required_rubygems_version: !ruby/object:Gem::Requirement
128
131
  requirements:
129
132
  - - ">="
@@ -135,4 +138,6 @@ signing_key:
135
138
  specification_version: 4
136
139
  summary: Asciidoctor support for PlantUML diagrams.
137
140
  test_files:
141
+ - test/fixtures/config.puml
142
+ - test/fixtures/test.puml
138
143
  - test/test_plantuml.rb