Class: AsciidoctorExtensions::KrokiDiagram
- Inherits:
-
Object
- Object
- AsciidoctorExtensions::KrokiDiagram
- Defined in:
- lib/asciidoctor/extensions/asciidoctor_kroki/extension.rb
Overview
Kroki diagram
Instance Attribute Summary collapse
-
#format ⇒ Object
readonly
Returns the value of attribute format.
-
#opts ⇒ Object
readonly
Returns the value of attribute opts.
-
#target ⇒ Object
readonly
Returns the value of attribute target.
-
#text ⇒ Object
readonly
Returns the value of attribute text.
-
#type ⇒ Object
readonly
Returns the value of attribute type.
Instance Method Summary collapse
- #encode ⇒ Object
- #get_diagram_uri(server_url) ⇒ Object
-
#initialize(type, format, text, target = nil, opts = {}) ⇒ KrokiDiagram
constructor
A new instance of KrokiDiagram.
- #save(output_dir_path, kroki_client, generated_files = nil, logger = nil) ⇒ Object
Constructor Details
#initialize(type, format, text, target = nil, opts = {}) ⇒ KrokiDiagram
Returns a new instance of KrokiDiagram.
319 320 321 322 323 324 325 |
# File 'lib/asciidoctor/extensions/asciidoctor_kroki/extension.rb', line 319 def initialize(type, format, text, target = nil, opts = {}) @text = text @type = type @format = format @target = target @opts = opts end |
Instance Attribute Details
#format ⇒ Object (readonly)
Returns the value of attribute format.
317 318 319 |
# File 'lib/asciidoctor/extensions/asciidoctor_kroki/extension.rb', line 317 def format @format end |
#opts ⇒ Object (readonly)
Returns the value of attribute opts.
317 318 319 |
# File 'lib/asciidoctor/extensions/asciidoctor_kroki/extension.rb', line 317 def opts @opts end |
#target ⇒ Object (readonly)
Returns the value of attribute target.
317 318 319 |
# File 'lib/asciidoctor/extensions/asciidoctor_kroki/extension.rb', line 317 def target @target end |
#text ⇒ Object (readonly)
Returns the value of attribute text.
317 318 319 |
# File 'lib/asciidoctor/extensions/asciidoctor_kroki/extension.rb', line 317 def text @text end |
#type ⇒ Object (readonly)
Returns the value of attribute type.
317 318 319 |
# File 'lib/asciidoctor/extensions/asciidoctor_kroki/extension.rb', line 317 def type @type end |
Instance Method Details
#encode ⇒ Object
332 333 334 |
# File 'lib/asciidoctor/extensions/asciidoctor_kroki/extension.rb', line 332 def encode ([Zlib::Deflate.deflate(@text, 9)].pack 'm0').tr '+/', '-_' end |
#get_diagram_uri(server_url) ⇒ Object
327 328 329 330 |
# File 'lib/asciidoctor/extensions/asciidoctor_kroki/extension.rb', line 327 def get_diagram_uri(server_url) query_params = opts.map { |k, v| "#{k}=#{_url_encode(v.to_s)}" }.join('&') unless opts.empty? _join_uri_segments(server_url, @type, @format, encode) + (query_params ? "?#{query_params}" : '') end |
#save(output_dir_path, kroki_client, generated_files = nil, logger = nil) ⇒ Object
336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 |
# File 'lib/asciidoctor/extensions/asciidoctor_kroki/extension.rb', line 336 def save(output_dir_path, kroki_client, generated_files = nil, logger = nil) diagram_url = get_diagram_uri(kroki_client.server_url) # An explicit name is used verbatim so links stay stable across content changes; # otherwise the name is content-addressed so anonymous diagrams don't collide (see #451). named = @target.is_a?(::String) && !@target.empty? diagram_name = named ? "#{@target}.#{@format}" : "diag-#{Digest::SHA256.hexdigest diagram_url}.#{@format}" file_path = File.join(output_dir_path, diagram_name) if named # A stable file may exist from a previous build with stale content, so always # re-fetch and overwrite. Warn when the same name is reused for a different diagram. warn_on_name_clash(generated_files, diagram_name, diagram_url, logger) generated_files[diagram_name] = diagram_url if generated_files fetch_and_write(output_dir_path, file_path, kroki_client) elsif !File.exist?(file_path) # Content-addressed name: an existing file necessarily has identical content. fetch_and_write(output_dir_path, file_path, kroki_client) end diagram_name end |