Skip to content

Commit

Permalink
resolves #48 use the target attribute as a prefix in the file name (#374
Browse files Browse the repository at this point in the history
)
  • Loading branch information
ggrossetie authored Jun 22, 2022
1 parent 7394bfc commit babad5b
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ def process(processor, parent, attrs, diagram_type, diagram_text, logger)
format = get_format(doc, attrs, diagram_type)
attrs['role'] = get_role(format, attrs['role'])
attrs['format'] = format
kroki_diagram = KrokiDiagram.new(diagram_type, format, diagram_text)
kroki_diagram = KrokiDiagram.new(diagram_type, format, diagram_text, attrs['target'])
kroki_client = KrokiClient.new({
server_url: server_url(doc),
http_method: http_method(doc),
Expand Down Expand Up @@ -269,12 +269,13 @@ class KrokiDiagram
require 'zlib'
require 'digest'

attr_reader :type, :text, :format
attr_reader :type, :text, :format, :target

def initialize(type, format, text)
def initialize(type, format, text, target = nil)
@text = text
@type = type
@format = format
@target = target
end

def get_diagram_uri(server_url)
Expand All @@ -287,7 +288,7 @@ def encode

def save(output_dir_path, kroki_client)
diagram_url = get_diagram_uri(kroki_client.server_url)
diagram_name = "diag-#{Digest::SHA256.hexdigest diagram_url}.#{@format}"
diagram_name = "#{@target || 'diag'}-#{Digest::SHA256.hexdigest diagram_url}.#{@format}"
file_path = File.join(output_dir_path, diagram_name)
encoding = case @format
when 'txt', 'atxt', 'utxt', 'svg'
Expand Down
27 changes: 24 additions & 3 deletions ruby/spec/asciidoctor_kroki_diagram_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,28 @@
output_dir_path = "#{__dir__}/../.asciidoctor/kroki"
diagram_name = kroki_diagram.save(output_dir_path, kroki_client)
diagram_path = File.join(output_dir_path, diagram_name)
expect(File.exist?(diagram_path)).to be_truthy, "expected diagram to be saved at #{diagram_path}"
expect(File.exist?(diagram_path)).to be_truthy, "diagram should be saved at: #{diagram_path}"
content = <<-TXT.chomp
,-----. ,---.
|alice| |bob|
`--+--' `-+-'
| hello |
|-------------->|
,--+--. ,-+-.
|alice| |bob|
`-----' `---'
TXT
expect(File.read(diagram_path).split("\n").map(&:rstrip).join("\n")).to eq(content)
end
it 'should fetch a diagram from Kroki and save it to disk using the target name' do
kroki_diagram = ::AsciidoctorExtensions::KrokiDiagram.new('plantuml', 'txt', ' alice -> bob: hello', 'hello-world')
kroki_http_client = ::AsciidoctorExtensions::KrokiHttpClient
kroki_client = ::AsciidoctorExtensions::KrokiClient.new(server_url: 'https://kroki.io', http_method: 'get', http_client: kroki_http_client)
output_dir_path = "#{__dir__}/../.asciidoctor/kroki"
diagram_name = kroki_diagram.save(output_dir_path, kroki_client)
diagram_path = File.join(output_dir_path, diagram_name)
expect(diagram_name).to start_with('hello-world-'), "diagram name should use the target as a prefix, got: #{diagram_name}"
expect(File.exist?(diagram_path)).to be_truthy, "diagram should be saved at: #{diagram_path}"
content = <<-TXT.chomp
,-----. ,---.
|alice| |bob|
Expand All @@ -55,9 +76,9 @@
expect(kroki_http_client).to receive(:get).once.and_return(diagram_contents)
diagram_name = kroki_diagram.save(output_dir_path, kroki_client)
diagram_path = File.join(output_dir_path, diagram_name)
expect(File.exist?(diagram_path)).to be_truthy, "expected diagram to be saved at #{diagram_path}"
expect(File.exist?(diagram_path)).to be_truthy, "diagram should be saved at: #{diagram_path}"
# calling again... should read the file from disk (and not do a GET request)
kroki_diagram.save(output_dir_path, kroki_client)
expect(File.size(diagram_path)).to be_eql(diagram_contents.length), 'expected diagram to be fully saved on disk'
expect(File.size(diagram_path)).to be_eql(diagram_contents.length), 'diagram should be fully saved on disk'
end
end
2 changes: 1 addition & 1 deletion src/fetch.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ module.exports.save = function (krokiDiagram, doc, target, vfs, krokiClient) {
const imagesOutputDirectory = getImagesOutputDirectory(doc)
const diagramUrl = krokiDiagram.getDiagramUri(krokiClient.getServerUrl())
const format = krokiDiagram.format
const diagramName = `diag-${rusha.createHash().update(diagramUrl).digest('hex')}.${format}`
const diagramName = `${target || 'diag'}-${rusha.createHash().update(diagramUrl).digest('hex')}.${format}`
const filePath = path.format({ dir: imagesOutputDirectory, base: diagramName })
let encoding
let mediaType
Expand Down
2 changes: 1 addition & 1 deletion test/antora/test.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ describe('Antora integration', () => {
expect(imageElements.length).to.equal(6)
imageElements.each((i, imageElement) => {
const src = $(imageElement).attr('src')
expect(src).to.startWith('_images/diag-')
expect(src).to.startWith('_images/ab-')
})
}).timeout(50000)
})
2 changes: 1 addition & 1 deletion test/test.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,7 @@ Hello -> World
extension_registry: registry,
attributes: { 'kroki-fetch-diagram': true }
})
expect(html).to.contain('<img src=".asciidoctor/kroki/diag-7a123c0b2909750ca5526554cd8620774ccf6cd9.svg" alt="hello-world">')
expect(html).to.contain('<img src=".asciidoctor/kroki/hello-world-7a123c0b2909750ca5526554cd8620774ccf6cd9.svg" alt="hello-world">')
}).timeout(5000)
it('should download and save an image to a local folder using a generated unique name (md5sum)', () => {
const input = `
Expand Down

0 comments on commit babad5b

Please sign in to comment.