module Asciidoctor::Diagram::PlantUml

@private

Constants

JARS

Private Class Methods

included(mod) click to toggle source
# File lib/asciidoctor-diagram/plantuml/extension.rb, line 86
def self.included(mod)
  mod.register_format(:png, :image) do |parent_block, source|
    plantuml(parent_block, source, mod.tag, 'image/png')
  end
  mod.register_format(:svg, :image) do |parent_block, source|
    plantuml(parent_block, source, mod.tag, 'image/svg+xml')
  end
  mod.register_format(:txt, :literal) do |parent_block, source|
    plantuml(parent_block, source, mod.tag, 'text/plain;charset=utf-8')
  end
end

Private Instance Methods

plantuml(parent_block, source, tag, mime_type) click to toggle source
# File lib/asciidoctor-diagram/plantuml/extension.rb, line 23
def plantuml(parent_block, source, tag, mime_type)
  Java.load

  inherit_prefix = name
  code = preprocess_code(parent_block, source, tag)

  headers = {
      'Accept' => mime_type
  }

  config_file = source.attr('plantumlconfig', nil, true) || source.attr('config', nil, inherit_prefix)
  if config_file
    headers['X-PlantUML-Config'] = File.expand_path(config_file, source.attr('docdir', nil, true))
  end

  dot = which(parent_block, 'dot', :alt_attrs => ['graphvizdot'], :raise_on_error => false)
  if dot
    headers['X-Graphviz'] = ::Asciidoctor::Diagram::Platform.host_os_path(dot)
  end

  response = Java.send_request(
      :url => '/plantuml',
      :body => code,
      :headers => headers
  )

  unless response[:code] == 200
    raise Java.create_error("PlantUML image generation failed", response)
  end

  response[:body]
end
preprocess_code(parent, source, tag) click to toggle source
# File lib/asciidoctor-diagram/plantuml/extension.rb, line 56
def preprocess_code(parent, source, tag)
  code = source.to_s
  base_dir = source.base_dir

  code = "@start#{tag}\n#{code}\n@end#{tag}" unless code.index "@start#{tag}"

  code.gsub!(/(?<=<img:)[^>]+(?=>)/) do |match|
    resolve_path(match, parent, parent.attr('imagesdir'))
  end

  code.gsub!(/(?<=!include )\s*[^<][^!\n\r]+/) do |match|
    resolve_path(match.lstrip, parent, base_dir)
  end

  code
end
resolve_path(path, parent, base_dir) click to toggle source
# File lib/asciidoctor-diagram/plantuml/extension.rb, line 73
def resolve_path(path, parent, base_dir)
  if path =~ ::URI::ABS_URI
    uri = ::URI.parse(path)
    if uri.scheme == 'file'
      parent.normalize_system_path(uri.path, base_dir)
    else
      parent.normalize_web_path(path)
    end
  else
    parent.normalize_system_path(path, base_dir)
  end
end