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, cache_dir: nil, cache_mode: { enabled: false, refresh: false }) ⇒ Object
-
#to_data_uri(kroki_client) ⇒ Object
Fetches this diagram from Kroki and returns it as a
data:URI, embedding the content directly without writing any file (mirrors the JavaScript extension's fetch.js#toDataUri).
Constructor Details
#initialize(type, format, text, target = nil, opts = {}) ⇒ KrokiDiagram
Returns a new instance of KrokiDiagram.
402 403 404 405 406 407 408 |
# File 'lib/asciidoctor/extensions/asciidoctor_kroki/extension.rb', line 402 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.
400 401 402 |
# File 'lib/asciidoctor/extensions/asciidoctor_kroki/extension.rb', line 400 def format @format end |
#opts ⇒ Object (readonly)
Returns the value of attribute opts.
400 401 402 |
# File 'lib/asciidoctor/extensions/asciidoctor_kroki/extension.rb', line 400 def opts @opts end |
#target ⇒ Object (readonly)
Returns the value of attribute target.
400 401 402 |
# File 'lib/asciidoctor/extensions/asciidoctor_kroki/extension.rb', line 400 def target @target end |
#text ⇒ Object (readonly)
Returns the value of attribute text.
400 401 402 |
# File 'lib/asciidoctor/extensions/asciidoctor_kroki/extension.rb', line 400 def text @text end |
#type ⇒ Object (readonly)
Returns the value of attribute type.
400 401 402 |
# File 'lib/asciidoctor/extensions/asciidoctor_kroki/extension.rb', line 400 def type @type end |
Instance Method Details
#encode ⇒ Object
415 416 417 |
# File 'lib/asciidoctor/extensions/asciidoctor_kroki/extension.rb', line 415 def encode ([Zlib::Deflate.deflate(@text, 9)].pack 'm0').tr '+/', '-_' end |
#get_diagram_uri(server_url) ⇒ Object
410 411 412 413 |
# File 'lib/asciidoctor/extensions/asciidoctor_kroki/extension.rb', line 410 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, cache_dir: nil, cache_mode: { enabled: false, refresh: false }) ⇒ Object
422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 |
# File 'lib/asciidoctor/extensions/asciidoctor_kroki/extension.rb', line 422 def save(output_dir_path, kroki_client, generated_files = nil, logger = nil, cache_dir: nil, cache_mode: { enabled: false, refresh: false }) 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 it cannot be # trusted by name alone: go through fetch_diagram, which re-fetches only when the # persistent cache doesn't already have this exact content (see #90). Warn when the # same name is reused for a diagram with different content. 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, cache_dir, cache_mode) elsif !File.exist?(file_path) # Content-addressed name: an existing output file necessarily has identical content. fetch_and_write(output_dir_path, file_path, kroki_client, cache_dir, cache_mode) end diagram_name end |
#to_data_uri(kroki_client) ⇒ Object
Fetches this diagram from Kroki and returns it as a data: URI, embedding the
content directly without writing any file (mirrors the JavaScript extension's
fetch.js#toDataUri).
447 448 449 450 |
# File 'lib/asciidoctor/extensions/asciidoctor_kroki/extension.rb', line 447 def to_data_uri(kroki_client) contents = kroki_client.get_image(self, image_encoding) "data:#{media_type};base64,#{[contents].pack 'm0'}" end |