kramdown-plantuml 1.1.5 → 1.1.6

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 3968b5c182c334e7dbe020e7cd02309f6349fedeab4c6011b63178aa570b5c7f
4
- data.tar.gz: 804e7e4c062c10f1f1c7578c338a8b19ec70a23031081a129f90dc44fa087926
3
+ metadata.gz: 2ad198f4e3e4e733d4e12d7e4281131c5ececd42896a0d14bd5815b6a06c5b7a
4
+ data.tar.gz: d2e558280a863d76b9554845ea31c52d37db234648cd1e2f9dae4fab22f8220f
5
5
  SHA512:
6
- metadata.gz: ad3f371c0c4608c8d613ba9ec73414f6eea92c8eccf9a087fe7ccc487403b4742afc68c63574937aaf8ea30c9f90aaf56b7e5636764647b442137e7a683cbecd
7
- data.tar.gz: c437216460a278794c2caeb20c151c5c2a98cce5d5a72e2a4d1b1fb077cc970ceb50fbb0fb3d832b92a7991d35b36776e019a295cbe9884ff928fee073dd9938
6
+ metadata.gz: a7258a0fcf799cbfd27088342d6030a25115118bda6b2c3d188984200d91821df537ab3f863470e497e9331165e06a88dd538650a41177972bb79bde02bb0704
7
+ data.tar.gz: 774cf7d37cf2968225a0889118e8cafb3f7234ce0caf26b3a969f088418d2c3ba3ce7438b54dfbab3209c81295756c9c1c5f260f5c96ce75f1e8aacc2ebf0c28
@@ -165,7 +165,6 @@ test_gem() {
165
165
  bundle install
166
166
  bundle exec jekyll build "${jekyll_build_args[@]}"
167
167
 
168
-
169
168
  file="${workdir}/_site/index.html"
170
169
 
171
170
  file_contains "${file}" "class=\"${class}\""
@@ -94,7 +94,7 @@ jobs:
94
94
  - name: RSPec (debug)
95
95
  env:
96
96
  DEBUG: 1
97
- run: bundle exec rspec --tag debug
97
+ run: bundle exec rspec --format documentation --tag debug
98
98
 
99
99
  - name: Upload code coverage (debug)
100
100
  uses: actions/upload-artifact@v2
@@ -117,6 +117,22 @@ jobs:
117
117
  - name: Codecov upload
118
118
  run: bundle exec rake codecov:upload || echo 'Codecov upload failed'
119
119
 
120
+ - name: RSPec (Jekyll)
121
+ run: |
122
+ echo "gem 'jekyll', require: false, group: :test" >> Gemfile
123
+ bundle install
124
+ bundle exec rspec --format documentation --tag jekyll
125
+ git checkout HEAD -- Gemfile
126
+
127
+ - name: Upload code coverage (Jekyll)
128
+ uses: actions/upload-artifact@v2
129
+ with:
130
+ name: rspec-jekyll-coverage
131
+ path: ./coverage
132
+
133
+ - name: Codecov upload (Jekyll)
134
+ run: bundle exec rake codecov:upload || echo 'Codecov upload failed'
135
+
120
136
  - name: Build gem
121
137
  id: gem
122
138
  run: .github/scripts/build-gem.sh --ref ${{ github.ref }} --verbose
data/README.md CHANGED
@@ -130,6 +130,20 @@ kramdown:
130
130
  directory: path/to/themes
131
131
  ```
132
132
 
133
+ ### Errors
134
+
135
+ By default, `kramdown-plantuml` will raise an error and crash if something goes
136
+ wrong during processing. This can be circumvented by setting the `raise_errors`
137
+ configuration key to `false`:
138
+
139
+ ```yaml
140
+ kramdown:
141
+ plantuml:
142
+ raise_errors: false
143
+ ```
144
+
145
+ The default value of `raise_errors` is `true`.
146
+
133
147
  ## Contributing
134
148
 
135
149
  Bug reports and pull requests are welcome on [GitHub][github]. This project is
data/Rakefile CHANGED
@@ -6,7 +6,7 @@ require 'rspec/core/rake_task'
6
6
 
7
7
  RSpec::Core::RakeTask.new(:spec) do |t|
8
8
  t.pattern = Dir.glob('spec/**/*_spec.rb')
9
- t.rspec_opts = '--format documentation --tag ~no_plantuml --tag ~no_java --tag ~debug'
9
+ t.rspec_opts = '--format documentation --tag ~no_plantuml --tag ~no_java --tag ~debug --tag ~jekyll'
10
10
  end
11
11
 
12
12
  namespace :maven do
@@ -1,6 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require_relative 'lib/which'
3
+ require_relative 'lib/kramdown-plantuml/which'
4
4
  require_relative 'lib/kramdown-plantuml/version'
5
5
 
6
6
  Gem::Specification.new do |spec|
@@ -2,8 +2,9 @@
2
2
 
3
3
  require_relative 'version'
4
4
  require_relative 'theme'
5
+ require_relative 'options'
5
6
  require_relative 'plantuml_error'
6
- require_relative 'logger'
7
+ require_relative 'log_wrapper'
7
8
  require_relative 'executor'
8
9
 
9
10
  module Kramdown
@@ -12,10 +13,14 @@ module Kramdown
12
13
  class Diagram
13
14
  attr_reader :theme, :plantuml, :result
14
15
 
15
- def initialize(plantuml, options = {})
16
+ def initialize(plantuml, options)
17
+ raise ArgumentError, 'options cannot be nil' if options.nil?
18
+ raise ArgumentError, "options must be a '#{Options}'." unless options.is_a?(Options)
19
+
16
20
  @plantuml = plantuml
17
- @theme = Theme.new(options || {})
18
- @logger = Logger.init
21
+ @options = options
22
+ @theme = Theme.new(options)
23
+ @logger = LogWrapper.init
19
24
  @executor = Executor.new
20
25
  @logger.warn 'PlantUML diagram is empty' if @plantuml.nil? || @plantuml.empty?
21
26
  end
@@ -25,12 +30,14 @@ module Kramdown
25
30
  return @plantuml if @plantuml.nil? || @plantuml.empty?
26
31
 
27
32
  @plantuml = @theme.apply(@plantuml)
28
- @plantuml = plantuml.strip
29
33
  log(plantuml)
30
34
  @result = @executor.execute(self)
31
35
  @result.validate
32
36
  @svg = wrap(@result.without_xml_prologue)
33
- @svg
37
+ rescue StandardError => e
38
+ raise e if @options.raise_errors?
39
+
40
+ @logger.error e.to_s
34
41
  end
35
42
 
36
43
  private
@@ -1,8 +1,8 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require 'open3'
4
- require_relative '../which'
5
- require_relative 'logger'
4
+ require_relative 'which'
5
+ require_relative 'log_wrapper'
6
6
  require_relative 'plantuml_result'
7
7
 
8
8
  module Kramdown
@@ -10,12 +10,18 @@ module Kramdown
10
10
  # Executes the PlantUML Java application.
11
11
  class Executor
12
12
  def initialize
13
- @logger = Logger.init
13
+ @logger = LogWrapper.init
14
+
15
+ java_location = Which.which('java')
16
+
17
+ raise IOError, 'Java can not be found' if java_location.nil?
18
+
19
+ @logger.debug "Java found: #{java_location}"
14
20
  @plantuml_jar_file = find_plantuml_jar_file
15
21
 
16
- raise IOError, 'Java can not be found' unless Which.which('java')
17
- raise IOError, "No 'plantuml.jar' file could be found" if @plantuml_jar_file.nil?
18
22
  raise IOError, "'#{@plantuml_jar_file}' does not exist" unless File.exist? @plantuml_jar_file
23
+
24
+ @logger.debug "plantuml.jar found: #{@plantuml_jar_file}"
19
25
  end
20
26
 
21
27
  def execute(diagram)
@@ -37,9 +43,13 @@ module Kramdown
37
43
 
38
44
  def find_plantuml_jar_file
39
45
  dir = File.dirname __dir__
40
- jar_glob = File.join dir, '../bin/**/plantuml*.jar'
46
+ bin_dir = File.expand_path File.join dir, '../bin'
47
+ jar_glob = File.join bin_dir, '/**/plantuml*.jar'
41
48
  first_jar = Dir[jar_glob].first
42
- File.expand_path first_jar unless first_jar.nil?
49
+
50
+ raise IOError, "No 'plantuml.jar' file could be found within the '#{bin_dir}' directory." if first_jar.nil?
51
+
52
+ File.expand_path first_jar
43
53
  end
44
54
 
45
55
  def debug_args
@@ -2,7 +2,8 @@
2
2
 
3
3
  require 'json'
4
4
  require 'English'
5
- require_relative 'logger'
5
+ require 'rexml/document'
6
+ require_relative 'log_wrapper'
6
7
  require_relative 'diagram'
7
8
 
8
9
  module Kramdown
@@ -37,14 +38,20 @@ module Kramdown
37
38
  end
38
39
 
39
40
  def needle(plantuml, options)
40
- plantuml_options = !options.nil? && options.key?(:plantuml) ? options[:plantuml] : nil
41
- hash = { 'plantuml' => plantuml, 'options' => plantuml_options }
41
+ hash = { 'plantuml' => plantuml, 'options' => options.to_h }
42
42
 
43
43
  <<~NEEDLE
44
44
  <!--#kramdown-plantuml.start#-->
45
45
  #{hash.to_json}
46
46
  <!--#kramdown-plantuml.end#-->
47
47
  NEEDLE
48
+ rescue StandardError => e
49
+ raise e if options.raise_errors?
50
+
51
+ puts e
52
+ logger.error 'Error while placing needle.'
53
+ logger.error e.to_s
54
+ logger.debug_multiline plantuml
48
55
  end
49
56
 
50
57
  private
@@ -52,14 +59,28 @@ module Kramdown
52
59
  def replace_needles(html)
53
60
  html.gsub(/<!--#kramdown-plantuml\.start#-->(?<json>.*?)<!--#kramdown-plantuml\.end#-->/m) do
54
61
  json = $LAST_MATCH_INFO[:json]
55
- hash = JSON.parse(json)
56
- plantuml = hash['plantuml']
57
- options = hash['options']
58
- diagram = ::Kramdown::PlantUml::Diagram.new(plantuml, options)
59
- return diagram.convert_to_svg
62
+ return replace_needle(json)
63
+ rescue StandardError => e
64
+ raise e if options.raise_errors?
65
+
66
+ logger.error "Error while replacing needle: #{e.inspect}"
60
67
  end
61
68
  end
62
69
 
70
+ def replace_needle(json)
71
+ hash = JSON.parse(json)
72
+ encoded_plantuml = hash['plantuml']
73
+ plantuml = decode_html_entities(encoded_plantuml)
74
+ options = ::Kramdown::PlantUml::Options.new({ plantuml: hash['options'] })
75
+ diagram = ::Kramdown::PlantUml::Diagram.new(plantuml, options)
76
+ diagram.convert_to_svg
77
+ end
78
+
79
+ def decode_html_entities(encoded_plantuml)
80
+ doc = REXML::Document.new "<plantuml>#{encoded_plantuml}</plantuml>"
81
+ doc.root.text
82
+ end
83
+
63
84
  def load_jekyll
64
85
  require 'jekyll'
65
86
  ::Jekyll
@@ -68,7 +89,7 @@ module Kramdown
68
89
  end
69
90
 
70
91
  def logger
71
- @logger ||= ::Kramdown::PlantUml::Logger.init
92
+ @logger ||= ::Kramdown::PlantUml::LogWrapper.init
72
93
  end
73
94
  end
74
95
  end
@@ -6,7 +6,7 @@ require_relative 'jekyll_provider'
6
6
  module Kramdown
7
7
  module PlantUml
8
8
  # Logs stuff
9
- class Logger
9
+ class LogWrapper
10
10
  def initialize(logger)
11
11
  raise ArgumentError, 'logger cannot be nil' if logger.nil?
12
12
  raise ArgumentError, 'logger must respond to #debug' unless logger.respond_to? :debug
@@ -52,8 +52,9 @@ module Kramdown
52
52
 
53
53
  class << self
54
54
  def init
55
- inner = JekyllProvider.jekyll ? JekyllProvider.jekyll.logger : nil || ConsoleLogger.new(level)
56
- Logger.new inner
55
+ inner = JekyllProvider.jekyll ? JekyllProvider.jekyll.logger : nil
56
+ inner ||= ConsoleLogger.new(level)
57
+ new inner
57
58
  end
58
59
 
59
60
  def level
@@ -0,0 +1,91 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'log_wrapper'
4
+
5
+ module Kramdown
6
+ module PlantUml
7
+ # Options for PlantUML processing
8
+ class Options
9
+ attr_reader :theme_name, :theme_directory
10
+
11
+ def initialize(options_hash = {})
12
+ @logger = LogWrapper.init
13
+ @options = massage(options_hash)
14
+ @raise_errors = extract_raise_errors(@options)
15
+ extract_theme_options(@options)
16
+ end
17
+
18
+ def raise_errors?
19
+ @raise_errors
20
+ end
21
+
22
+ def to_h
23
+ @options
24
+ end
25
+
26
+ private
27
+
28
+ def boolean(value, default_value)
29
+ return value if [true, false].include? value
30
+ return default_value if value.nil?
31
+
32
+ s = value.to_s.strip
33
+ return true if %w[true yes 1].select { |v| v.casecmp(s).zero? }.any?
34
+ return false if %w[false no 0].select { |v| v.casecmp(s).zero? }.any?
35
+
36
+ default_value
37
+ end
38
+
39
+ def extract_plantuml_options(options_hash)
40
+ return options_hash[:plantuml] if options_hash.key?(:plantuml)
41
+ return options_hash['plantuml'] if options_hash.key?('plantuml')
42
+
43
+ {}
44
+ end
45
+
46
+ def extract_theme_options(options)
47
+ return if options.empty? || !options.key?(:theme)
48
+
49
+ theme = options[:theme] || {}
50
+
51
+ unless theme.is_a?(Hash)
52
+ @logger.warn ":theme is not a Hash: #{theme}"
53
+ return
54
+ end
55
+
56
+ @theme_name = theme.key?(:name) ? theme[:name] : nil
57
+ @theme_directory = theme.key?(:directory) ? theme[:directory] : nil
58
+ end
59
+
60
+ def extract_raise_errors(options)
61
+ if options.key?(:raise_errors)
62
+ raise_errors = options[:raise_errors]
63
+ return boolean(raise_errors, true)
64
+ end
65
+
66
+ true
67
+ end
68
+
69
+ def massage(options_hash)
70
+ if options_hash.nil? || !options_hash.is_a?(Hash) || options_hash.empty?
71
+ @logger.debug 'No options provided'
72
+ return {}
73
+ end
74
+
75
+ plantuml_options = extract_plantuml_options(options_hash)
76
+ symbolize_keys(plantuml_options)
77
+ end
78
+
79
+ def symbolize_keys(options)
80
+ return options if options.nil? || options.empty?
81
+
82
+ array = options.map do |key, value|
83
+ value = value.is_a?(Hash) ? symbolize_keys(value) : value
84
+ [key.to_sym, value]
85
+ end
86
+
87
+ array.to_h
88
+ end
89
+ end
90
+ end
91
+ end
@@ -1,6 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require_relative 'logger'
3
+ require_relative 'log_wrapper'
4
4
  require_relative 'plantuml_error'
5
5
  require_relative 'diagram'
6
6
 
@@ -20,7 +20,7 @@ module Kramdown
20
20
  @stdout = stdout
21
21
  @stderr = stderr
22
22
  @exitcode = exitcode
23
- @logger = Logger.init
23
+ @logger = LogWrapper.init
24
24
  end
25
25
 
26
26
  def without_xml_prologue
@@ -1,6 +1,7 @@
1
1
  # frozen_string_literal: false
2
2
 
3
- require_relative 'logger'
3
+ require_relative 'options'
4
+ require_relative 'log_wrapper'
4
5
 
5
6
  module Kramdown
6
7
  module PlantUml
@@ -8,9 +9,13 @@ module Kramdown
8
9
  class Theme
9
10
  attr_reader :name, :directory
10
11
 
11
- def initialize(options = {})
12
- @logger = Logger.init
13
- @name, @directory = theme_options(options)
12
+ def initialize(options)
13
+ raise ArgumentError, 'options cannot be nil' if options.nil?
14
+ raise ArgumentError, "options must be a '#{Options}'." unless options.is_a?(Options)
15
+
16
+ @logger = LogWrapper.init
17
+ @name = options.theme_name
18
+ @directory = options.theme_directory
14
19
  end
15
20
 
16
21
  def apply(plantuml)
@@ -21,7 +26,7 @@ module Kramdown
21
26
 
22
27
  if @name.nil? || @name.empty?
23
28
  @logger.debug 'No theme to apply.'
24
- return plantuml
29
+ return plantuml.strip
25
30
  end
26
31
 
27
32
  theme(plantuml)
@@ -29,31 +34,6 @@ module Kramdown
29
34
 
30
35
  private
31
36
 
32
- def theme_options(options)
33
- options = symbolize_keys(options)
34
-
35
- @logger.debug "Options: #{options}"
36
-
37
- return nil if options.nil? || !options.key?(:theme)
38
-
39
- theme = options[:theme] || {}
40
- name = theme.key?(:name) ? theme[:name] : nil
41
- directory = theme.key?(:directory) ? theme[:directory] : nil
42
-
43
- [name, directory]
44
- end
45
-
46
- def symbolize_keys(options)
47
- return options if options.nil?
48
-
49
- array = options.map do |key, value|
50
- value = value.is_a?(Hash) ? symbolize_keys(value) : value
51
- [key.to_sym, value]
52
- end
53
-
54
- array.to_h
55
- end
56
-
57
37
  def theme(plantuml)
58
38
  startuml = '@startuml'
59
39
  startuml_index = plantuml.index(startuml) + startuml.length
@@ -69,7 +49,7 @@ module Kramdown
69
49
  return plantuml.insert match.end(0), theme_string
70
50
  end
71
51
 
72
- plantuml
52
+ plantuml.strip
73
53
  end
74
54
  end
75
55
  end
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Kramdown
4
4
  module PlantUml
5
- VERSION = '1.1.5'
5
+ VERSION = '1.1.6'
6
6
  end
7
7
  end
@@ -1,6 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- # Wraps the `which` Unix utility
3
+ # Mimics the `which` Unix utility
4
4
  class Which
5
5
  def self.which(cmd)
6
6
  exts = ENV['PATHEXT'] ? ENV['PATHEXT'].split(';') : ['']
@@ -10,6 +10,7 @@ class Which
10
10
  return exe if File.executable?(exe) && !File.directory?(exe)
11
11
  end
12
12
  end
13
+
13
14
  nil
14
15
  end
15
16
  end
data/lib/kramdown_html.rb CHANGED
@@ -2,8 +2,9 @@
2
2
 
3
3
  require 'kramdown'
4
4
  require 'kramdown-parser-gfm'
5
- require_relative 'kramdown-plantuml/logger'
5
+ require_relative 'kramdown-plantuml/log_wrapper'
6
6
  require_relative 'kramdown-plantuml/plantuml_error'
7
+ require_relative 'kramdown-plantuml/options'
7
8
  require_relative 'kramdown-plantuml/diagram'
8
9
  require_relative 'kramdown-plantuml/jekyll_provider'
9
10
 
@@ -24,7 +25,8 @@ module Kramdown
24
25
  # be copied to the assets directory before the PlantUML conversion can
25
26
  # be performed. We therefore place a needle in the haystack that we will
26
27
  # convert in the :site:pre_render hook.
27
- return jekyll.needle(element.value, @options) if jekyll.installed?
28
+ options = ::Kramdown::PlantUml::Options.new(@options)
29
+ return jekyll.needle(element.value, options) if jekyll.installed?
28
30
 
29
31
  convert_plantuml(element.value)
30
32
  end
@@ -36,9 +38,8 @@ module Kramdown
36
38
  end
37
39
 
38
40
  def convert_plantuml(plantuml)
39
- plantuml_options = @options.key?(:plantuml) ? @options[:plantuml] : {}
40
-
41
- diagram = ::Kramdown::PlantUml::Diagram.new(plantuml, plantuml_options)
41
+ options = ::Kramdown::PlantUml::Options.new(@options)
42
+ diagram = ::Kramdown::PlantUml::Diagram.new(plantuml, options)
42
43
  diagram.convert_to_svg
43
44
  end
44
45
  end
data/pom.xml CHANGED
@@ -10,7 +10,7 @@
10
10
  <dependency>
11
11
  <groupId>net.sourceforge.plantuml</groupId>
12
12
  <artifactId>plantuml</artifactId>
13
- <version>1.2021.10</version>
13
+ <version>1.2021.12</version>
14
14
  </dependency>
15
15
  </dependencies>
16
16
  </project>
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: kramdown-plantuml
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.5
4
+ version: 1.1.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - Swedbank Pay
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2021-10-01 00:00:00.000000000 Z
11
+ date: 2021-11-01 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: kramdown
@@ -166,7 +166,7 @@ files:
166
166
  - LICENSE
167
167
  - README.md
168
168
  - Rakefile
169
- - bin/net/sourceforge/plantuml/plantuml/1.2021.10/plantuml-1.2021.10.jar
169
+ - bin/net/sourceforge/plantuml/plantuml/1.2021.12/plantuml-1.2021.12.jar
170
170
  - kramdown-plantuml.gemspec
171
171
  - lib/kramdown-plantuml.rb
172
172
  - lib/kramdown-plantuml/bool_env.rb
@@ -174,13 +174,14 @@ files:
174
174
  - lib/kramdown-plantuml/diagram.rb
175
175
  - lib/kramdown-plantuml/executor.rb
176
176
  - lib/kramdown-plantuml/jekyll_provider.rb
177
- - lib/kramdown-plantuml/logger.rb
177
+ - lib/kramdown-plantuml/log_wrapper.rb
178
+ - lib/kramdown-plantuml/options.rb
178
179
  - lib/kramdown-plantuml/plantuml_error.rb
179
180
  - lib/kramdown-plantuml/plantuml_result.rb
180
181
  - lib/kramdown-plantuml/theme.rb
181
182
  - lib/kramdown-plantuml/version.rb
183
+ - lib/kramdown-plantuml/which.rb
182
184
  - lib/kramdown_html.rb
183
- - lib/which.rb
184
185
  - pom.xml
185
186
  homepage: https://github.com/SwedbankPay/kramdown-plantuml
186
187
  licenses: