kramdown-plantuml 1.1.8 → 1.1.12

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: f873d08b8ab86ebe4ea964c30ad59a43a75c28d042d6627af1694d1ff262cbfd
4
- data.tar.gz: cca59110bbdac25b41dc4c7ef41f9df0f84b17aa4a6da662feab19a73ca61724
3
+ metadata.gz: '09e8c86eb711f1566f32fce47684f204a1daaef68fd53c7d9837dea2af47eedc'
4
+ data.tar.gz: 5393660894a40a510b0d709dc5d6c8bfa738a7584c1f59c5f549526827fd99ef
5
5
  SHA512:
6
- metadata.gz: bbe10bc2e73c25b0a2bd09651e9bd73b2aeb612fa26f0470df0e9f0477c8f8f69f3c69d428b136826264e975068dc073724fcf4ab5e10b93e59585001983ada8
7
- data.tar.gz: b74471596aadcf0bde3bf7878864918fb5bd6297172f69b5f9d30c214f84c1f67dfe2c11b7b6e1dc78bfb85c817c0a2c4a26e5bb9c420c519d490b5567b29fe8
6
+ metadata.gz: f74720e634e10a0688ee87bc8c85277e07865583f85e87e86b87747e1aec03362ab98a7c85d92a14d399ad06eb6525b12647757809128af77f3d35d782e6bbba
7
+ data.tar.gz: 013a677b39a48aa8e401fb1d6dfd5340e66e01f564b2188a17fe5437201df7324ed0f4fd37704c137594867513c614ac33f7c8ce26948988225a11f8cd75da23
@@ -167,6 +167,7 @@ test_gem() {
167
167
 
168
168
  file="${workdir}/_site/index.html"
169
169
 
170
+ file_contains "${file}" "This is a fixture"
170
171
  file_contains "${file}" "class=\"${class}\""
171
172
  file_contains "${file}" "<svg"
172
173
  file_contains "${file}" "<ellipse"
@@ -0,0 +1,105 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'htmlentities'
4
+ require 'json'
5
+ require_relative 'log_wrapper'
6
+
7
+ module Kramdown
8
+ module PlantUml
9
+ # Processes Jekyll pages.
10
+ class JekyllPageProcessor
11
+ PROCESSED_KEY = :kramdown_plantuml_processed
12
+
13
+ def initialize(page)
14
+ raise ArgumentError, 'page cannot be nil' if page.nil?
15
+
16
+ puts page.class
17
+
18
+ @page = page
19
+ end
20
+
21
+ def process(site_destination_directory)
22
+ @page.output = do_process
23
+ @page.data[PROCESSED_KEY] = true
24
+ @page.write(site_destination_directory)
25
+ end
26
+
27
+ def should_process?
28
+ return false unless @page.output_ext == '.html'
29
+
30
+ if !@page.data.nil? && @page.data.key?(PROCESSED_KEY) && @page.data[PROCESSED_KEY]
31
+ logger.debug "Skipping #{@page.path} because it has already been processed."
32
+ return false
33
+ end
34
+
35
+ true
36
+ end
37
+
38
+ class << self
39
+ def needle(plantuml, options)
40
+ hash = { 'plantuml' => plantuml, 'options' => options.to_h }
41
+
42
+ <<~NEEDLE
43
+ <!--#kramdown-plantuml.start#-->
44
+ #{hash.to_json}
45
+ <!--#kramdown-plantuml.end#-->
46
+ NEEDLE
47
+ rescue StandardError => e
48
+ raise e if options.nil? || options.raise_errors?
49
+
50
+ logger.error 'Error while placing needle.'
51
+ logger.error e.to_s
52
+ logger.debug_multiline plantuml
53
+ end
54
+
55
+ def logger
56
+ @logger ||= ::Kramdown::PlantUml::LogWrapper.init
57
+ end
58
+ end
59
+
60
+ private
61
+
62
+ def do_process
63
+ logger.debug "Replacing Jekyll needles in #{@page.path}"
64
+
65
+ html = @page.output
66
+
67
+ return html if html.nil? || html.empty? || !html.is_a?(String)
68
+
69
+ html.gsub(/<!--#kramdown-plantuml\.start#-->(?<json>.*?)<!--#kramdown-plantuml\.end#-->/m) do
70
+ json = $LAST_MATCH_INFO ? $LAST_MATCH_INFO[:json] : nil
71
+ replace_needle(json) unless json.nil?
72
+ end
73
+ end
74
+
75
+ def replace_needle(json)
76
+ logger.debug 'Replacing Jekyll needle.'
77
+
78
+ needle_hash = JSON.parse(json)
79
+ options_hash = needle_hash['options']
80
+ options = ::Kramdown::PlantUml::Options.new({ plantuml: options_hash })
81
+
82
+ begin
83
+ decode_and_convert(needle_hash, options)
84
+ rescue StandardError => e
85
+ raise e if options.raise_errors?
86
+
87
+ logger.error 'Error while replacing Jekyll needle.'
88
+ logger.error e.to_s
89
+ logger.debug_multiline json
90
+ end
91
+ end
92
+
93
+ def decode_and_convert(hash, options)
94
+ encoded_plantuml = hash['plantuml']
95
+ plantuml = HTMLEntities.new.decode encoded_plantuml
96
+ diagram = ::Kramdown::PlantUml::Diagram.new(plantuml, options)
97
+ diagram.convert_to_svg
98
+ end
99
+
100
+ def logger
101
+ @logger ||= ::Kramdown::PlantUml::LogWrapper.init
102
+ end
103
+ end
104
+ end
105
+ end
@@ -1,16 +1,15 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'json'
4
- require 'English'
5
- require 'htmlentities'
6
3
  require_relative 'log_wrapper'
7
- require_relative 'diagram'
4
+ require_relative 'jekyll_page_processor'
8
5
 
9
6
  module Kramdown
10
7
  module PlantUml
11
8
  # Provides an instance of Jekyll if available.
12
9
  module JekyllProvider
13
10
  class << self
11
+ attr_reader :site_destination_dir
12
+
14
13
  def jekyll
15
14
  return @jekyll if defined? @jekyll
16
15
 
@@ -20,16 +19,8 @@ module Kramdown
20
19
  def install
21
20
  return @installed = false if jekyll.nil?
22
21
 
23
- logger.debug 'Jekyll detected, hooking into :site:post_render'
24
-
25
- Jekyll::Hooks.register :site, :post_render do |site|
26
- logger.debug ':site:post_render triggered.'
27
-
28
- site.pages.each do |page|
29
- page.output = replace_needles(page.output)
30
- end
31
- end
32
-
22
+ find_site_destination_dir
23
+ register_hook
33
24
  @installed = true
34
25
  end
35
26
 
@@ -38,54 +29,39 @@ module Kramdown
38
29
  end
39
30
 
40
31
  def needle(plantuml, options)
41
- hash = { 'plantuml' => plantuml, 'options' => options.to_h }
42
-
43
- <<~NEEDLE
44
- <!--#kramdown-plantuml.start#-->
45
- #{hash.to_json}
46
- <!--#kramdown-plantuml.end#-->
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
32
+ JekyllPageProcessor.needle(plantuml, options)
55
33
  end
56
34
 
57
35
  private
58
36
 
59
- def replace_needles(html)
60
- return html if html.nil? || html.empty? || !html.is_a?(String)
37
+ def find_site_destination_dir
38
+ if jekyll.sites.nil? || jekyll.sites.empty?
39
+ logger.debug 'Jekyll detected, hooking into :site:post_write.'
40
+ return nil
41
+ end
61
42
 
62
- html.gsub(/<!--#kramdown-plantuml\.start#-->(?<json>.*?)<!--#kramdown-plantuml\.end#-->/m) do
63
- json = $LAST_MATCH_INFO[:json]
64
- return replace_needle(json)
43
+ @site_destination_dir = jekyll.sites.first.dest
44
+ logger.debug "Jekyll detected, hooking into :site:post_write of '#{@site_destination_dir}'."
45
+ @site_destination_dir
46
+ end
47
+
48
+ def register_hook
49
+ Jekyll::Hooks.register :site, :post_write do |site|
50
+ site_post_write(site)
65
51
  end
66
52
  end
67
53
 
68
- def replace_needle(json)
69
- hash = JSON.parse(json)
70
- options_hash = hash['options']
71
- options = ::Kramdown::PlantUml::Options.new({ plantuml: options_hash })
54
+ def site_post_write(site)
55
+ logger.debug 'Jekyll:site:post_write triggered.'
56
+ @site_destination_dir ||= site.dest
72
57
 
73
- begin
74
- decode_and_convert(hash, options)
75
- rescue StandardError => e
76
- raise e if options.raise_errors?
58
+ site.pages.each do |page|
59
+ processor = JekyllPageProcessor.new(page)
77
60
 
78
- logger.error 'Error while replacing needle.'
79
- logger.error e.to_s
80
- logger.debug_multiline json
81
- end
82
- end
61
+ next unless processor.should_process?
83
62
 
84
- def decode_and_convert(hash, options)
85
- encoded_plantuml = hash['plantuml']
86
- plantuml = HTMLEntities.new.decode encoded_plantuml
87
- diagram = ::Kramdown::PlantUml::Diagram.new(plantuml, options)
88
- diagram.convert_to_svg
63
+ processor.process(site.dest)
64
+ end
89
65
  end
90
66
 
91
67
  def load_jekyll
@@ -10,7 +10,7 @@ module Kramdown
10
10
 
11
11
  def initialize(options_hash = {})
12
12
  @logger = LogWrapper.init
13
- @options = massage(options_hash)
13
+ @options = massage(options_hash) || {}
14
14
  @raise_errors = extract_raise_errors(@options)
15
15
  extract_theme_options(@options)
16
16
  end
@@ -44,9 +44,9 @@ module Kramdown
44
44
  end
45
45
 
46
46
  def extract_theme_options(options)
47
- return if options.empty? || !options.key?(:theme)
47
+ return if options.nil? || options.empty? || !options.key?(:theme)
48
48
 
49
- theme = options[:theme] || {}
49
+ theme = options[:theme]
50
50
 
51
51
  unless theme.is_a?(Hash)
52
52
  @logger.warn ":theme is not a Hash: #{theme}"
@@ -58,12 +58,10 @@ module Kramdown
58
58
  end
59
59
 
60
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
61
+ return true if options.nil? || options.empty? || !options.key?(:raise_errors)
65
62
 
66
- true
63
+ raise_errors = options[:raise_errors]
64
+ boolean(raise_errors, true)
67
65
  end
68
66
 
69
67
  def massage(options_hash)
@@ -2,6 +2,7 @@
2
2
 
3
3
  require_relative 'options'
4
4
  require_relative 'log_wrapper'
5
+ require_relative 'jekyll_provider'
5
6
 
6
7
  module Kramdown
7
8
  module PlantUml
@@ -13,9 +14,10 @@ module Kramdown
13
14
  raise ArgumentError, 'options cannot be nil' if options.nil?
14
15
  raise ArgumentError, "options must be a '#{Options}'." unless options.is_a?(Options)
15
16
 
17
+ @raise_errors = options.raise_errors?
16
18
  @logger = LogWrapper.init
17
19
  @name = options.theme_name
18
- @directory = options.theme_directory
20
+ @directory = resolve options.theme_directory
19
21
  end
20
22
 
21
23
  def apply(plantuml)
@@ -34,6 +36,28 @@ module Kramdown
34
36
 
35
37
  private
36
38
 
39
+ def resolve(directory)
40
+ jekyll = JekyllProvider
41
+
42
+ return directory if directory.nil? || directory.empty? || !jekyll.installed?
43
+
44
+ directory = File.absolute_path(directory, jekyll.site_destination_dir)
45
+
46
+ log_or_raise "The theme directory '#{directory}' cannot be found" unless Dir.exist?(directory)
47
+
48
+ theme_path = File.join(directory, "puml-theme-#{@name}.puml")
49
+
50
+ log_or_raise "The theme '#{theme_path}' cannot be found" unless File.exist?(theme_path)
51
+
52
+ directory
53
+ end
54
+
55
+ def log_or_raise(message)
56
+ raise IOError, message if @raise_errors
57
+
58
+ logger.warn message
59
+ end
60
+
37
61
  def theme(plantuml)
38
62
  startuml = '@startuml'
39
63
  startuml_index = plantuml.index(startuml) + startuml.length
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Kramdown
4
4
  module PlantUml
5
- VERSION = '1.1.8'
5
+ VERSION = '1.1.12'
6
6
  end
7
7
  end
data/lib/kramdown_html.rb CHANGED
@@ -43,6 +43,7 @@ module Kramdown
43
43
  rescue StandardError => e
44
44
  raise e if options.raise_errors?
45
45
 
46
+ logger = ::Kramdown::PlantUml::LogWrapper.init
46
47
  logger.error "Error while replacing needle: #{e.inspect}"
47
48
  end
48
49
  end
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.8
4
+ version: 1.1.12
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-11-01 00:00:00.000000000 Z
11
+ date: 2021-11-05 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: htmlentities
@@ -187,6 +187,7 @@ files:
187
187
  - lib/kramdown-plantuml/console_logger.rb
188
188
  - lib/kramdown-plantuml/diagram.rb
189
189
  - lib/kramdown-plantuml/executor.rb
190
+ - lib/kramdown-plantuml/jekyll_page_processor.rb
190
191
  - lib/kramdown-plantuml/jekyll_provider.rb
191
192
  - lib/kramdown-plantuml/log_wrapper.rb
192
193
  - lib/kramdown-plantuml/options.rb