Class: Asciidoctor::Diagram::DiagramsConverter

Inherits:
Object
  • Object
show all
Includes:
CliGenerator, DiagramConverter
Defined in:
lib/asciidoctor-diagram/diagrams/converter.rb

Instance Method Summary collapse

Methods included from CliGenerator

#generate_file, #generate_file_stdout, #generate_stdin, #generate_stdin_file, #generate_stdin_stdout

Methods included from DiagramConverter

#collect_options, #native_scaling?, #wrap_source

Instance Method Details

#convert(source, format, options) ⇒ Object



17
18
19
20
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
# File 'lib/asciidoctor-diagram/diagrams/converter.rb', line 17

def convert(source, format, options)
  python_path = source.find_command('python3', :attrs => ['diagrams-python'], :alt_cmds => ['python'])

  code = source.to_s

  match_data = /Diagram\((.*?)\)/.match(code)
  raise "Could not find Diagram constructor" unless match_data

  args = match_data[1].strip

  target_file = Tempfile.new('diagrams')

  diagram = 'Diagram('
  diagram << args
  diagram << ',' unless args.empty?
  diagram << "filename=\"#{target_file.path}\""
  diagram << ",outformat=\"#{format}\""
  diagram << ')'

  code = match_data.pre_match + diagram + match_data.post_match

  begin
    target_file.close
    generate_stdin_file(python_path, code, target_file.path + ".#{format}") do |tool|
      {
        :args => [tool, '-'],
        :chdir => source.base_dir
      }
    end
  ensure
    target_file.unlink
  end
end

#supported_formatsObject



13
14
15
# File 'lib/asciidoctor-diagram/diagrams/converter.rb', line 13

def supported_formats
  [:png, :svg, :pdf]
end