jekyll-diagrams 0.9.1 → 0.9.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (43) hide show
  1. checksums.yaml +4 -4
  2. data/.github/workflows/test.yml +83 -0
  3. data/.gitignore +4 -5
  4. data/.rubocop.yml +22 -0
  5. data/.simplecov +5 -0
  6. data/.travis.yml +48 -1
  7. data/Dockerfile +14 -12
  8. data/Gemfile +4 -1
  9. data/README.md +49 -21
  10. data/Rakefile +20 -2
  11. data/features/blockdiag.feature +20 -0
  12. data/features/graphviz.feature +22 -0
  13. data/features/mermaid.feature +21 -0
  14. data/features/nomnoml.feature +36 -0
  15. data/features/plantuml.feature +24 -0
  16. data/features/smcat.feature +29 -0
  17. data/features/step_definitions/jekyll-diagrams.rb +25 -0
  18. data/features/support/env.rb +33 -0
  19. data/features/svgbob.feature +22 -0
  20. data/features/syntrax.feature +22 -0
  21. data/features/vega.feature +32 -0
  22. data/features/wavedrom.feature +22 -0
  23. data/jekyll-diagrams.gemspec +13 -7
  24. data/lib/jekyll-diagrams.rb +3 -2
  25. data/lib/jekyll-diagrams/block.rb +5 -4
  26. data/lib/jekyll-diagrams/blockdiag.rb +9 -7
  27. data/lib/jekyll-diagrams/erd.rb +9 -6
  28. data/lib/jekyll-diagrams/graphviz.rb +12 -17
  29. data/lib/jekyll-diagrams/mermaid.rb +21 -18
  30. data/lib/jekyll-diagrams/nomnoml.rb +6 -4
  31. data/lib/jekyll-diagrams/plantuml.rb +8 -4
  32. data/lib/jekyll-diagrams/smcat.rb +10 -7
  33. data/lib/jekyll-diagrams/svgbob.rb +6 -4
  34. data/lib/jekyll-diagrams/syntrax.rb +8 -6
  35. data/lib/jekyll-diagrams/util.rb +78 -11
  36. data/lib/jekyll-diagrams/vega.rb +8 -5
  37. data/lib/jekyll-diagrams/version.rb +4 -2
  38. data/lib/jekyll-diagrams/wavedrom.rb +6 -4
  39. data/test/erd_test.rb +22 -0
  40. data/test/test_helper.rb +4 -7
  41. data/test/util_test.rb +20 -0
  42. metadata +54 -11
  43. data/lib/jekyll-diagrams/renderer.rb +0 -53
@@ -1,29 +1,32 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Jekyll
2
- module Diagrams
3
- class MermaidBlock < Block
4
- CONFIGURATIONS = %w( backgroundColor height theme width).freeze
4
+ module Diagrams
5
+ class MermaidBlock < Block
6
+ CONFIGURATIONS = %w[backgroundColor height theme width].freeze
5
7
 
6
- def render_svg(code, config)
7
- command = build_command(config)
8
+ def render_svg(code, config)
9
+ command = build_command(config)
8
10
 
9
- render_with_tempfile(command, code) do |command, input, output|
10
- "#{command} --input #{input} --output #{output}"
11
- end
11
+ render_with_tempfile(command, code) do |input, output|
12
+ "--input #{input} --output #{output}"
12
13
  end
14
+ end
13
15
 
14
- def build_command(config)
15
- command = 'mmdc'
16
- command << ' --transparent' if config.has_key?('transparent')
17
- command << " --puppeteerConfigFile #{vendor_path('mermaid_puppeteer_config.json')}"
18
-
19
- CONFIGURATIONS.each do |conf|
20
- command << " --#{conf} #{config[conf]}" if config.has_key?(conf)
21
- end
16
+ def build_command(config)
17
+ command = +'mmdc'
18
+ command << ' --puppeteerConfigFile '
19
+ command << vendor_path('mermaid_puppeteer_config.json')
20
+ command << ' --transparent' if config.fetch('transparent', false) != false
22
21
 
23
- command
22
+ CONFIGURATIONS.each do |conf|
23
+ command << " --#{conf} #{config[conf]}" if config.key?(conf)
24
24
  end
25
+
26
+ command
25
27
  end
26
28
  end
27
29
  end
30
+ end
28
31
 
29
- Liquid::Template.register_tag(:mermaid, Jekyll::Diagrams::MermaidBlock)
32
+ Liquid::Template.register_tag(:mermaid, Jekyll::Diagrams::MermaidBlock)
@@ -1,19 +1,21 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Jekyll
2
4
  module Diagrams
3
5
  class NomnomlBlock < Block
4
6
  def render_svg(code, config)
