Skip to content

Instantly share code, notes, and snippets.

@zerodogg
Created November 22, 2010 13:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zerodogg/709956 to your computer and use it in GitHub Desktop.
Save zerodogg/709956 to your computer and use it in GitHub Desktop.
Quick and dirty wrapper around Graph::Easy
#!/usr/bin/perl
# graph_from_text
# Copyright (C) Eskild Hustvedt 2007
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
use strict;
use warnings;
use utf8;
use Graph::Easy::Parser;
# Purpose: Check for a file in path
# Usage: InPath(FILE)
sub InPath {
foreach (split /:/, $ENV{PATH}) { if (-x "$_/@_" and ! -d "$_/@_" ) { return 1; } } return 0;
}
die("Needs at least one parameter: file to work on.\n") if not @ARGV;
my $file = shift(@ARGV);
die("$file: does not exist\n") if not -e $file;
my $outfile = $file;
$outfile =~ s/\.\w\w\w\w?$//;
my $g = Graph::Easy::Parser->new();
my $p = $g->from_file($file);
if (defined($ARGV[0]) and $ARGV[0] eq 'ascii')
{
print $p->as_ascii();
}
elsif (defined($ARGV[0]) and $ARGV[0] eq 'html')
{
$outfile .= '.html';
print "Writing $outfile...";
open(my $OUT,'>',$outfile) or die ("Can't open file $outfile: $!\n");
no warnings; # Avoid 'wide character in print' errors
print $OUT $p->as_html_file();
use warnings;
close($OUT);
print "done\n";
}
else
{
die("You don't appear to have 'dot' installed. Please install graphviz, then re-run this program.\n") if not InPath('dot');
$outfile .= '.png';
$| = 1;
print "Writing $outfile...";
my $graphviz = $p->as_graphviz();
my $dotProg = 'dot';
if($ENV{DOT_REPLACEMENT})
{
$dotProg = $ENV{DOT_REPLACEMENT};
}
open my $DOT, '|'.$dotProg.' -Tpng -o '.$outfile or die ("Cannot open pipe to dot: $!");
print $DOT $graphviz;
close $DOT;
print "done\n";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment