class Asciidoctor::PlantUml::Processor

Constants

DEFAULT_FORMAT
FORMATS

Public Class Methods

enabled?() click to toggle source
# File lib/asciidoctor-plantuml/plantuml.rb, line 61
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 95
def gen_url(text, format)
  result = ""
  compressedData = Zlib::Deflate.deflate(text)
  compressedData.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 65
def plantuml_content(code, attrs = {})

  format = attrs["format"] || DEFAULT_FORMAT

  if !enabled?
    return plantuml_disabled_content(code, attrs)
  end

  if !valid_uri?(server_url)
    return plantuml_server_unavailable_content(server_url, attrs)
  end

  case format
  when "png"
    plantuml_img_content(code, format, attrs)
  when "txt"
    if txt_enabled?
      plantuml_txt_content(code, format, attrs)
    else
      plantuml_invalid_content(format, attrs)
    end
  when "svg"
    plantuml_img_content(code, format, attrs)
  else
    plantuml_invalid_content(format, attrs)
  end
end
png_enabled?() click to toggle source
# File lib/asciidoctor-plantuml/plantuml.rb, line 53
def png_enabled?
  PlantUml::configuration.png_enable
end
server_url() click to toggle source
# File lib/asciidoctor-plantuml/plantuml.rb, line 45
def server_url
  PlantUml::configuration.url
end
svg_enabled?() click to toggle source
# File lib/asciidoctor-plantuml/plantuml.rb, line 57
def svg_enabled?
  PlantUml::configuration.svg_enable
end
txt_enabled?() click to toggle source
# File lib/asciidoctor-plantuml/plantuml.rb, line 49
def txt_enabled?
  PlantUml::configuration.txt_enable
end
valid_format?(format) click to toggle source
# File lib/asciidoctor-plantuml/plantuml.rb, line 41
def valid_format?(format)
  FORMATS.include?(format)
end

Private Class Methods

append3bytes(b1, b2, b3) click to toggle source
# File lib/asciidoctor-plantuml/plantuml.rb, line 205
def append3bytes(b1, b2, b3)
  c1 = b1 >> 2
  c2 = ((b1 & 0x3) << 4) | (b2 >> 4)
  c3 = ((b2 & 0xF) << 2) | (b3 >> 6)
  c4 = b3 & 0x3F
  return 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 218
def check_server(check_url)
  response = Net::HTTP.get_response(URI(check_url))
  return response.is_a?(Net::HTTPSuccess)
rescue
  return false
end
encode6bit(b) click to toggle source
# File lib/asciidoctor-plantuml/plantuml.rb, line 183
def encode6bit(b)
  if b < 10
    return ('0'.ord + b).chr
  end
  b = b - 10
  if b < 26
    return ('A'.ord + b).chr
  end
  b = b - 26
  if b < 26
    return ('a'.ord + b).chr
  end
  b = b - 26
  if b == 0
    return '-'
  end
  if b == 1
    return '_'
  end
  return '?'
end
expand_path(path, current, last, separator) click to toggle source
# File lib/asciidoctor-plantuml/plantuml.rb, line 237
def expand_path(path, current, last, separator)
  if path.start_with?(separator) && current != 0
    path = path[1..-1]
  end

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

  path
end
join_paths(*paths, separator: '/') click to toggle source
# File lib/asciidoctor-plantuml/plantuml.rb, line 229
def join_paths(*paths, separator: '/')
  paths = paths.compact.reject(&:empty?)
  last = paths.length - 1
  paths.each_with_index.map { |path, index|
    expand_path(path, index, last, separator)
  }.join
end
plantuml_ascii_content(code, format, attrs = {}) click to toggle source
# File lib/asciidoctor-plantuml/plantuml.rb, line 121
def plantuml_ascii_content(code, format, 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 171
def plantuml_disabled_content(code, 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 += code
  content +="</pre>"
  content += "</div>"
  content += "</div>"
end
plantuml_img_content(code, format, attrs = {}) click to toggle source
# File lib/asciidoctor-plantuml/plantuml.rb, line 133
def plantuml_img_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_invalid_content(format, attrs = {}) click to toggle source
# File lib/asciidoctor-plantuml/plantuml.rb, line 147
def plantuml_invalid_content(format, attrs = {})
  content = "<div class=\"listingblock\">"
  content += "<div class=\"content\">"
  content += "<pre "
  content +="id=\"#{attrs['id']}\" " if attrs['id']
  content +="class=\"plantuml plantuml-error\"> "
  content += "PlantUML Error: Invalid format \"#{format}\""
  content +="</pre>"
  content += "</div>"
  content += "</div>"
end
plantuml_server_unavailable_content(url, attrs = {}) click to toggle source
# File lib/asciidoctor-plantuml/plantuml.rb, line 159
def plantuml_server_unavailable_content(url, attrs = {})
  content = "<div class=\"listingblock\">"
  content += "<div class=\"content\">"
  content += "<pre "
  content +="id=\"#{attrs['id']}\" " if attrs['id']
  content +="class=\"plantuml plantuml-error\"> "
  content += "PlantUML Error: cannot connect to PlantUML server at \"#{url}\""
  content +="</pre>"
  content += "</div>"
  content += "</div>"
end
plantuml_txt_content(code, format, attrs = {}) click to toggle source
# File lib/asciidoctor-plantuml/plantuml.rb, line 110
def plantuml_txt_content(code, format, attrs = {})
  begin
    url = gen_url(code, format)
    open(url) do |f|
      plantuml_ascii_content(f.read, format, attrs)
    end
  rescue
    plantuml_img_content(code, format, attrs)
  end
end
valid_uri?(uri) click to toggle source
# File lib/asciidoctor-plantuml/plantuml.rb, line 225
def valid_uri?(uri)
  !(uri =~ /\A#{URI::regexp(['http', 'https'])}\z/).nil?
end