jekyll-kroki-tag 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 488d990027da2d4ae306d839ab723012fa54b771af9d018b85b164ee898813f1
4
+ data.tar.gz: 8492e2cc74b6d5741a85c0863b1523c09969d08e16139cb9fc816f63a2c13a51
5
+ SHA512:
6
+ metadata.gz: 6e96ebe980202292b408f202aac57bcb6579e12a76ff1725c2046db91cf240e33cbb3bec0c9837ef02a5925e331ab5103da754dfdf34f1f7e068a82ed4e85ee6
7
+ data.tar.gz: 1206b2112fe9c275221cc65494ea111b14fc460037cdee1929b8b7c6a80c58cdaa5907df9954c9f967583fa3bab66cb729f74aa30aed4ee5b7898d5b0c1d9b5e
data/.editorconfig ADDED
@@ -0,0 +1,5 @@
1
+ [*]
2
+ indent_size = 2
3
+ indent_style = space
4
+ insert_final_newline = true
5
+ trim_trailing_whitespace = true
data/.rubocop.yml ADDED
@@ -0,0 +1,5 @@
1
+ require:
2
+ - standard
3
+
4
+ inherit_gem:
5
+ standard: config/base.yml
data/.standard.yml ADDED
@@ -0,0 +1,3 @@
1
+ # For available configuration options, see:
2
+ # https://github.com/testdouble/standard
3
+ ruby_version: 2.7
data/CHANGELOG.md ADDED
@@ -0,0 +1,3 @@
1
+ ## [0.1.0] 2023-08-17
2
+
3
+ - Initial release
data/Gemfile ADDED
@@ -0,0 +1,20 @@
1
+ # frozen_string_literal: true
2
+
3
+ source "https://rubygems.org"
4
+
5
+ # Specify your gem's dependencies in jekyll-kroki-tag.gemspec
6
+ gemspec
7
+
8
+ gem "rake", "~> 13.0"
9
+
10
+ gem "minitest", "~> 5.0"
11
+
12
+ gem "standard", "~> 1"
13
+
14
+ gem "solargraph", "~> 0.49.0"
15
+
16
+ gem "jekyll", "~> 4.3"
17
+
18
+ gem "minitest-power_assert", "~> 0.3.1"
19
+
20
+ gem "minitest-reporters", "~> 1.6"
data/LICENSE ADDED
@@ -0,0 +1,9 @@
1
+ Copyright (c) 2023, wtnabe
2
+
3
+ Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
4
+
5
+ 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
6
+
7
+ 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
8
+
9
+ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS “AS IS” AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
data/README.md ADDED
@@ -0,0 +1,74 @@
1
+ # Jekyll::KrokiTag
2
+
3
+ A plugin that add feature \`text to diagram\` to Jekyll using [kroki.io](https://kroki.io/) hosting. To experiment with that code, run `bin/console` for an interactive prompt.
4
+
5
+ ```
6
+ irb(main)> Liquid::Template.register_tag('kroki', Jekyll::KrokiTag::Block)
7
+ irb(main)> t = Liquid::Template.parse('{% kroki type: "plantuml" %}actor User{% endkroki %}'
8
+ irb(main)> puts t.render
9
+ ```
10
+
11
+ result
12
+
13
+ ```
14
+ <figure class="jekyll-kroki" data-kroki-type="plantuml" data-kroki-format="svg">
15
+ <img src="https://kroki.io/plantuml/svg/eJxLTC7JL1IILU4tAgAVLQPZ" alt="">
16
+
17
+ </figure>
18
+ ```
19
+
20
+ ## Installation
21
+
22
+ Install the gem and add to the application's Gemfile by executing:
23
+
24
+ $ bundle add jekyll-kroki-tag
25
+
26
+ If bundler is not being used to manage dependencies, install the gem by executing:
27
+
28
+ $ gem install jekyll-kroki-tag
29
+
30
+ ## Usage
31
+
32
+ Add to config.
33
+
34
+ ```yaml
35
+ plugins:
36
+ - jekyll-kroki-tag
37
+ ```
38
+
39
+ Write your content.
40
+
41
+ ```liquid
42
+ {% kroki type: 'plantuml' %}
43
+ actor User
44
+ usecase "Write great code" as case
45
+ User -> case
46
+ {% endkroki %}
47
+ ```
48
+
49
+ A Converted HTML as below.
50
+
51
+ ```html
52
+ <figure class="jekyll-kroki" data-kroki-type="plantuml" data-kroki-format="svg">
53
+ <img src="https://kroki.io/plantuml/svg/eJzjSkwuyS9SCC1OLeIqLU5NTixOVVAKL8osSVVIL0pNLFFIzk9JVVJILFYASXGB1Cno2kE4AHFnE90%3D" alt="">
54
+ </figure>
55
+ ```
56
+
57
+ Now you don't need to convert every test-diagram image into real image file and store it and write its path in Markdown or HTML flle.
58
+
59
+ ### Supported attribute
60
+
61
+ * type ( required. PlantUML, Graphviz, Mermaid, ... )
62
+ * format ( PNG, SVG, and more. default is svg )
63
+ * alt
64
+ * caption
65
+
66
+ ## Development
67
+
68
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
69
+
70
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
71
+
72
+ ## Contributing
73
+
74
+ Bug reports and pull requests are welcome on GitHub at https://github.com/wtnabe/jekyll-kroki-tag.
data/Rakefile ADDED
@@ -0,0 +1,14 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ require "rake/testtask"
5
+
6
+ Rake::TestTask.new(:spec) do |t|
7
+ t.libs << "spec"
8
+ t.libs << "lib"
9
+ t.test_files = FileList["spec/**/*_spec.rb"]
10
+ end
11
+
12
+ require "standard/rake"
13
+
14
+ task default: %i[spec standard]
@@ -0,0 +1,38 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "lib/jekyll/kroki_tag/version"
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "jekyll-kroki-tag"
7
+ spec.version = Jekyll::KrokiTag::VERSION
8
+ spec.authors = ["wtnabe"]
9
+ spec.email = ["18510+wtnabe@users.noreply.github.com"]
10
+
11
+ spec.summary = "text-to-diagram power to Jekyll with kroki.io"
12
+ spec.homepage = "https://github.com/wtnabe/jekyll-kroki-tag"
13
+ spec.required_ruby_version = ">= 2.7.0"
14
+
15
+ spec.metadata["allowed_push_host"] = "https://rubygems.org"
16
+
17
+ spec.metadata["homepage_uri"] = spec.homepage
18
+ spec.metadata["source_code_uri"] = spec.homepage
19
+ spec.metadata["changelog_uri"] = "#{spec.homepage}/blob/main/CHANGELOG.md"
20
+
21
+ # Specify which files should be added to the gem when it is released.
22
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
23
+ spec.files = Dir.chdir(__dir__) do
24
+ `git ls-files -z`.split("\x0").reject do |f|
25
+ (f == __FILE__) || f.match(%r{\A(?:(?:bin|test|spec|features)/|\.(?:git|travis|circleci)|appveyor)})
26
+ end
27
+ end
28
+ spec.bindir = "exe"
29
+ spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
30
+ spec.require_paths = ["lib"]
31
+
32
+ # Uncomment to register a new dependency of your gem
33
+ spec.add_dependency "liquid", "~> 4"
34
+ spec.add_dependency "parser", "~> 3"
35
+
36
+ # For more information and examples about making a new gem, check out our
37
+ # guide at: https://bundler.io/guides/creating_gem.html
38
+ end
@@ -0,0 +1,5 @@
1
+ module Jekyll
2
+ module KrokiTag
3
+ class Error < StandardError; end
4
+ end
5
+ end
@@ -0,0 +1,126 @@
1
+ require_relative "./error"
2
+ require "parser/current"
3
+
4
+ module Jekyll
5
+ module KrokiTag
6
+ class SyntaxError < Error; end
7
+
8
+ class ArgumentError < Error; end
9
+
10
+ class MissingRequiredArgument < Error; end
11
+
12
+ class OptionParser
13
+ DEFINED_KWORDS = %w[type format alt caption].map(&:to_sym).freeze
14
+
15
+ #
16
+ # @param [string] args
17
+ #
18
+ class << self
19
+ #
20
+ # @param [string] args
21
+ # @return [Hash]
22
+ #
23
+ def parse(args)
24
+ parser = new(args)
25
+ parser.opts
26
+ end
27
+ end
28
+
29
+ #
30
+ # @param [string] args
31
+ #
32
+ def initialize(args)
33
+ @opts = parse!(args)
34
+ end
35
+ attr_reader :tree, :opts
36
+
37
+ #
38
+ # @param [string] args
39
+ # @return [Hash]
40
+ #
41
+ def parse!(args)
42
+ @tree = Parser::CurrentRuby.parse("{ #{args} }")
43
+
44
+ opts = Hash[*node_value(@tree.to_sexp_array).flatten]
45
+
46
+ if valid_options?(opts)
47
+ raise MissingRequiredArgument.new("required arg `type` is missing") unless opts[:type]
48
+ normalize(opts)
49
+ end
50
+ rescue Parser::SyntaxError => e
51
+ raise SyntaxError.new(e)
52
+ end
53
+
54
+ #
55
+ # @param [Hash] opts
56
+ # @return [Hash]
57
+ #
58
+ def normalize(opts)
59
+ opts[:format] ||= "svg"
60
+ [:type, :format].each { |k|
61
+ opts[k].downcase!
62
+ }
63
+
64
+ opts
65
+ end
66
+
67
+ #
68
+ # @param [array] opts
69
+ # @return [true]
70
+ #
71
+ def valid_options?(opts)
72
+ opts.all? { |key, val|
73
+ config = key.to_s.downcase.sub(/:$/, "").to_sym # standard:disable Lint/RedundantStringCoercion
74
+
75
+ if DEFINED_KWORDS.include?(config)
76
+ true
77
+ else
78
+ raise ArgumentError.new("`#{config}` is not allowed keyword")
79
+ end
80
+ }
81
+ end
82
+
83
+ #
84
+ # make sexp into type-type array of array
85
+ #
86
+ # [[:sym, :str], [:sym, :sym], ...]
87
+ #
88
+ def node_type(ast_array)
89
+ node_attribute(ast_array, key: :type)
90
+ end
91
+
92
+ #
93
+ # make sexp into value-value array of array
94
+ #
95
+ # [[:type, "plantuml"], [:format, "png"], ...]
96
+ #
97
+ def node_value(ast_array)
98
+ node_attribute(ast_array, key: :value)
99
+ end
100
+
101
+ #
102
+ # @param [array] ast_array
103
+ # @param [Symnol] key
104
+ # @return [array]
105
+ #
106
+ def node_attribute(ast_array, key: :type)
107
+ type, *vals = ast_array
108
+
109
+ case type
110
+ when :hash
111
+ vals.map { |e| node_attribute(e, key: key) }
112
+ when :pair
113
+ vals.map { |e| node_attribute(e, key: key) }
114
+ when :sym, :str, :int, :float
115
+ if key == :type
116
+ type
117
+ else
118
+ vals.first
119
+ end
120
+ else
121
+ raise ArgumentError.new("#{vals} is not in type Symbol, String, Integer and Float. Did you write invalid syntax ?")
122
+ end
123
+ end
124
+ end
125
+ end
126
+ end
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Jekyll
4
+ module KrokiTag
5
+ VERSION = "0.1.0"
6
+ end
7
+ end
@@ -0,0 +1,111 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "liquid"
4
+ require "base64"
5
+ require "zlib"
6
+ require "parser/current"
7
+ require_relative "kroki_tag/option_parser"
8
+ require_relative "kroki_tag/error"
9
+ require_relative "kroki_tag/version"
10
+
11
+ module Jekyll
12
+ module KrokiTag
13
+ module Util
14
+ #
15
+ # @param [string] args
16
+ # @return [Hash]
17
+ #
18
+ def parse_args(args)
19
+ OptionParser.parse(args)
20
+ end
21
+
22
+ #
23
+ # @param [string] diagram
24
+ # @return [string]
25
+ #
26
+ def encode_diagram(diagram)
27
+ Base64.urlsafe_encode64(Zlib.deflate(diagram))
28
+ end
29
+
30
+ #
31
+ # @param [string] str
32
+ # @return [string]
33
+ #
34
+ def esc(str)
35
+ CGI.escapeHTML(str.to_s)
36
+ end
37
+
38
+ #
39
+ # @param [Symbol|String] type - diagram type
40
+ # @param [Symbol|String] format - imgage ( or document ) format
41
+ # @param [String] content - diagram content
42
+ # @return [URI]
43
+ #
44
+ def uri(type:, format:, content:)
45
+ URI.join("https://kroki.io/", [type, format, encode_diagram(content)].map { |e| URI.encode_www_form_component(e) }.join("/"))
46
+ end
47
+
48
+ #
49
+ # @param [string] body
50
+ # @param [Hash] opts
51
+ # @return [String]
52
+ #
53
+ def render_body(body, opts:)
54
+ u = uri(type: opts[:type], format: opts[:format], content: body).to_s
55
+ img = "<img src=\"#{u}\" alt=\"#{esc(opts[:alt])}\">"
56
+
57
+ caption = "<figcaption>#{esc(opts[:caption])}</figcaption>" if opts[:caption]
58
+
59
+ <<-EOD.chomp
60
+ <figure class="jekyll-kroki" data-kroki-type="#{esc(opts[:type])}" data-kroki-format="#{esc(opts[:format])}">
61
+ #{img}
62
+ #{caption}
63
+ </figure>
64
+ EOD
65
+ end
66
+ end
67
+
68
+ class Block < ::Liquid::Block
69
+ include Liquid::StandardFilters
70
+ include Util
71
+
72
+ class << self
73
+ #
74
+ # if you want to customize, read from `site.config`
75
+ #
76
+ # @param [Jekyll:Site] site
77
+ #
78
+ def register(site)
79
+ Liquid::Template.register_tag("kroki", self)
80
+ end
81
+ end
82
+
83
+ #
84
+ # @param [string] tag_name
85
+ # @param [string] text
86
+ # @param [Object] tokens
87
+ #
88
+ def initialize(tag_name, text, tokens)
89
+ super
90
+
91
+ @opts = parse_args(text)
92
+ end
93
+
94
+ #
95
+ # @param [Liquid::Context] context
96
+ # @return [string]
97
+ #
98
+ def render(context)
99
+ inner_text = super
100
+
101
+ render_body(inner_text, opts: @opts)
102
+ end
103
+ end
104
+ end
105
+ end
106
+
107
+ if defined? Jekyll::Hooks
108
+ Jekyll::Hooks.register(:site, :post_read) do |site|
109
+ Jekyll::KrokiTag::Block.register(site)
110
+ end
111
+ end
@@ -0,0 +1 @@
1
+ require_relative "./jekyll-kroki_tag"
@@ -0,0 +1 @@
1
+ require_relative "./jekyll/kroki_tag"
@@ -0,0 +1,8 @@
1
+ module Jekyll
2
+ module KrokiTag
3
+ VERSION: String
4
+ # See the writing guide of rbs: https://github.com/ruby/rbs#guides
5
+ class Block
6
+ end
7
+ end
8
+ end
metadata ADDED
@@ -0,0 +1,90 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: jekyll-kroki-tag
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - wtnabe
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2023-08-17 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: liquid
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '4'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '4'
27
+ - !ruby/object:Gem::Dependency
28
+ name: parser
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '3'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '3'
41
+ description:
42
+ email:
43
+ - 18510+wtnabe@users.noreply.github.com
44
+ executables: []
45
+ extensions: []
46
+ extra_rdoc_files: []
47
+ files:
48
+ - ".editorconfig"
49
+ - ".rubocop.yml"
50
+ - ".standard.yml"
51
+ - CHANGELOG.md
52
+ - Gemfile
53
+ - LICENSE
54
+ - README.md
55
+ - Rakefile
56
+ - jekyll-kroki-tag.gemspec
57
+ - lib/jekyll-kroki-tag.rb
58
+ - lib/jekyll-kroki_tag.rb
59
+ - lib/jekyll/kroki_tag.rb
60
+ - lib/jekyll/kroki_tag/error.rb
61
+ - lib/jekyll/kroki_tag/option_parser.rb
62
+ - lib/jekyll/kroki_tag/version.rb
63
+ - sig/jekyll/kroki_tag.rbs
64
+ homepage: https://github.com/wtnabe/jekyll-kroki-tag
65
+ licenses: []
66
+ metadata:
67
+ allowed_push_host: https://rubygems.org
68
+ homepage_uri: https://github.com/wtnabe/jekyll-kroki-tag
69
+ source_code_uri: https://github.com/wtnabe/jekyll-kroki-tag
70
+ changelog_uri: https://github.com/wtnabe/jekyll-kroki-tag/blob/main/CHANGELOG.md
71
+ post_install_message:
72
+ rdoc_options: []
73
+ require_paths:
74
+ - lib
75
+ required_ruby_version: !ruby/object:Gem::Requirement
76
+ requirements:
77
+ - - ">="
78
+ - !ruby/object:Gem::Version
79
+ version: 2.7.0
80
+ required_rubygems_version: !ruby/object:Gem::Requirement
81
+ requirements:
82
+ - - ">="
83
+ - !ruby/object:Gem::Version
84
+ version: '0'
85
+ requirements: []
86
+ rubygems_version: 3.3.7
87
+ signing_key:
88
+ specification_version: 4
89
+ summary: text-to-diagram power to Jekyll with kroki.io
90
+ test_files: []