asciidoctor-mermaid 0.1.1 → 0.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: bc6cd61b1b5833e22f2f3865f120f7972e117a7a25a1636e25a387cd9b6e320e
4
- data.tar.gz: c185efdacc78a3a50f649126dc16566008c4b7f6f1d4ac93c3ac66bdb0bbf0cb
3
+ metadata.gz: 577319efee650e33d1db7ee0a56917ae8b6107fdfaeee67ea9018a857c189529
4
+ data.tar.gz: 5c2df794560ef01598f5dd475310e894a9f68bd60b4b0a20d4acaf870536180c
5
5
  SHA512:
6
- metadata.gz: 4580feaec78a7cb864e6f62e1fc8753c3c4fae3e709df8ae0a444c20487921e7e4cd1fc0991b240f06fbc250ad52a4936d6847d24fbd65035e5da2ec63b69d18
7
- data.tar.gz: 39e5f2d9127491299d97b1839d52c0eecfcb2b0ef25b0a47c82ca9c3605772f717b0d8a0516b9718de3901dfe08007dd2a6aac9539ac1631ae63c338353e9e81
6
+ metadata.gz: 3996fb7d9d1c3a15a0013489763154ee68881a05a5f1e67d3f0a9d31310899ed717f201e33edd835154022edad3be3b5592b8db0fc70b1fa6514f78641eb8cab
7
+ data.tar.gz: 12a5dc56e46770bb26b41fb2b800fc5314d05b67b7d7a42ad62c41a4dedf7ac232ee930c0b298eb07b8001ad43cec17d651338549631c70ae264d795082677de
@@ -6,13 +6,17 @@ require_relative 'cli'
6
6
  class MermaidTreeProcessor < Asciidoctor::Extensions::TreeProcessor
7
7
  def process(document)
8
8
  document.blocks.map! do |block|
9
- if block.attributes['style'] === 'source' &&
9
+ if block.respond_to?(:blocks) && !block.blocks.empty?
10
+ process(block)
11
+ elsif block.respond_to?(:attributes) &&
12
+ block.attributes['style'] === 'source' &&
10
13
  block.attributes['language'] === 'mermaid'
11
14
  self.convert_block(block)
12
15
  else
13
16
  block
14
17
  end
15
18
  end
19
+ document
16
20
  end
17
21
 
18
22
  protected
@@ -0,0 +1,45 @@
1
+ # An alternative entry point to be used w/ asciidoctor-pdf
2
+ require 'asciidoctor/extensions'
3
+ require 'digest/md5'
4
+ require 'tempfile'
5
+ require_relative 'cli'
6
+
7
+ class MermaidPDFTreeProcessor < Asciidoctor::Extensions::TreeProcessor
8
+ def process(document)
9
+ document.blocks.map! do |block|
10
+ if block.respond_to?(:blocks) && !block.blocks.empty?
11
+ process(block)
12
+ elsif block.respond_to?(:attributes) &&
13
+ block.attributes['style'] === 'source' &&
14
+ block.attributes['language'] === 'mermaid'
15
+ self.convert_block(block)
16
+ else
17
+ block
18
+ end
19
+ end
20
+ document
21
+ end
22
+
23
+ protected
24
+
25
+ def convert_block(block)
26
+ # TODO: Currently I'm unsure how to clean up tmp images after pdf generation completes
27
+ img = self.render_svg(block.source)
28
+ Asciidoctor::Block.new block.parent,
29
+ :image,
30
+ attributes: {
31
+ 'imagesdir' => nil,
32
+ 'target' => img.path,
33
+ 'align' => 'center',
34
+ }
35
+ end
36
+
37
+ def render_svg(content)
38
+ file_name = Digest::MD5.hexdigest(content)
39
+ output_file = Tempfile.new([file_name, '.png'])
40
+ ::Asciidoctor::Mermaid::Cli.run(content, "-o#{output_file.path}")
41
+ output_file
42
+ end
43
+ end
44
+
45
+ Asciidoctor::Extensions.register { treeprocessor MermaidPDFTreeProcessor }
@@ -1,5 +1,5 @@
1
1
  module Asciidoctor
2
2
  module Mermaid
3
- VERSION = '0.1.1'
3
+ VERSION = '0.4.0'
4
4
  end
5
5
  end
@@ -1,8 +1,7 @@
1
1
  require 'asciidoctor'
2
2
  require 'asciidoctor-mermaid'
3
- require 'pry'
4
3
 