5
7
  command = build_command(config)
6
8
 
7
- render_with_tempfile(command, code, stdout: true) do |command, input|
8
- "#{command} #{input}"
9
+ render_with_tempfile(command, code, stdout: true) do |input|
10
+ input
9
11
  end
10
12
  end
11
13
 
12
- def build_command(config)
14
+ def build_command(_config)
13
15
  'nomnoml'
14
16
  end
15
17
  end
16
18
  end
17
19
  end
18
20
 
19
- Liquid::Template.register_tag(:nomnoml, Jekyll::Diagrams::NomnomlBlock)
21
+ Liquid::Template.register_tag(:nomnoml, Jekyll::Diagrams::NomnomlBlock)
@@ -1,15 +1,19 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Jekyll
2
4
  module Diagrams
3
5
  class PlantUMLBlock < Block
6
+ XML_REGEX = /^<\?xml([^>]|\n)*>\n?/.freeze
7
+
4
8
  def render_svg(code, config)
5
9
  command = build_command(config)
6
10
 
7
11
  svg = render_with_stdin_stdout(command, code)
8
- svg.sub!(/^<\?xml([^>]|\n)*>\n?/, '')
12
+ svg.sub!(XML_REGEX, '')
9
13
  end
10
14
 
11
- def build_command(config)
12
- options = 'java'
15
+ def build_command(_config)
16
+ options = +'java'
13
17
  options << ' -Djava.awt.headless=true'
14
18
  options << ' -jar '
15
19
  options << vendor_path('plantuml.1.2020.1.jar')
@@ -19,4 +23,4 @@ module Jekyll
19
23
  end
20
24
  end
21
25
 
22
- Liquid::Template.register_tag(:plantuml, Jekyll::Diagrams::PlantUMLBlock)
26
+ Liquid::Template.register_tag(:plantuml, Jekyll::Diagrams::PlantUMLBlock)
@@ -1,23 +1,26 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Jekyll
2
4
  module Diagrams
3
5
  class SMCatBlock < Block
4
- CONFIGURATIONS = %w( direction engine input-type ).freeze
6
+ XML_REGEX = /^<\?xml(([^>]|\n)*>\n?){2}/.freeze
7
+ CONFIGURATIONS = %w[direction engine input-type].freeze
5
8
 
6
9
  def render_svg(code, config)
7
10
  command = build_command(config)
8
11
 
9
- svg = render_with_tempfile(command, code, stdout: true) do |command, input|
10
- "#{command} #{input} -"
12
+ svg = render_with_tempfile(command, code, stdout: true) do |input|
13
+ "#{input} --output-to -"
11
14
  end
12
15
 
13
- svg.sub!(/^<\?xml(([^>]|\n)*>\n?){2}/, '')
16
+ svg.sub!(XML_REGEX, '')
14
17
  end
15
18
 
16
19
  def build_command(config)
17
- command = 'smcat'
20
+ command = +'smcat'
18
21
 
19
22
  CONFIGURATIONS.each do |conf|
20
- command << " --#{conf} #{config[conf]}" if config.has_key?(conf)
23
+ command << " --#{conf} #{config[conf]}" if config.key?(conf)
21
24
  end
22
25
 
23
26
  command
@@ -26,4 +29,4 @@ module Jekyll
26
29
  end
27
30
  end
28
31
 
29
- Liquid::Template.register_tag(:smcat, Jekyll::Diagrams::SMCatBlock)
32
+ Liquid::Template.register_tag(:smcat, Jekyll::Diagrams::SMCatBlock)
@@ -1,7 +1,9 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Jekyll
2
4
  module Diagrams
3
5
  class SvgbobBlock < Block
4
- CONFIGURATIONS = %w( font-family font-size scale stroke-width ).freeze
6
+ CONFIGURATIONS = %w[font-family font-size scale stroke-width].freeze
5
7
 
6
8
  def render_svg(code, config)
7
9
  command = build_command(config)
@@ -10,10 +12,10 @@ module Jekyll
10
12
  end
11
13
 
12
14
  def build_command(config)
13
- command = 'svgbob'
15
+ command = +'svgbob'
14
16
 
15
17
  CONFIGURATIONS.each do |conf|
16
- command << " --#{conf} #{config[conf]}" if config.has_key?(conf)
18
+ command << " --#{conf} #{config[conf]}" if config.key?(conf)
17
19
  end
18
20
 
19
21
  command
@@ -22,4 +24,4 @@ module Jekyll
22
24
  end
23
25
  end
24
26
 
25
- Liquid::Template.register_tag(:svgbob, Jekyll::Diagrams::SvgbobBlock)
27
+ Liquid::Template.register_tag(:svgbob, Jekyll::Diagrams::SvgbobBlock)
@@ -1,21 +1,23 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Jekyll
2
4
  module Diagrams
