class Asciidoctor::PlantUml::Processor

PlantUML Processor

Constants

DEFAULT_ENCODING
DEFAULT_FORMAT
ENCODINGS
ENCODINGS_MAGIC_STRINGS_MAP
FORMATS
URI_SCHEMES_REGEXP

Public Class Methods

enabled?() click to toggle source
# File lib/asciidoctor_plantuml/plantuml.rb, line 77
def enabled?
  txt_enabled? || png_enabled? || svg_enabled?
end
gen_url(text, format) click to toggle source

Compression code used to generate PlantUML URLs. Taken directly from the transcoder class in the PlantUML java code.

# File lib/asciidoctor_plantuml/plantuml.rb, line 102
def gen_url(text, format)
  result = ''
  result += encoding_magic_prefix
  compressed_data = Zlib::Deflate.deflate(text)
  compressed_data.chars.each_slice(3) do |bytes|
    # print bytes[0], ' ' , bytes[1] , ' ' , bytes[2]
    b1 = bytes[0].nil? ? 0 : (bytes[0].ord & 0xFF)
    b2 = bytes[1].nil? ? 0 : (bytes[1].ord & 0xFF)
    b3 = bytes[2].nil? ? 0 : (bytes[2].ord & 0xFF)
    result += append3bytes(b1, b2, b3)
  end
  join_paths(server_url, "#{format}/", result).to_s
end
plantuml_content(code, attrs = {}) click to toggle source
# File lib/asciidoctor_plantuml/plantuml.rb, line 90
def plantuml_content(code, attrs = {})
  format = attrs['format'] || DEFAULT_FORMAT

  return plantuml_disabled_content(code, attrs) unless enabled?

  return plantuml_server_unavailable_content(server_url, attrs) unless valid_uri?(server_url)

  plantuml_content_format(code, format, attrs)
end
plantuml_content_format(code, format, attrs = {}) click to toggle source
# File lib/asciidoctor_plantuml/plantuml.rb, line 81
def plantuml_content_format(code, format, attrs = {})
  if %w[png svg txt].include?(format) &&
     method("#{format}_enabled?").call
    method("plantuml_#{format}_content").call(code, format, attrs)
  else
    plantuml_invalid_content(format, attrs)
  end
end
png_enabled?() click to toggle source
# File lib/asciidoctor_plantuml/plantuml.rb, line 69
def png_enabled?
  PlantUml.configuration.png_enable
end
server_url() click to toggle source
# File lib/asciidoctor_plantuml/plantuml.rb, line 61
def server_url
  PlantUml.configuration.url
end
svg_enabled?() click to toggle source
# File lib/asciidoctor_plantuml/plantuml.rb, line 73
def svg_enabled?
  PlantUml.configuration.svg_enable
end
txt_enabled?() click to toggle source
# File lib/asciidoctor_plantuml/plantuml.rb, line 65
def txt_enabled?
  PlantUml.configuration.txt_enable
end
valid_encoding?(encoding) click to toggle source
# File lib/asciidoctor_plantuml/plantuml.rb, line 57
def valid_encoding?(encoding)
  ENCODINGS.include?(encoding)
end
valid_format?(format) click to toggle source
# File lib/asciidoctor_plantuml/plantuml.rb, line 53
def valid_format?(format)
  FORMATS.include?(format)
end

Private Class Methods

_plantuml_error_content(error, attrs = {}) click to toggle source
# File lib/asciidoctor_plantuml/plantuml.rb, line 183
def _plantuml_error_content(error, attrs = {})
  content = '<div class="listingblock">'
  content += '<div class="content">'
  content += '<pre '
  content += "id=\"#{attrs['id']}\" " if attrs['id']
  content += 'class="plantuml plantuml-error">\n'
  content += error
  content += '</pre>'
  content += '</div>'
  content + '</div>'
end
append3bytes(bit1, bit2, bit3) click to toggle source
# File lib/asciidoctor_plantuml/plantuml.rb, line 212
def append3bytes(bit1, bit2, bit3)
  c1 = bit1 >> 2
  c2 = ((bit1 & 0x3) << 4) | (bit2 >> 4)
  c3 = ((bit2 & 0xF) << 2) | (bit3 >> 6)
  c4 = bit3 & 0x3F
  encode6bit(c1 & 0x3F).chr +
    encode6bit(c2 & 0x3F).chr +
    encode6bit(c3 & 0x3F).chr +
    encode6bit(c4 & 0x3F).chr
end
check_server(check_url) click to toggle source

Make a call to the PlantUML server with the simplest diagram possible to check if the server is available or not.

# File lib/asciidoctor_plantuml/plantuml.rb, line 229
def check_server(check_url)
  response = Net::HTTP.get_response(URI(check_url))
  response.is_a?(Net::HTTPSuccess)
rescue OpenURI::HTTPError, Errno::ECONNREFUSED, SocketError
  false
