asciidoctor-kroki 2.0.0.rc.3 → 2.0.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 +4 -4
- data/.rubocop.yml +1 -1
- data/Gemfile +3 -0
- data/Gemfile.lock +13 -1
- data/asciidoctor-kroki.gemspec +1 -0
- data/lib/asciidoctor/extensions/asciidoctor_kroki/attributes.rb +15 -0
- data/lib/asciidoctor/extensions/asciidoctor_kroki/cache.rb +53 -7
- data/lib/asciidoctor/extensions/asciidoctor_kroki/extension.rb +167 -78
- data/lib/asciidoctor/extensions/asciidoctor_kroki/http_limits.rb +37 -0
- data/lib/asciidoctor/extensions/asciidoctor_kroki/path_policy.rb +49 -0
- data/lib/asciidoctor/extensions/asciidoctor_kroki/preprocess.rb +48 -23
- data/lib/asciidoctor/extensions/asciidoctor_kroki/version.rb +1 -1
- data/lib/asciidoctor/extensions/asciidoctor_kroki.rb +2 -0
- data/spec/asciidoctor_kroki_attributes_spec.rb +102 -0
- data/spec/asciidoctor_kroki_block_macro_spec.rb +36 -3
- data/spec/asciidoctor_kroki_cache_spec.rb +31 -1
- data/spec/asciidoctor_kroki_client_spec.rb +23 -0
- data/spec/asciidoctor_kroki_diagram_spec.rb +88 -22
- data/spec/asciidoctor_kroki_ditaa_spec.rb +53 -0
- data/spec/asciidoctor_kroki_read_policy_spec.rb +268 -0
- data/spec/asciidoctor_kroki_spec.rb +181 -30
- data/spec/rspec_helper.rb +36 -0
- metadata +21 -1
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: f76765d8311aa663098b90be7247a5a5af3c923a8d43e429f61430d2f6142673
|
|
4
|
+
data.tar.gz: 602d5d12d63fc375a4f3f86df158e5aefdbf237123bc0fc560c3ab89d33ef9e0
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: dccfbb46bae6bd057ec31b8f0a2799c517da693ccc1b8ffe8a96e3dbc792fc6031bd54db34649466558b6346c32ec2712d6b8925afdd9e45c9fd3f5aae320301
|
|
7
|
+
data.tar.gz: 69da6d005b7fc619080b880359b97de16685f05a8f30fb1aca32bc6d1e21963926f83405255ed172a9e4b6d0b8ff360cf8fa4ac3a1c6f7690e3d75bc4603ff5c
|
data/.rubocop.yml
CHANGED
data/Gemfile
CHANGED
data/Gemfile.lock
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
PATH
|
|
2
2
|
remote: .
|
|
3
3
|
specs:
|
|
4
|
-
asciidoctor-kroki (2.0.0
|
|
4
|
+
asciidoctor-kroki (2.0.0)
|
|
5
5
|
asciidoctor (~> 2.0)
|
|
6
6
|
|
|
7
7
|
GEM
|
|
@@ -9,10 +9,18 @@ GEM
|
|
|
9
9
|
specs:
|
|
10
10
|
asciidoctor (2.0.26)
|
|
11
11
|
ast (2.4.3)
|
|
12
|
+
base64 (0.3.0)
|
|
12
13
|
diff-lcs (1.6.2)
|
|
14
|
+
docker-api (2.4.0)
|
|
15
|
+
excon (>= 0.64.0)
|
|
16
|
+
multi_json
|
|
17
|
+
excon (1.2.5)
|
|
18
|
+
logger
|
|
13
19
|
json (2.13.0)
|
|
14
20
|
language_server-protocol (3.17.0.5)
|
|
15
21
|
lint_roller (1.1.0)
|
|
22
|
+
logger (1.7.0)
|
|
23
|
+
multi_json (1.15.0)
|
|
16
24
|
parallel (1.27.0)
|
|
17
25
|
parser (3.3.8.0)
|
|
18
26
|
ast (~> 2.4.1)
|
|
@@ -50,6 +58,8 @@ GEM
|
|
|
50
58
|
parser (>= 3.3.7.2)
|
|
51
59
|
prism (~> 1.4)
|
|
52
60
|
ruby-progressbar (1.13.0)
|
|
61
|
+
testcontainers-core (0.2.0)
|
|
62
|
+
docker-api (~> 2.2)
|
|
53
63
|
unicode-display_width (3.1.4)
|
|
54
64
|
unicode-emoji (~> 4.0, >= 4.0.4)
|
|
55
65
|
unicode-emoji (4.0.4)
|
|
@@ -59,9 +69,11 @@ PLATFORMS
|
|
|
59
69
|
|
|
60
70
|
DEPENDENCIES
|
|
61
71
|
asciidoctor-kroki!
|
|
72
|
+
base64 (~> 0.3.0)
|
|
62
73
|
rake (~> 13.3.0)
|
|
63
74
|
rspec (~> 3.13.1)
|
|
64
75
|
rubocop (~> 1.78)
|
|
76
|
+
testcontainers-core (~> 0.2.0)
|
|
65
77
|
|
|
66
78
|
BUNDLED WITH
|
|
67
79
|
2.3.15
|
data/asciidoctor-kroki.gemspec
CHANGED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module AsciidoctorExtensions
|
|
4
|
+
# Reads attribute values, the same way as src/attributes.js in the JavaScript/Node.js extension.
|
|
5
|
+
module KrokiAttributes
|
|
6
|
+
# Returns the value of an attribute as a stripped string, or nil if it has none (unset, empty,
|
|
7
|
+
# blank, or `true` for a flag set through the API), so that the caller's default applies.
|
|
8
|
+
def self.value(value)
|
|
9
|
+
return if value == true
|
|
10
|
+
|
|
11
|
+
value = value.to_s.strip
|
|
12
|
+
value unless value.empty?
|
|
13
|
+
end
|
|
14
|
+
end
|
|
15
|
+
end
|
|
@@ -3,6 +3,9 @@
|
|
|
3
3
|
require 'digest'
|
|
4
4
|
require 'fileutils'
|
|
5
5
|
require 'json'
|
|
6
|
+
require 'securerandom'
|
|
7
|
+
require_relative 'attributes'
|
|
8
|
+
require_relative 'path_policy'
|
|
6
9
|
|
|
7
10
|
module AsciidoctorExtensions
|
|
8
11
|
# Persistent, content-addressed cache for fetched diagrams, independent of the output
|
|
@@ -16,11 +19,15 @@ module AsciidoctorExtensions
|
|
|
16
19
|
|
|
17
20
|
class << self
|
|
18
21
|
# Resolves the persistent cache directory.
|
|
19
|
-
# Uses the `kroki-cache-dir` attribute when set
|
|
20
|
-
# directory (`$XDG_CACHE_HOME/kroki` or `~/.cache/kroki`).
|
|
21
|
-
def resolve_cache_dir(doc)
|
|
22
|
-
configured = doc.attr('kroki-cache-dir')
|
|
23
|
-
|
|
22
|
+
# Uses the `kroki-cache-dir` attribute when set and allowed by the safe mode; otherwise
|
|
23
|
+
# defaults to the XDG cache directory (`$XDG_CACHE_HOME/kroki` or `~/.cache/kroki`).
|
|
24
|
+
def resolve_cache_dir(doc, logger = ::Asciidoctor::LoggerManager.logger)
|
|
25
|
+
configured = KrokiAttributes.value(doc.attr('kroki-cache-dir'))
|
|
26
|
+
if configured
|
|
27
|
+
return configured if KrokiPathPolicy.cache_dir_allowed?(doc, configured)
|
|
28
|
+
|
|
29
|
+
warn_ignored_cache_dir(doc, configured, logger)
|
|
30
|
+
end
|
|
24
31
|
|
|
25
32
|
xdg_cache_home = ENV['XDG_CACHE_HOME'] || File.join(Dir.home, '.cache')
|
|
26
33
|
File.join(xdg_cache_home, 'kroki')
|
|
@@ -47,6 +54,24 @@ module AsciidoctorExtensions
|
|
|
47
54
|
{ enabled: value != 'false', refresh: value == 'refresh' }
|
|
48
55
|
end
|
|
49
56
|
|
|
57
|
+
# Persistent cache settings for a document, as keyword arguments for KrokiDiagram#save
|
|
58
|
+
# and KrokiDiagram#to_data_uri. The cache is an optimisation: failing to read or write
|
|
59
|
+
# it must never prevent a diagram from being rendered (e.g. read-only file system or
|
|
60
|
+
# unwritable home directory), so failures are only reported, once per document rather
|
|
61
|
+
# than once per diagram.
|
|
62
|
+
def options(doc, logger)
|
|
63
|
+
cache_dir = resolve_cache_dir(doc, logger)
|
|
64
|
+
on_cache_error = lambda do |error|
|
|
65
|
+
next if doc.instance_variable_get(:@kroki_cache_warned)
|
|
66
|
+
|
|
67
|
+
doc.instance_variable_set(:@kroki_cache_warned, true)
|
|
68
|
+
logger.warn "kroki: unable to use the diagram cache directory '#{cache_dir}' (#{error.message}); " \
|
|
69
|
+
'diagrams will be fetched from the server. Set kroki-cache-dir to a writable directory, ' \
|
|
70
|
+
'or set kroki-cache to false to disable the cache.'
|
|
71
|
+
end
|
|
72
|
+
{ cache_dir: cache_dir, cache_mode: resolve_cache_mode(doc, logger), on_cache_error: on_cache_error }
|
|
73
|
+
end
|
|
74
|
+
|
|
50
75
|
# Computes the content-addressed cache key for a diagram.
|
|
51
76
|
#
|
|
52
77
|
# Deliberately host-dependent: the server URL is part of the key because two Kroki
|
|
@@ -60,8 +85,10 @@ module AsciidoctorExtensions
|
|
|
60
85
|
end
|
|
61
86
|
|
|
62
87
|
# Whether a diagram is already present in the cache.
|
|
88
|
+
# An empty file is treated as missing: it can only be the leftover of an interrupted
|
|
89
|
+
# write (from a version that did not write atomically).
|
|
63
90
|
def exists_in_cache?(cache_dir, key, format)
|
|
64
|
-
File.
|
|
91
|
+
!File.size?(cache_file_path(cache_dir, key, format)).nil?
|
|
65
92
|
end
|
|
66
93
|
|
|
67
94
|
# Reads a cached diagram.
|
|
@@ -70,13 +97,32 @@ module AsciidoctorExtensions
|
|
|
70
97
|
end
|
|
71
98
|
|
|
72
99
|
# Writes a diagram to the cache, creating the cache directory if needed.
|
|
100
|
+
# The content is written to a temporary file then renamed, so that an interrupted write
|
|
101
|
+
# never leaves a truncated entry behind, and concurrent builds sharing the cache never
|
|
102
|
+
# read a partially written one.
|
|
73
103
|
def write_to_cache(cache_dir, key, format, contents)
|
|
74
104
|
FileUtils.mkdir_p(cache_dir)
|
|
75
|
-
|
|
105
|
+
target = cache_file_path(cache_dir, key, format)
|
|
106
|
+
# Unique per writer, and in the same directory so the rename stays on one file system.
|
|
107
|
+
tmp = "#{target}.#{Process.pid}-#{SecureRandom.hex(4)}.tmp"
|
|
108
|
+
begin
|
|
109
|
+
File.write(tmp, contents, mode: 'wb')
|
|
110
|
+
File.rename(tmp, target)
|
|
111
|
+
rescue StandardError
|
|
112
|
+
FileUtils.rm_f(tmp)
|
|
113
|
+
raise
|
|
114
|
+
end
|
|
76
115
|
end
|
|
77
116
|
|
|
78
117
|
private
|
|
79
118
|
|
|
119
|
+
def warn_ignored_cache_dir(doc, cache_dir, logger)
|
|
120
|
+
return if doc.instance_variable_get(:@kroki_cache_dir_ignored)
|
|
121
|
+
|
|
122
|
+
doc.instance_variable_set(:@kroki_cache_dir_ignored, true)
|
|
123
|
+
logger.warn "kroki: ignoring the kroki-cache-dir attribute '#{cache_dir}', because it is outside of the base directory."
|
|
124
|
+
end
|
|
125
|
+
|
|
80
126
|
def cache_file_path(cache_dir, key, format)
|
|
81
127
|
File.join(cache_dir, "#{key}.#{format}")
|
|
82
128
|
end
|