liquid-diagrams 0.1.0 → 0.2.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: bf13af189666b210d371da56b940db2a64d320f154867ef334d196ed4d4213dd
4
- data.tar.gz: 5e9c1f89224785821de3de8c7d0365b00dbec7a864e08b0de1aa9dea7ac03e8c
3
+ metadata.gz: df588bcd7fd4a7aaa39d7b84616886346cd3ef33e9e731eda9eca167f6e4e546
4
+ data.tar.gz: b14f75ad3612cf7438acd839f91530e82197e09c8ae55e8a0c049c7edf78603a
5
5
  SHA512:
6
- metadata.gz: 5c50fbdc3ab16e9f6fd411d2819ca5492d03e239fc185273d4d2382d32c198c7c53884d0ffcef61eeda154eff0f3984c130b90e542424ebc32fae462b8b6be82
7
- data.tar.gz: 203812b4bb6ecbf2d812dcb8748124f8a13050afed77986d59d22d38b0837f730ffd1ea4c3405d4ae2fdba4aa7bab1d81711456552651291bd02842f82a2d067
6
+ metadata.gz: 06152201c6a73f2198737ddb543a2fa7ad6a04f583569de17273658e5c3a270f33d3cc1bfb55660afcbf05b1ad43c64ca32916607b03229efb90d39a9cf9e96c
7
+ data.tar.gz: ea18e10f7b712012f179759d6baa0bab4c73471539ee4580927e75173cb3e14730414d7930b4d1de3bcbb32b4c41c993c05d049fc6e0f39145401530ee1a5e42
data/README.md CHANGED
@@ -26,7 +26,7 @@ Liquid Diagrams is a liquid plugins for creating diagrams, it is inspired by [as
26
26
  - [Usage](#usage)
27
27
  - [List of diagrams](#list-of-diagrams)
28
28
  - [Register diagrams](#register-diagrams)
29
- - [Now you can use the diagrams in liquid](#now-you-can-use-the-diagrams-in-liquid)
29
+ - [Use diagrams tag](#use-diagrams-tag)
30
30
  - [Configurations](#configurations)
31
31
  - [Contributing](#contributing)
32
32
  - [License](#license)
@@ -90,7 +90,7 @@ LiquidDiagrams.registered_diagrams
90
90
  LiquidDiagrams.register_diagrams(LiquidDiagrams.diagrams)
91
91
  ```
92
92
 
93
- ### Now you can use the diagrams in liquid
93
+ ### Use diagrams tag
94
94
 
95
95
  ```ruby
96
96
  content = <<~CONTENT
@@ -107,9 +107,33 @@ template.render
107
107
  # => "<svg ...>...</svg>"
108
108
  ```
109
109
 
110
- ## Configurations
110
+ ## Dependencies and Configurations
111
111
 
112
- See [Configrurations](CONFIGURATIONS.md)
112
+ Configurations can be set for each diagrams when parse content, e.g.:
113
+
114
+ ```ruby
115
+ content = <<~CONTENT
116
+ {% blockdiag %}
117
+ blockdiag {
118
+ A -> B -> C -> D;
119
+ A -> E -> F -> G;
120
+ }
121
+ {% endblockdiag %}
122
+ CONTENT
123
+
124
+ options = {
125
+ blockdiag: {
126
+ 'scale' => 3
127
+ }
128
+ # options for other diagrams
129
+ }
130
+
131
+ template = Liquid::Template.parse(content, liquid_diagrams: options)
132
+ template.render
133
+ # => "<svg ...>...</svg>"
134
+ ```
135
+
136
+ See [Configurations](CONFIGURATIONS.md)
113
137
 
114
138
  ## Contributing
115
139
 
@@ -1,7 +1,8 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module LiquidDiagrams
4
- # @abstract Subclass and override {#render} or {#render!} to implement
4
+ # @abstract Subclass and override {#render_with_rescue} or
5
+ # {#render_without_rescue} to implement
5
6
  class BasicBlock < ::Liquid::Block
6
7
  # Return the renderer class matching the block
7
8
  #
@@ -14,36 +15,47 @@ module LiquidDiagrams
14
15
  )
15
16
  end
16
17
 
17
- # Render with error rescued
18
- #
19
- # @param context [Liquid::Context]
20
- #
21
- # @return String
18
+ # @note Do not overwite this method, overwrite {#render_with_rescue} or
19
+ # {#render_without_recue} instead
22
20
  def render(context)
23
21
  @content = super.to_s
22
+ @context = context
24
23
 
25
- render!(context)
26
- rescue Errors::BasicError => error
27
- error
24
+ render_with_rescue
28
25
  end
29
26
 
30
- # Render without error rescued
27
+ # Render diagram with error rescued
31
28
  #
32
- # @param context [Liquid::Context]
33
- #
34
- # @return String
29
+ # @return [String]
30
+ def render_with_rescue
31
+ render_without_rescue
32
+ rescue Errors::BasicError => error
33
+ handle_error(error)
34
+ end
35
+
36
+ # Render diagram without error rescued
35
37
  #
36
- # @raise [Errors::BasicError]
38
+ # @return [String]
37
39
  #
38
- # rubocop:disable Lint/UnusedMethodArgument
39
- def render!(context)
40
+ # @raise [NameError] @see {.renderer}
41
+ # @raise [Errors::BasicError] if rendering failed
42
+ def render_without_rescue
40
43
  self.class.renderer.render(@content, config)
41
44
  end
42
- # rubocop:enable Lint/UnusedMethodArgument
43
45
 
46
+ def handle_error(error)
47
+ error
48
+ end
49
+
50
+ # Read block config from parse context
51
+ #
52
+ # @return [Hash]
44
53
  def config
45
- config = (parse_context[:config] || {})
46
- config.fetch(name.split('::').last.chomp('Block').downcase, {})
54
+ opts = options[:liquid_diagrams] || options['liquid_diagrams'] || {}
55
+
56
+ opts.fetch block_name.to_sym do
57
+ opts.fetch(block_name.to_s, {})
58
+ end
47
59
  end
48
60
  end
49
61
  end
@@ -9,8 +9,7 @@ module LiquidDiagrams
9
9
 
10
10
  def initialize(content, options = {})
11
11
  @content = content
12
- @options = options
13
- @config = @options.delete(:config) || {}
12
+ @config = options
14
13
  end
15
14
 
16
15
  def render
@@ -2,12 +2,7 @@
2
2
 
3
3
  module LiquidDiagrams
4
4
  module Renderers
5
- %i[ Blockdiag
6
- Seqdiag
7
- Actdiag
8
- Nwdiag
9
- Rackdiag
10
- Packetdiag ].each do |diagram|
5
+ %i[Blockdiag Seqdiag Actdiag Nwdiag Rackdiag Packetdiag].each do |diagram|
11
6
  renderer = Class.new(BasicRenderer) do
12
7
  const_set :OPTIONS, %w[
13
8
  config
@@ -29,14 +24,11 @@ module LiquidDiagrams
29
24
  define_method :build_command do
30
25
  command = +"#{diagram.downcase} -T svg --nodoctype"
31
26
 
32
- options = self.class.const_get(:OPTIONS)
33
- switches = self.class.const_get(:SWITCHES)
34
-
35
- @config.slice(*options).each do |opt, value|
27
+ @config.slice(*self.class.const_get(:OPTIONS)).each do |opt, value|
36
28
  command << " --#{opt}=#{value}"
37
29
  end
38
30
 
39
- Utils.merge(switches, @config).each do |swc, value|
31
+ Utils.merge(self.class.const_get(:SWITCHES), @config).each do |swc, value|
40
32
  command << " --#{swc}" if value
41
33
  end
42
34
 
@@ -10,7 +10,7 @@ module LiquidDiagrams
10
10
  end
11
11
 
12
12
  def build_command
13
- jar = Utils.vendor_path('Plantuml.1.2020.1.jar')
13
+ jar = Utils.vendor_path('plantuml.1.2020.1.jar')
14
14
 
15
15
  options = +Utils.run_jar(jar)
16
16
  options << ' -tsvg -pipe'
@@ -18,7 +18,7 @@ module LiquidDiagrams
18
18
  end
19
19
 
20
20
  def build_command
21
- command = +'Smcat'
21
+ command = +'smcat'
22
22
 
23
23
  @config.slice(*OPTIONS).each do |opt, value|
24
24
  command << " --#{opt} #{value}"
@@ -2,24 +2,32 @@
2
2
 
3
3
  module LiquidDiagrams
4
4
  module Renderers
5
- class VegaRenderer < BasicRenderer
6
- OPTIONS = %w[
7
- scale
8
- ].freeze
5
+ %i[Vega Vegalite].each do |diagram|
6
+ renderer = Class.new(BasicRenderer) do
7
+ const_set :OPTIONS, %w[
8
+ scale
9
+ ].freeze
9
10
 
10
- def render
11
- Rendering.render_with_stdin_stdout(build_command, @content)
12
- end
13
-
14
- def build_command
15
- command = +'vg2svg'
11
+ define_method :render do
12
+ if diagram.downcase == 'vegalite'
13
+ @content = Rendering.render_with_stdin_stdout('vl2vg', @content)
14
+ end
16
15
 
17
- @config.slice(*OPTIONS).each do |opt, value|
18
- command << " --#{opt} #{value}"
16
+ Rendering.render_with_stdin_stdout(build_command, @content)
19
17
  end
20
18
 
21
- command
19
+ def build_command
20
+ command = +'vg2svg'
21
+
22
+ @config.slice(*self.class.const_get(:OPTIONS)).each do |opt, value|
23
+ command << " --#{opt} #{value}"
24
+ end
25
+
26
+ command
27
+ end
22
28
  end
29
+
30
+ const_set("#{diagram}Renderer", renderer)
23
31
  end
24
32
  end
25
33
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module LiquidDiagrams
4
- VERSION = '0.1.0'
4
+ VERSION = '0.2.0'
5
5
  end
@@ -22,28 +22,35 @@ RSpec.describe LiquidDiagrams::BasicBlock do
22
22
  end
23
23
 
24
24
  describe '#render' do
25
- it 'call #render!' do
26
- allow(block).to receive(:render!).and_return 'success'
25
+ it 'call #render_with_rescue' do
26
+ allow(block).to receive(:render_with_rescue).and_return 'success'
27
27
 
28
28
  expect(block.render(Liquid::Context.new)).to eq 'success'
29
29
  end
30
+ end
30
31
 
31
- context 'when rendering failed' do
32
- let(:error) { LiquidDiagrams::Errors::BasicError.new }
32
+ describe '#render_with_rescue' do
33
+ it 'call #render_without_rescue' do
34
+ allow(block).to receive(:render_without_rescue).and_return 'success'
33
35
 
34
- it 'rescue and return the error' do
35
- allow(block).to receive(:render!).and_raise(error)
36
+ expect(block.render(Liquid::Context.new)).to eq 'success'
37
+ end
36
38
 
37
- expect(block.render(Liquid::Context.new)).to eq error
38
- end
39
+ it 'rescue and handle the error' do
40
+ allow(block).to receive(:render_without_rescue).and_raise(
41
+ LiquidDiagrams::Errors::BasicError.new
42
+ )
43
+ allow(block).to receive(:handle_error).and_return 'ok'
44
+
45
+ expect(block.render(Liquid::Context.new)).to eq 'ok'
39
46
  end
40
47
  end
41
48
 
42
- describe '#render!' do
49
+ describe '#render_without_rescue' do
43
50
  it 'render with renderer' do
44
51
  allow(TestRenderer).to receive(:render).and_return 'success'
45
52
 
46
- expect(block.render!(Liquid::Context.new)).to eq 'success'
53
+ expect(block.render(Liquid::Context.new)).to eq 'success'
47
54
  end
48
55
  end
49
56
  end
@@ -34,7 +34,7 @@ RSpec.describe LiquidDiagrams::Renderers::PlantumlRenderer do
34
34
  it do
35
35
  command = renderer.build_command
36
36
 
37
- expect(command).to match 'Plantuml'
37
+ expect(command).to match 'plantuml'
38
38
  expect(command).to match '-tsvg -pipe'
39
39
  end
40
40
  end
@@ -11,7 +11,7 @@ RSpec.describe LiquidDiagrams::Renderers::SmcatRenderer do
11
11
 
12
12
  describe '#build_command' do
13
13
  context 'when config is empty' do
14
- it { expect(renderer.build_command).to eq 'Smcat' }
14
+ it { expect(renderer.build_command).to eq 'smcat' }
15
15
  end
16
16
 
17
17
  context 'when config is not empty' do
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: liquid-diagrams
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - zhustec