end
encode6bit(bit) click to toggle source
# File lib/asciidoctor_plantuml/plantuml.rb, line 195
def encode6bit(bit)
  return ('0'.ord + bit).chr if bit < 10

  bit -= 10
  return ('A'.ord + bit).chr if bit < 26

  bit -= 26
  return ('a'.ord + bit).chr if bit < 26

  bit -= 26
  return '-' if bit.zero?

  return '_' if bit == 1

  '?'
end
encoding_magic_prefix() click to toggle source
# File lib/asciidoctor_plantuml/plantuml.rb, line 223
def encoding_magic_prefix
  ENCODINGS_MAGIC_STRINGS_MAP[PlantUml.configuration.encoding]
end
expand_path(path, current, last, separator) click to toggle source
# File lib/asciidoctor_plantuml/plantuml.rb, line 248
def expand_path(path, current, last, separator)
  path = path[1..-1] if path.start_with?(separator) && current.zero?

  path = [path, separator] unless path.end_with?(separator) || current == last

  path
end
join_paths(*paths, separator: '/') click to toggle source
# File lib/asciidoctor_plantuml/plantuml.rb, line 240
def join_paths(*paths, separator: '/')
  paths = paths.compact.reject(&:empty?)
  last = paths.length - 1
  paths.each_with_index.map do |path, index|
    expand_path(path, index, last, separator)
  end.join
end
plantuml_ascii_content(code, attrs = {}) click to toggle source
# File lib/asciidoctor_plantuml/plantuml.rb, line 127
def plantuml_ascii_content(code, attrs = {})
  content = '<div class="listingblock">'
  content += '<div class="content">'
  content += '<pre '
  content += "id=\"#{attrs['id']}\" " if attrs['id']
  content += 'class="plantuml">\n'
  content += code
  content += '</pre>'
  content += '</div>'
  content + '</div>'
end
plantuml_disabled_content(code, attrs = {}) click to toggle source
# File lib/asciidoctor_plantuml/plantuml.rb, line 179
def plantuml_disabled_content(code, attrs = {})
  _plantuml_error_content(code, attrs)
end
plantuml_invalid_content(format, attrs = {}) click to toggle source
# File lib/asciidoctor_plantuml/plantuml.rb, line 169
def plantuml_invalid_content(format, attrs = {})
  error = "PlantUML Error: Invalid format \"#{format}\""
  _plantuml_error_content(error, attrs)
end
plantuml_png_content(code, format, attrs = {}) click to toggle source
# File lib/asciidoctor_plantuml/plantuml.rb, line 139
def plantuml_png_content(code, format, attrs = {})
  content = '<div class="imageblock">'
  content += '<div class="content">'
  content += '<img '
  content += "id=\"#{attrs['id']}\" " if attrs['id']
  content += 'class="plantuml" '
  content += "width=\"#{attrs['width']}\" " if attrs['width']
  content += "height=\"#{attrs['height']}\" " if attrs['height']
  content += "alt=\"#{attrs['alt']}\" " if attrs['alt']
  content += "src=\"#{gen_url(code, format)}\" />"
  content += '</div>'
  content + '</div>'
end
plantuml_server_unavailable_content(url, attrs = {}) click to toggle source
# File lib/asciidoctor_plantuml/plantuml.rb, line 174
def plantuml_server_unavailable_content(url, attrs = {})
  error = "Error: cannot connect to PlantUML server at \"#{url}\""
  _plantuml_error_content(error, attrs)
end
plantuml_svg_content(code, format, attrs = {}) click to toggle source
# File lib/asciidoctor_plantuml/plantuml.rb, line 153
def plantuml_svg_content(code, format, attrs = {})
  content = '<div class="imageblock">'
  content += '<div class="content">'
  content += '<object type="image/svg+xml" '
  content += "data=\"#{gen_url(code, format)}\" "
  content += "id=\"#{attrs['id']}\" " if attrs['id']
  content += "width=\"#{attrs['width']}\" " if attrs['width']
  content += "height=\"#{attrs['height']}\" " if attrs['height']
  content += '>'
  attrs['id'] = 'fallback_' + attrs['id'] if attrs['id']
  content += plantuml_png_content(code, format, attrs)
  content += '</object>'
  content += '</div>'
  content + '</div>'
end
plantuml_txt_content(code, format, attrs = {}) click to toggle source
# File lib/asciidoctor_plantuml/plantuml.rb, line 118
def plantuml_txt_content(code, format, attrs = {})
  url = gen_url(code, format)
  URI(url).open do |f|
    plantuml_ascii_content(f.read, attrs)
  end
rescue OpenURI::HTTPError, Errno::ECONNREFUSED, SocketError
  plantuml_png_content(code, format, attrs)
end
valid_uri?(uri) click to toggle source
# File lib/asciidoctor_plantuml/plantuml.rb, line 236
def valid_uri?(uri)
  !(uri =~ /\A#{URI_SCHEMES_REGEXP}\z/).nil?
end