Class: Asciidoctor::Standoc::PlantUMLBlockMacroBackend

Inherits:
Object
  • Object
show all
Defined in:
lib/asciidoctor/standoc/macros.rb

Class Method Summary collapse

Class Method Details

.generate_attrs(attrs) ⇒ Object



228
229
230
231
232
233
234
# File 'lib/asciidoctor/standoc/macros.rb', line 228

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

.generate_file(parent, reader) ⇒ Object

if no :imagesdir: leave image file in plantuml sleep need for windows because dot works in separate process and plantuml process may finish earlier then dot, as result png file maybe not created yet after plantuml finish



188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
# File 'lib/asciidoctor/standoc/macros.rb', line 188

def self.generate_file parent, reader
  localdir = Utils::localdir(parent.document)

  imagesdir = parent.document.attr('imagesdir')
  umlfile, outfile = save_plantuml parent, reader, localdir

  # TODO: this should raise failure if there is no image output!!
  run(umlfile, outfile) or return
  umlfile.unlink

  path = Pathname.new(localdir) + (imagesdir || "plantuml")
  path.mkpath

  # TODO: this should raise failure if the destination path already exists!
  File.exist?(path) or return

  # TODO: this should raise failure if the destination path is not writable!
  File.writable?(path) or return

  # Warning for Heisenbug: metanorma/metanorma-standoc#187
  # Windows Ruby 2.4 will crash if a Tempfile is "mv"ed.
  # This is why we need to copy and then unlink.
  filename = File.basename(outfile.to_s)
  FileUtils.cp(outfile, path) && outfile.unlink

  imagesdir ? filename : File.join(path, filename)
end

.plantuml_installed?Boolean

Returns:

  • (Boolean)


162
163
164
165
166
167
168
169
170
171
172
# File 'lib/asciidoctor/standoc/macros.rb', line 162

def self.plantuml_installed?
  cmd = "plantuml"
  exts = ENV['PATHEXT'] ? ENV['PATHEXT'].split(';') : ['']
  ENV['PATH'].split(File::PATH_SEPARATOR).each do |path|
    exts.each do |ext|
      exe = File.join(path, "#{cmd}#{ext}")
      return exe if File.executable?(exe) && !File.directory?(exe)
    end
  end
  nil
end

.run(umlfile, outfile) ⇒ Object



174
175
176
177
178
179
180
181
182
# File 'lib/asciidoctor/standoc/macros.rb', line 174

def self.run umlfile, outfile
  system "plantuml #{umlfile.path}" or (warn $? and return false)
  i = 0
  until !Gem.win_platform? || File.exist?(outfile) || i == 15
    sleep(1)
    i += 1
  end
  File.exist?(outfile)
end

.save_plantuml(parent, reader, localdir) ⇒ Object



216
217
218
219
220
221
222
223
224
225
226
# File 'lib/asciidoctor/standoc/macros.rb', line 216

def self.save_plantuml parent, reader, localdir
  src = reader.source
  reader.lines.first.sub(/\s+$/, "").match /^@startuml($| )/ or
    src = "@startuml\n#{src}\n@enduml\n"
  /^@startuml (?<fn>[^\n]+)\n/ =~ src
  Tempfile.open(["plantuml", ".pml"], :encoding => "utf-8") do |f|
    f.write(src)
    [f, File.join(File.dirname(f.path),
                  (fn || File.basename(f.path, ".pml")) + ".png")]
  end
end