Module: Jekyll::KrokiTag::Util

Included in:
Block
Defined in:
lib/jekyll/kroki_tag.rb

Instance Method Summary collapse

Instance Method Details

#encode_diagram(diagram) ⇒ string



29
30
31
# File 'lib/jekyll/kroki_tag.rb', line 29

def encode_diagram(diagram)
  Base64.urlsafe_encode64(Zlib.deflate(diagram))
end

#esc(str) ⇒ string



37
38
39
# File 'lib/jekyll/kroki_tag.rb', line 37

def esc(str)
  CGI.escapeHTML(str.to_s)
end

#parse_args(args) ⇒ Hash



21
22
23
# File 'lib/jekyll/kroki_tag.rb', line 21

def parse_args(args)
  OptionParser.parse(args)
end

#render_body(body, opts:) ⇒ String



56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/jekyll/kroki_tag.rb', line 56

def render_body(body, opts:)
  u = uri(type: opts[:type], format: opts[:format], content: body).to_s
  img = "<img src=\"#{u}\" alt=\"#{esc(opts[:alt])}\">"

  caption = "<figcaption>#{esc(opts[:caption])}</figcaption>" if opts[:caption]

  "  <figure class=\"jekyll-kroki\" data-kroki-type=\"\#{esc(opts[:type])}\" data-kroki-format=\"\#{esc(opts[:format])}\">\n    \#{img}\n    \#{caption}\n  </figure>\n  EOD\nend\n".chomp

#uri(type:, format:, content:) ⇒ URI



47
48
49
# File 'lib/jekyll/kroki_tag.rb', line 47

def uri(type:, format:, content:)
  URI.join("https://kroki.io/", [type, format, encode_diagram(content)].map { |e| URI.encode_www_form_component(e) }.join("/"))
end