3
5
  class SyntraxBlock < Block
4
- CONFIGURATIONS = %w( scale style ).freeze
6
+ CONFIGURATIONS = %w[scale style].freeze
5
7
 
6
8
  def render_svg(code, config)
7
9
  command = build_command(config)
8
10
 
9
- render_with_tempfile(command, code) do |command, input, output|
10
- "#{command} --input #{input} --output #{output}"
11
+ render_with_tempfile(command, code) do |input, output|
12
+ "--input #{input} --output #{output}"
11
13
  end
12
14
  end
13
15
 
14
16
  def build_command(config)
15
- command = 'syntrax'
17
+ command = +'syntrax'
16
18
 
17
19
  CONFIGURATIONS.each do |conf|
18
- command << " --#{conf} #{config[conf]}" if config.has_key?(conf)
20
+ command << " --#{conf} #{config[conf]}" if config.key?(conf)
19
21
  end
20
22
 
21
23
  command
@@ -24,4 +26,4 @@ module Jekyll
24
26
  end
25
27
  end
26
28
 
27
- Liquid::Template.register_tag(:syntrax, Jekyll::Diagrams::SyntraxBlock)
29
+ Liquid::Template.register_tag(:syntrax, Jekyll::Diagrams::SyntraxBlock)
@@ -1,28 +1,95 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'open3'
4
+ require 'tempfile'
5
+
1
6
  module Jekyll
2
7
  module Diagrams
3
8
  module Util
4
- extend self
9
+ module_function
5
10
 
6
11
  def diagrams_config(context)
7
- site_config = context.registers[:site].config
8
- new_config = site_config.fetch('jekyll-diagrams', {})
9
- old_config = site_config.fetch('diagrams', {})
10
-
11
- return new_config if old_config.empty?
12
+ site_config = context.registers[:site].config.fetch(config_name, {})
13
+ page_config = context.registers[:page].fetch(config_name, {})
12
14
 
13
- Jekyll.warn "Configuration `diagram` in _config.yml will be depreated"
14
- Jekyll.warn " Rename it to `jekyll-diagram` instead."
15
-
16
- old_config.merge(new_config)
15
+ site_config.merge(page_config)
17
16
  end
18
17
 
19
18
  def config_for(context, name)
20
19
  diagrams_config(context).fetch(name, {})
21
20
  end
22
21
 
22
+ def config_name
23
+ 'jekyll-diagrams'
24
+ end
25
+
23
26
  def vendor_path(file = '')
24
27
  File.join(File.expand_path('../../vendor', __dir__), file)
25
28
  end
29
+
30
+ def normalized_attrs(attrs, prefix:, sep: '=')
31
+ attrs = case attrs
32
+ when String
33
+ attrs
34
+ when Array
35
+ attrs.join(prefix)
36
+ when Hash
37
+ attrs.map { |k, v| "#{k}#{sep}#{v}" }.join(prefix)
38
+ end
39
+
40
+ "#{prefix}#{attrs}"
41
+ end
42
+
43
+ def render_with_stdin_stdout(command, content)
44
+ render_with_stdin(command, content, stdout: true)
45
+ end
46
+
47
+ def render_with_stdin(command, content, stdout: false, &block)
48
+ if stdout == true
49
+ render_with_command(command, :stdout, stdin_data: content)
50
+ else
51
+ output = Tempfile.open(['jekyll_diagrams_output', '.svg'])
52
+ output.close
53
+ extra = block.call(ouput.path)
54
+ command = "#{command} #{extra}"
55
+
56
+ render_with_command(command, output.path, stdin_data: content)
57
+ end
58
+ end
59
+
60
+ def render_with_tempfile(command, content, stdout: false, &block)
61
+ Tempfile.open('jekyll_diagrams_input') do |input|
62
+ File.write(input.path, content)
63
+
64
+ if stdout == true
65
+ extra = block.call(input.path)
66
+ command = "#{command} #{extra}"
67
+
68
+ render_with_command(command, :stdout)
69
+ else
70
+ output = Tempfile.open(['jekyll_diagrams_output', '.svg'])
71
+ output.close
72
+ extra = block.call(input.path, output.path)
73
+ command = "#{command} #{extra}"
74
+
75
+ render_with_command(command, output.path)
76
+ end
77
+ end
78
+ end
79
+
80
+ def render_with_command(command, output, options = {})
81
+ stdout, stderr, status = Open3.capture3(command, options)
82
+
83
+ unless status.success?
84
+ raise <<~MESSAGE
85
+ Failed to render command: #{command}
86
+ #{stdout}
87
+ #{stderr}
88
+ MESSAGE
89
+ end
90
+
91
+ output == :stdout ? stdout : File.read(output)
92
+ end
26
93
  end
