Skip to content

Commit

Permalink
resolves #48 use the target attribute as a prefix in the file name
Browse files Browse the repository at this point in the history
  • Loading branch information
ggrossetie committed Jun 8, 2022
1 parent 082e6d0 commit 5084b86
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,7 @@ class KrokiDiagram

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

def initialize(type, format, text, target)
def initialize(type, format, text, target = nil)
@text = text
@type = type
@format = format
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 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 and generated name', () => {
const input = `
Expand Down

0 comments on commit 5084b86

Please sign in to comment.