Skip to content

Commit

Permalink
Regenerate Ditaa diagram when options string changes
Browse files Browse the repository at this point in the history
  • Loading branch information
Pepijn Van Eeckhoudt authored and Pepijn Van Eeckhoudt committed Mar 28, 2015
1 parent e7ddf13 commit 5149ac6
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 0 deletions.
17 changes: 17 additions & 0 deletions lib/asciidoctor-diagram/ditaa/extension.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,23 @@ def self.included(mod)
ditaa(c.to_s, attrs)
end
end

def create_source(parent, reader, attributes)
DitaaSource.new(reader, attributes)
end

class DitaaSource < Asciidoctor::Diagram::Extensions::ReaderSource
def should_process?(image_file, image_metadata)
super(image_file, image_metadata) || image_metadata['options'] != attributes['options']
end

def create_image_metadata
{
'checksum' => checksum,
'options' => attributes['options']
}
end
end
end

class DitaaBlockProcessor < Extensions::DiagramBlockProcessor
Expand Down
2 changes: 2 additions & 0 deletions lib/asciidoctor-diagram/extensions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,8 @@ def create_image_metadata
class BasicSource
include DiagramSource

attr_reader :attributes

def initialize(attributes)
@attributes = attributes
end
Expand Down
35 changes: 35 additions & 0 deletions spec/ditaa_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -149,4 +149,39 @@
expect(target).to match /\.png$/
expect(File.exists?(target)).to be true
end

it "should regenerate images when options change" do
doc = <<-eos
= Hello, PlantUML!
Doc Writer <[email protected]>
== First Section
[ditaa, options="{opts}"]
----
+--------+ +-------+ +-------+
| | --+ ditaa +--> | |
| Text | +-------+ |diagram|
|Document| |!magic!| | |
| {d}| | | | |
+---+----+ +-------+ +-------+
: ^
| Lots of work |
+-------------------------+
----
eos

d = Asciidoctor.load StringIO.new(doc.sub('{opts}', 'no-shadow'))
b = d.find { |b| b.context == :image }
target = b.attributes['target']
mtime1 = File.mtime(target)

sleep 1

d = Asciidoctor.load StringIO.new(doc.sub('{opts}', 'round-corners'))

mtime2 = File.mtime(target)

expect(mtime2).to be > mtime1
end
end

0 comments on commit 5149ac6

Please sign in to comment.