Class: Metanorma::Plugin::Plantuml::Backend

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

Class Method Summary collapse

Class Method Details

.generate_attrs(attrs) ⇒ Object



134
135
136
137
138
139
# File 'lib/metanorma/plugin/plantuml/backend.rb', line 134

def generate_attrs(attrs)
  %w[id align float title role width height alt]
    .each_with_object({}) do |key, memo|
    memo[key] = attrs[key] if attrs.key?(key)
  end
end

.generate_file(parent, source, format_override: nil, options: {}) ⇒ Object

rubocop:disable Metrics/AbcSize, Metrics/MethodLength



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/metanorma/plugin/plantuml/backend.rb', line 21

def generate_file( # rubocop:disable Metrics/AbcSize, Metrics/MethodLength
  parent, source, format_override: nil, options: {}
)
  ldir, imagesdir, fmt = generate_file_prep(parent)
  fmt = format_override if format_override
  validate_source(source)

  filename = generate_unique_filename(fmt)
  extracted_filename = FilenameResolver.extract(source)

  if extracted_filename
    safe_filename = FilenameResolver.sanitize(extracted_filename)
    filename = "#{safe_filename}.#{fmt}"
  end

  absolute_path, relative_path = path_prep(ldir, imagesdir)
  output_file = File.join(absolute_path, filename)

  result = Wrapper.generate(
    source,
    format: fmt,
    output_file: output_file,
    includedirs: options[:includedirs],
    layout: options[:layout],
  )

  unless result[:success]
    raise GenerationError,
          "No image output from PlantUML: #{result[:error].message}"
  end

  File.join(relative_path, filename)
end

.generate_file_prep(parent) ⇒ Object

rubocop:disable Metrics/AbcSize,Metrics/MethodLength



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/metanorma/plugin/plantuml/backend.rb', line 80

def generate_file_prep(parent) # rubocop:disable Metrics/AbcSize,Metrics/MethodLength
  imagesdir = if parent.document.attr("docfile") &&
      parent.document.attr("imagesdir")
                File.expand_path(
                  File.join(
                    File.dirname(parent.document.attr("docfile")),
                    parent.document.attr("imagesdir"),
                  ),
                )
              else
                parent.document.attr("imagesdir")
              end

  ldir = localdir(parent)
  fmt = parent.document
    .attr("plantuml-image-format")&.strip&.downcase ||
    Wrapper::DEFAULT_FORMAT
  [ldir, imagesdir, fmt]
end

.generate_multiple_files(parent, source, formats, attrs, options: {}) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/metanorma/plugin/plantuml/backend.rb', line 55

def generate_multiple_files(
  parent, source, formats, attrs, options: {}
)
  filenames = formats.map do |format|
    generate_file(parent, source,
                  format_override: format, options: options)
  end

  through_attrs = generate_attrs attrs
  through_attrs["target"] = filenames.first
  through_attrs["data-formats"] = formats.join(",")
  through_attrs["data-files"] = filenames.join(",")

  through_attrs
end

.localdir(parent) ⇒ Object

Raises:



100
101
102
103
104
105
106
# File 'lib/metanorma/plugin/plantuml/backend.rb', line 100

def localdir(parent)
  ret = Utils.localdir(parent.document)
  return ret if File.writable?(ret)

  raise GenerationError,
        "Destination directory #{ret} not writable for PlantUML!"
end

.path_prep(localdir, imagesdir) ⇒ Object

rubocop:disable Metrics/AbcSize,Metrics/MethodLength



108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/metanorma/plugin/plantuml/backend.rb', line 108

def path_prep(localdir, imagesdir) # rubocop:disable Metrics/AbcSize,Metrics/MethodLength
  sourcepath = if imagesdir.nil?
                 localdir
               elsif Pathname.new(imagesdir).absolute?
                 imagesdir
               else
                 File.join(localdir, imagesdir)
               end

  path = Pathname.new(
    File.expand_path(File.join(localdir, "_plantuml_images")),
  )
  path.mkpath

  unless File.writable?(path)
    raise GenerationError,
          "Destination path #{path} not writable for PlantUML!"
  end

  [
    path,
    Pathname.new(path)
      .relative_path_from(Pathname.new(sourcepath)).to_s,
  ]
end

.plantuml_available?Boolean

Returns:

  • (Boolean)


17
18
19
# File 'lib/metanorma/plugin/plantuml/backend.rb', line 17

def plantuml_available?
  Wrapper.available?
end

.plantuml_installed?Boolean

Returns:

  • (Boolean)

Raises:



11
12
13
14
15
# File 'lib/metanorma/plugin/plantuml/backend.rb', line 11

def plantuml_installed?
  return true if plantuml_available?

  raise PlantumlError, "PlantUML not installed"
end

.validate_source(source) ⇒ Object

Raises:



71
72
73
74
75
76
77
78
# File 'lib/metanorma/plugin/plantuml/backend.rb', line 71

def validate_source(source)
  diagram_type = extract_diagram_type(source)
  return if diagram_type && matching_end?(source, diagram_type)

  raise GenerationError,
        "@start#{diagram_type} without matching " \
        "@end#{diagram_type} in PlantUML!"
end