module Metanorma::Plugin::Lutaml::LutamlDiagramBase

Public Instance Methods

lutaml_file(reader) click to toggle source
# File lib/metanorma/plugin/lutaml/lutaml_diagram_base.rb, line 26
def lutaml_file(reader)
  raise 'Implement me!'
end
process(parent, reader, attrs) click to toggle source
# File lib/metanorma/plugin/lutaml/lutaml_diagram_base.rb, line 15
def process(parent, reader, attrs)
  uml_document = ::Lutaml::Uml::Parsers::Dsl.parse(lutaml_file(parent.document, reader))
  filename = generate_file(parent, reader, uml_document)
  through_attrs = generate_attrs(attrs)
  through_attrs["target"] = filename
  through_attrs["title"] = uml_document.caption
  create_image_block(parent, through_attrs)
rescue StandardError => e
  abort(parent, reader, attrs, e.message)
end

Protected Instance Methods

abort(parent, reader, attrs, msg) click to toggle source
# File lib/metanorma/plugin/lutaml/lutaml_diagram_base.rb, line 32
def abort(parent, reader, attrs, msg)
  warn(msg)
  attrs["language"] = "lutaml"
  source = reader.respond_to?(:source) ? reader.source : reader
  create_listing_block(
    parent,
    source,
    attrs.reject { |k, _v| k == 1 }
  )
end
generate_attrs(attrs) click to toggle source
# File lib/metanorma/plugin/lutaml/lutaml_diagram_base.rb, line 71
def generate_attrs(attrs)
  %w(id align float title role width height alt)
    .reduce({}) do |memo, key|
      memo[key] = attrs[key] if attrs.has_key? key
      memo
    end
end
generate_file(parent, _reader, uml_document) click to toggle source

if no :imagesdir: leave image file in lutaml

# File lib/metanorma/plugin/lutaml/lutaml_diagram_base.rb, line 44
def generate_file(parent, _reader, uml_document)
  formatter = ::Lutaml::Formatter::Graphviz.new
  formatter.type = :png

  imagesdir = if parent.document.attr("imagesdir")
                File.join(parent.document.attr("imagesdir"), "lutaml")
              else
                "lutaml"
              end
  result_path = Utils.relative_file_path(parent.document, imagesdir)
  result_pathname = Pathname.new(result_path)
  result_pathname.mkpath
  File.writable?(result_pathname) || raise("Destination path #{result_path} not writable for Lutaml!")

  outfile = Tempfile.new(["lutaml", ".png"])
  outfile.binmode
  outfile.puts(formatter.format(uml_document))

  # Warning: 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.path)
  FileUtils.cp(outfile, result_pathname) && outfile.unlink

  File.join(result_pathname, filename)
end