Class: Asciidoctor::Diagram::TikZConverter

Inherits:
Object
  • Object
show all
Includes:
CliGenerator, DiagramConverter
Defined in:
lib/asciidoctor-diagram/tikz/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

#native_scaling?, #wrap_source

Instance Method Details

#collect_options(source) ⇒ Object



16
17
18
19
20
# File 'lib/asciidoctor-diagram/tikz/converter.rb', line 16

def collect_options(source)
  {
      :preamble => source.attr('preamble') == 'true'
  }
end

#convert(source, format, options) ⇒ Object



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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/asciidoctor-diagram/tikz/converter.rb', line 22

def convert(source, format, options)
  latexpath = source.find_command('pdflatex')

  if format == :svg
    svgpath = source.find_command('pdf2svg')
  else
    svgpath = nil
  end

  if options[:preamble]
    preamble, body = source.to_s.split(/^~~~~$/, 2)
    unless body
      body = preamble
      preamble = ''
    end
  else
    preamble = ''
    body = source.to_s
  end

  latex = <<'END'
\documentclass[border=2bp, tikz]{standalone}
\usepackage{tikz}
END
  latex << preamble
  latex << <<'END'
\begin{document}
\begingroup
\tikzset{every picture/.style={scale=1}}
END
  latex << body
  latex << <<'END'
\endgroup
\end{document}
END

  pdf = generate_file(latexpath, 'tex', 'pdf', latex) do |tool_path, input_path, output_path|
    {
        :args => [tool_path, '-shell-escape', '-file-line-error', '-interaction=nonstopmode', '-output-directory', Platform.native_path(File.dirname(output_path)), Platform.native_path(input_path)],
        :out_file => "#{File.dirname(input_path)}/#{File.basename(input_path, '.*')}.pdf"
    }
  end

  if svgpath
    generate_file(svgpath, 'pdf', 'svg', pdf) do |tool_path, input_path, output_path|
      {
        :args => [tool_path, Platform.native_path(input_path), Platform.native_path(output_path)],
        :chdir => source.base_dir
      }
    end
  else
    pdf
  end
end

#supported_formatsObject



12
13
14
# File 'lib/asciidoctor-diagram/tikz/converter.rb', line 12

def supported_formats
  [:pdf, :svg]
end