Method: GraphViz#output

Defined in:
lib/graphviz.rb

#output(hOpts = {}) ⇒ Object Also known as: save

Generate the graph

Options :

  • :output : Output format (GraphViz::Constants::FORMATS)

  • :file : Output file name

  • :use : Program to use (GraphViz::Constants::PROGRAMS)

  • :path : Program PATH

  • :<format> => <file> : <file> can be

    • a file name

    • nil, then the output will be printed to STDOUT

    • String, then the output will be returned as a String

  • :errors : DOT error level (default 1)

    • 0 = Error + Warning

    • 1 = Error

    • 2 = none



447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
# File 'lib/graphviz.rb', line 447

def output( hOpts = {} )
  xDOTScript = DOTScript.new
  lNotHugly = []

  append_attributes_and_types(xDOTScript)

  xDOTScript << "}"

  if has_parent_graph?
    xDOTScript.make_subgraph("#{GraphViz.escape(@name, :unquote_empty => true)}")
  else
    hOutput = {}

    hOpts.each do |xKey, xValue|
      xValue = xValue.to_s unless xValue.nil? or [Class, TrueClass, FalseClass].include?(xValue.class)
      case xKey.to_s
        when "use"
          if PROGRAMS.index( xValue ).nil?
            raise ArgumentError, "can't use '#{xValue}'"
          end
          @prog = xValue
        when "path"
          @path = xValue && xValue.split( "," ).map{ |x| x.strip }
        when "errors"
          @errors = xValue
        when "extlib"
          @extlibs = xValue.split( "," ).map{ |x| x.strip }
        when "scale"
          # Scale input by 'v' (=72)
          @scale = xValue
        when "inverty"
          # Invert y coordinate in output
          @inverty = xValue
        when "no_layout"
          # No layout mode 'v' (=1)
          @no_layout = xValue
        when "reduce"
          # Reduce graph
          @reduce_graph = xValue
        when "Lg"
          # Don't use grid
          @Lg = xValue
        when "LO"
          # Use old attractive force
          @LO = xValue
        when "Ln"
          # Set number of iterations to i
          @Ln = xValue
        when "LU"
          # Set unscaled factor to i
          @LU = xValue
        when "LC"
          # Set overlap expansion factor to v
          @LC = xValue
        when "LT"
          # Set temperature (temperature factor) to v
          @LT = xValue
        when "nothugly"
          begin
            require 'graphviz/nothugly'
            @nothugly = true
          rescue LoadError
            warn "You must install ruby-xslt or libxslt-ruby to use nothugly option!"
            @nothugly = false
          end
        else
          if FORMATS.index( xKey.to_s ).nil?
            raise ArgumentError, "output format '#{xValue}' invalid"
          end
          hOutput[xKey.to_s] = xValue
      end
    end

    @output = hOutput if hOutput.size > 0

    xStict = ((@strict && @oGraphType == "digraph") ? "strict " : "")
    xDOTScript.prepend(
      "#{xStict}#{@oGraphType} #{GraphViz.escape(@name, :unquote_empty => true)} {"
    )

    xOutputString = (@filename == String ||
      @output.any? {|format, file| file == String })

    xOutput = String.new
    if @format.to_s == "none" or @output.any? {|fmt, fn| fmt.to_s == "none"}
      if xOutputString
        xOutput << xDOTScript
      else
        xFileName = @output["none"] || @filename
        open( xFileName, "w" ) do |h|
          h.puts xDOTScript
        end
      end
    end

    if (@format.to_s != "none" and not @format.nil?) or (@output.any? {|format, file| format != "none" } and @output.size > 0)
      ## Act: Save script and send it to dot
      t = Tempfile::open( File.basename(__FILE__) )
      t.print( xDOTScript )
      t.close

      cmd = find_executable( @prog, @path )
      if cmd == nil
        raise StandardError, "GraphViz not installed or #{@prog} not in PATH. Install GraphViz or use the 'path' option"
      end

      xOutputWithFile = []
      xOutputWithoutFile = []
      unless @format.nil? or @format == "none"
        lNotHugly << @filename if @format.to_s == "svg" and @nothugly
        if @filename.nil? or @filename == String
          xOutputWithoutFile = ["-T#{@format}"]
        else
          xOutputWithFile = ["-T#{@format}", "-o#{@filename}"]
        end
      end
      @output.each_except( :key => ["none"] ) do |format, file|
        lNotHugly << file if format.to_s == "svg" and @nothugly
        if file.nil? or file == String
          xOutputWithoutFile += ["-T#{format}"]
        else
          xOutputWithFile += ["-T#{format}", "-o#{file}"]
        end
      end

      xExternalLibraries = @extlibs.map { |lib| "-l#{lib}" }

      xOtherOptions = []
      xOtherOptions << "-s#{@scale}" if @scale
      xOtherOptions << "-y" if @inverty
      xOtherOptions << "-n#{@no_layout}" if @no_layout
      xOtherOptions << "-x" if @reduce_graph
      xOtherOptions << "-Lg" if @Lg
      xOtherOptions << "-LO" if @LO
      xOtherOptions << "-Ln#{@Ln}" if @Ln
      xOtherOptions << "-LU#{@LU}" if @LU
      xOtherOptions << "-LC#{@LC}" if @LC
      xOtherOptions << "-LT#{@LT}" if @LT

      tmpPath = if IS_JRUBY
        t.path
      elsif IS_CYGWIN
        begin
          IO.popen("cygpath", "-w", t.path).chomp
        rescue
          warn "cygpath is not installed!"
          t.path
        end
      else
        t.path
      end

      xCmd = [cmd, "-q#{@errors}"] +
             xExternalLibraries +
             xOtherOptions +
             xOutputWithFile +
             xOutputWithoutFile +
             [tmpPath]

      xOutput << output_from_command( xCmd )
    end

    # Not Hugly
    lNotHugly.each do |f|
      if f.nil? or f == String
        xOutput = GraphViz.nothugly( xOutput, false )
      else
        GraphViz.nothugly( f, true )
      end
    end

    if xOutputString
      xOutput
    else
      print xOutput
    end
  end
end