5
- MERMAID_DOC = <<-eos
4
+ mermaid_doc = <<-eos
6
5
  lorem ipsum
7
6
 
8
7
  ```mermaid
@@ -11,9 +10,37 @@ flowchart LR
11
10
  ```
12
11
  eos
13
12
 
13
+ array_handling = <<-eos
14
+ [glossary]
15
+ == Glossary
16
+
17
+ - [[g-foo, FOO]]
18
+ FOO::
19
+
20
+ This is the first entry
21
+
22
+ - [[g-bar, BAR]]
23
+ BAR::
24
+
25
+ This is the second entry
26
+ eos
27
+
14
28
  describe 'extension' do
29
+ before do
30
+ Asciidoctor::Extensions.unregister_all
31
+ Asciidoctor::Extensions.register { treeprocessor MermaidTreeProcessor }
32
+ end
33
+
15
34
  it 'renders a basic mermaid diagram' do
16
- doc = Asciidoctor.convert MERMAID_DOC
35
+ doc = Asciidoctor.convert mermaid_doc
36
+ expect(doc).to include('id="mermaid-')
17
37
  expect(doc).to include('flowchart node text')
18
38
  end
39
+
40
+ # On v0.3.0 this should fail with NoMethodError: undefined method `blocks' for #<Array...>
41
+ it 'renders a document containing arrays' do
42
+ doc = Asciidoctor.convert array_handling
43
+ expect(doc).to include('FOO')
44
+ expect(doc).to include('BAR')
45
+ end
19
46
  end
@@ -0,0 +1,46 @@
1
+ require 'asciidoctor'
2
+ require 'asciidoctor-mermaid/pdf'
3
+
4
+ mermaid_doc = <<-eos
5
+ lorem ipsum
6
+
7
+ ```mermaid
8
+ flowchart LR
9
+ id1[flowchart node text]
10
+ ```
11
+ eos
12
+
13
+ array_handling = <<-eos
14
+ [glossary]
15
+ == Glossary
16
+
17
+ - [[g-foo, FOO]]
18
+ FOO::
19
+
20
+ This is the first entry
21
+
22
+ - [[g-bar, BAR]]
23
+ BAR::
24
+
25
+ This is the second entry
26
+ eos
27
+
28
+ describe 'pdf extension' do
29
+ before do
30
+ Asciidoctor::Extensions.unregister_all
31
+ Asciidoctor::Extensions.register { treeprocessor MermaidPDFTreeProcessor }
32
+ end
33
+
34
+ it 'renders a basic mermaid diagram' do
35
+ doc = Asciidoctor.convert mermaid_doc
36
+ expect(doc).to include('class="imageblock text-center"')
37
+ expect(doc).to include('<img src=')
38
+ end
39
+
40
+ # On v0.3.0 this should fail with NoMethodError: undefined method `blocks' for #<Array...>
41
+ it 'renders a document containing arrays' do
42
+ doc = Asciidoctor.convert array_handling
43
+ expect(doc).to include('FOO')
44
+ expect(doc).to include('BAR')
45
+ end
46
+ end
data/spec/spec_helper.rb CHANGED
@@ -1,4 +1,4 @@
1
- require 'rspec/snapshot'
1
+ require 'asciidoctor'
2
2
 
3
3
  # This file was generated by the `rspec --init` command. Conventionally, all
4
4
  # specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: asciidoctor-mermaid
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Justin Bennett
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2022-03-05 00:00:00.000000000 Z
11
+ date: 2022-06-24 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -104,8 +104,10 @@ files:
104
104
  - lib/asciidoctor-mermaid.rb
105
105
  - lib/asciidoctor-mermaid/cli.rb
106
106
  - lib/asciidoctor-mermaid/extension.rb
107
+ - lib/asciidoctor-mermaid/pdf.rb
107
108
  - lib/asciidoctor-mermaid/version.rb
108
109
  - spec/extension_spec.rb
110
+ - spec/pdf_extension_spec.rb
109
111
  - spec/spec_helper.rb
110
112
  homepage: https://github.com/oxidecomputer/asciidoctor-mermaid
111
113
  licenses:
@@ -133,4 +135,5 @@ summary: An Asciidoctor extension that transforms Mermaid diagrams in markdown c
133
135
  to inline svgs.
134
136
  test_files:
135
137
  - spec/extension_spec.rb
138
+ - spec/pdf_extension_spec.rb
136
139
  - spec/spec_helper.rb