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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 29d4b4f337f262af6f37dc49c91eed4aa845559b8c4b41241de1e62a3372bbcc
4
- data.tar.gz: 8bb44da1bf73548f7d987115296d5e4f1e68fcee47049b6ce60e86b2be872a06
3
+ metadata.gz: f76765d8311aa663098b90be7247a5a5af3c923a8d43e429f61430d2f6142673
4
+ data.tar.gz: 602d5d12d63fc375a4f3f86df158e5aefdbf237123bc0fc560c3ab89d33ef9e0
5
5
  SHA512:
6
- metadata.gz: c216085705f6caed4a58631f00a9d7c25b28c45ad5f111c13d4ffd506b55003f207c0fa7ab89e5159aa3d213392a00e87e6f1d29ca29de3b7496f487814b0b0c
7
- data.tar.gz: 6d675348900d95665bde246cacf7372f9a739123de2ea83af826b7496672a7d2b3d52c6f4e5ecd3fdeae1611f87bedf57c07949fd78069ce89a560f0fa265f90
6
+ metadata.gz: dccfbb46bae6bd057ec31b8f0a2799c517da693ccc1b8ffe8a96e3dbc792fc6031bd54db34649466558b6346c32ec2712d6b8925afdd9e45c9fd3f5aae320301
7
+ data.tar.gz: 69da6d005b7fc619080b880359b97de16685f05a8f30fb1aca32bc6d1e21963926f83405255ed172a9e4b6d0b8ff360cf8fa4ac3a1c6f7690e3d75bc4603ff5c
data/.rubocop.yml CHANGED
@@ -1,5 +1,5 @@
1
1
  AllCops:
2
- TargetRubyVersion: 2.7
2
+ TargetRubyVersion: 3.2
3
3
  SuggestExtensions: false
4
4
  NewCops: enable
5
5
 
data/Gemfile CHANGED
@@ -3,3 +3,6 @@
3
3
  source 'https://rubygems.org'
4
4
 
5
5
  gemspec
6
+
7
+ # required by testcontainers-core but not declared, and no longer a default gem since Ruby 3.4
8
+ gem 'base64', '~> 0.3.0'
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- asciidoctor-kroki (2.0.0.rc.3)
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
@@ -32,4 +32,5 @@ Gem::Specification.new do |s|
32
32
  s.add_development_dependency 'rake', '~> 13.3.0'
33
33
  s.add_development_dependency 'rspec', '~> 3.13.1'
34
34
  s.add_development_dependency 'rubocop', '~> 1.78'
35
+ s.add_development_dependency 'testcontainers-core', '~> 0.2.0'
35
36
  end
@@ -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; otherwise defaults to the XDG cache
20
- # directory (`$XDG_CACHE_HOME/kroki` or `~/.cache/kroki`).
21
- def resolve_cache_dir(doc)
22
- configured = doc.attr('kroki-cache-dir')
23
- return configured if configured && !configured.empty?
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.exist?(cache_file_path(cache_dir, key, format))
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
- File.write(cache_file_path(cache_dir, key, format), contents, mode: 'wb')
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