Class: Metanorma::Plugin::Plantuml::Wrapper

Inherits:
Object
  • Object
show all
Defined in:
lib/metanorma/plugin/plantuml/wrapper.rb

Constant Summary collapse

PLANTUML_JAR_NAME =
"plantuml.jar"
PLANTUML_JAR_PATH =
File.join(
  Gem::Specification.find_by_name("metanorma-plugin-plantuml").gem_dir,
  "data", PLANTUML_JAR_NAME
)
SUPPORTED_FORMATS =
%w[png svg pdf txt eps].freeze
DEFAULT_FORMAT =
"png"

Class Method Summary collapse

Class Method Details

.available?Boolean

Returns:

  • (Boolean)


78
79
80
81
82
# File 'lib/metanorma/plugin/plantuml/wrapper.rb', line 78

def available?
  return false if ENV["PLANTUML_DISABLED"] == "true"

  File.exist?(PLANTUML_JAR_PATH) && java_available?
end

.generate(content, format: DEFAULT_FORMAT, output_file: nil, **options) ⇒ Object

rubocop:disable Metrics/MethodLength



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/metanorma/plugin/plantuml/wrapper.rb', line 22

def generate( # rubocop:disable Metrics/MethodLength
  content,
  format: DEFAULT_FORMAT,
  output_file: nil, **options
)
  validate_format!(format)
  ensure_jar_available!
  ensure_java_available!

  content = content.dup.force_encoding("UTF-8")
  include_files = get_include_files(content, options)
  options[:include_files] = include_files unless include_files.empty?

  result = if output_file
             generate_to_file(content, format, output_file, options)
           else
             generate_to_temp_file(content, format, options)
           end

  { success: true }.merge(result)
rescue PlantumlError => e
  { success: false, error: e }
end

.get_include_files(content, _options) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/metanorma/plugin/plantuml/wrapper.rb', line 46

def get_include_files(content, _options)
  include_files = []
  content.each_line do |line|
    case line
    when /(!include|!includesub)\s(.+){1}/
      found_file = ::Regexp.last_match(2).split("!").first

      # skip web links and standard libraries
      found_file = nil if found_file.start_with?("<", "http")

      include_files << found_file
    end
  end
  include_files.compact.uniq
end

.jar_pathObject



84
85
86
# File 'lib/metanorma/plugin/plantuml/wrapper.rb', line 84

def jar_path
  PLANTUML_JAR_PATH
end

.versionObject



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/metanorma/plugin/plantuml/wrapper.rb', line 62

def version
  return nil unless available?

  cmd = [configuration.java_path, *configuration.jvm_options, "-jar",
         PLANTUML_JAR_PATH, "-version"]
  output, _, status = Open3.capture3(*cmd)

  if status.success?
    # Extract version from output
    version_match = output.match(/PlantUML version ([\d.]+)/)
    version_match ? version_match[1] : PLANTUML_JAR_VERSION
  end
rescue StandardError
  nil
end