27
94
  end
28
- end
95
+ end
@@ -1,20 +1,23 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Jekyll
2
4
  module Diagrams
3
5
  class VegaBlock < Block
4
- CONFIGURATIONS = %w( scale )
6
+ CONFIGURATIONS = %w[scale].freeze
5
7
 
6
8
  def render_svg(code, config)
7
9
  code = render_with_stdin_stdout('vl2vg', code) if block_name == 'vegalite'
10
+
8
11
  command = build_command(config)
9
12
 
10
13
  render_with_stdin_stdout(command, code)
11
14
  end
12
15
 
13
16
  def build_command(config)
14
- command = 'vg2svg'
17
+ command = +'vg2svg'
15
18
 
16
19
  CONFIGURATIONS.each do |conf|
17
- command << " --#{conf} #{config[conf]}" if config.has_key?(conf)
20
+ command << " --#{conf} #{config[conf]}" if config.key?(conf)
18
21
  end
19
22
 
20
23
  command
@@ -23,6 +26,6 @@ module Jekyll
23
26
  end
24
27
  end
25
28
 
26
- %i(vega vegalite).each do |tag|
29
+ %i[vega vegalite].each do |tag|
27
30
  Liquid::Template.register_tag(tag, Jekyll::Diagrams::VegaBlock)
28
- end
31
+ end
@@ -1,5 +1,7 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Jekyll
2
4
  module Diagrams
3
- VERSION = '0.9.1'
5
+ VERSION = '0.9.2'
4
6
  end
5
- end
7
+ end
@@ -1,19 +1,21 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Jekyll
2
4
  module Diagrams
3
5
  class WavedromBlock < Block
4
6
  def render_svg(code, config)
5
7
  command = build_command(config)
6
8
 
7
- render_with_tempfile(command, code) do |command, input, output|
8
- "#{command} --input #{input} --svg #{output}"
9
+ render_with_tempfile(command, code) do |input, output|
10
+ "--input #{input} --svg #{output}"
9
11
  end
10
12
  end
11
13
 
12
- def build_command(config)
14
+ def build_command(_config)
13
15
  'wavedrom-cli'
14
16
  end
15
17
  end
16
18
  end
17
19
  end
18
20
 
19
- Liquid::Template.register_tag(:wavedrom, Jekyll::Diagrams::WavedromBlock)
21
+ Liquid::Template.register_tag(:wavedrom, Jekyll::Diagrams::WavedromBlock)
@@ -0,0 +1,22 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'test_helper'
4
+
5
+ class ErdTest < Minitest::Test
6
+ def setup
7
+ @block = Jekyll::Diagrams::ErdBlock.send(
8
+ :new, 'erd', '', Liquid::ParseContext.new
9
+ )
10
+ end
11
+
12
+ def test_build_command_with_default_config
13
+ config = {}
14
+ assert_equal 'erd --fmt=svg', @block.build_command(config)
15
+
16
+ config = { 'edge' => 'compound' }
17
+ assert_match 'edge', @block.build_command(config)
18
+
19
+ config = { 'dot-entity' => true }
20
+ assert_match 'dot-entity', @block.build_command(config)
21
+ end
22
+ end
@@ -1,16 +1,13 @@
1
+ # frozen_string_literal: true
1
2
 
2
3
  lib = File.expand_path('../lib', __dir__)
3
4
  $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
5
 
5
6
  require 'coveralls'
6
-
7
7
  Coveralls.wear!
8
8
 
9
+ require 'minitest/autorun'
10
+
9
11
  require 'fileutils'
10
12
  require 'jekyll'
11
-
12
- require 'jekyll-diagrams/util'
13
- require 'jekyll-diagrams/renderer'
14
- require 'jekyll-diagrams/block'
15
-
16
- require 'minitest/autorun'
13
+ require 'jekyll-diagrams'
@@ -0,0 +1,20 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'test_helper'
4
+
5
+ class UtilTest < Minitest::Test
6
+ include Jekyll::Diagrams::Util
7
+
8
+ def test_normalized_attrs
9
+ attrs = 'color=red'
10
+ assert_equal ' -Ecolor=red', normalized_attrs(attrs, prefix: ' -E')
11
+
12
+ attrs = %w[color=red background=green]
13
+ assert_equal ' -Gcolor=red -Gbackground=green',
14
+ normalized_attrs(attrs, prefix: ' -G')
15
+
16
+ attrs = { 'color' => 'red', 'background' => 'green' }
17
+ assert_equal ' -Ncolor=red -Nbackground=green',
18
+ normalized_attrs(attrs, prefix: ' -N')
19
+ end
20
+ end