This repository has been archived by the owner on Oct 19, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgraph-easy
executable file
·713 lines (541 loc) · 18.6 KB
/
graph-easy
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
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
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
#!/usr/bin/env perl -w
use strict;
use Graph::Easy 0.63;
use Graph::Easy::Parser;
my $help_requested = 0;
# echo "[A]" | graph-easy # should work
# graph-easy # need help
$help_requested = 1 if @ARGV == 0 && -t STDIN;
# list of supported output formats for external renderers like dot:
my @external = qw/png bmp gif jpg pdf ps ps2 tif tga pcl hpgl/;
my $external = join('|',@external);
my $qr_ext = qr/^($external)\z/;
my $OUT = \*STDERR;
my $opt = get_options();
# error?
$help_requested = 1 if !ref($opt);
# no error and --help was specified
$help_requested = 2 if ref($opt) && $opt->{help} ne '';
my $copyright = "Graph::Easy v$Graph::Easy::VERSION (c) by Tels 2004-2008. "
."Released under the GPL 2.0 or later.\n\n";
if (ref($opt) && $opt->{version} != 0)
{
print $copyright;
print "Running under Perl v$]";
eval { require Graph::Easy::As_svg; };
if (defined $Graph::Easy::As_svg::VERSION)
{
print " and using Graph::Easy::As_svg v$Graph::Easy::As_svg::VERSION";
}
print ".\n\n";
exit 2;
}
if ($help_requested > 0)
{
print STDERR $copyright;
require Pod::Usage;
if ($help_requested > 1 && $Pod::Usage::VERSION < 1.35)
{
# The way old Pod::Usage executes "perldoc" might fail:
system('perldoc', $0);
exit 2;
}
Pod::Usage::pod2usage( { -exitval => 2, -verbose => $help_requested } );
}
my $verbose = $opt->{verbose};
print $OUT $copyright if $verbose;
#############################################################################
# Create the parser object
my $parser_class = 'Graph::Easy::Parser';
if ($opt->{from} eq 'graphviz')
{
require Graph::Easy::Parser::Graphviz;
$parser_class = 'Graph::Easy::Parser::Graphviz';
}
elsif ($opt->{from} =~ /^(vcg|gdl)\z/)
{
require Graph::Easy::Parser::VCG;
$parser_class = 'Graph::Easy::Parser::VCG';
}
print $OUT "Creating $parser_class object.\n" if $verbose;
my $parser = $parser_class->new( debug => $opt->{debug} );
#############################################################################
# parse the input file
print $OUT "Parsing input in $opt->{from} from $opt->{inputname}.\n" if $verbose;
my $graph = $parser->from_file($opt->{input});
my $error = '';
$error = $parser->error() if !$graph || $parser->error();
$error = $graph->error() if $graph && $graph->error();
die ($error) if $error;
#############################################################################
# If wanted, generate the statistics:
if ($opt->{stats})
{
print STDERR "\nInput is a ",
$graph->is_simple() ? 'simple' : 'multi-edged',
", ",
$graph->is_undirected() ? 'undirected' : 'directed',
" graph with:\n";
my $nodes = $graph->nodes();
my $edges = $graph->edges();
my $groups = $graph->groups();
print STDERR " $nodes node" . ($nodes != 1 ? 's' : '') .
", $edges edge" . ($edges != 1 ? 's' : '') .
" and $groups group" . ($groups != 1 ? 's' : '') . "\n\n";
for my $g ($graph->groups())
{
my $nodes = $g->nodes();
my $edges = $g->edges();
my $groups = $g->groups();
print STDERR " Group '$g->{name}':\n";
print STDERR " $nodes node" . ($nodes != 1 ? 's' : '') .
", $edges edge" . ($edges != 1 ? 's' : '') .
" and $groups group" . ($groups != 1 ? 's' : '') . "\n\n";
}
}
#############################################################################
# Generate the wanted output format and write it to the output:
if (! $opt->{parse})
{
my $method = 'as_' . $opt->{as} . '_file';
if ($verbose)
{
if ($opt->{outputname} =~ /\.$external\z/)
{
print $OUT "Piping output to '$opt->{renderer} -T$opt->{ext} -o \"$opt->{outputname}\"'.\n";
}
else
{
print $OUT "Writing output as $opt->{as} to $opt->{outputname}.\n";
}
}
$graph->timeout(abs($opt->{timeout} || 240));
my $FILE = $opt->{output};
print $FILE $graph->$method();
print $OUT "Everything done. Have fun!\n\n" if $verbose;
}
#############################################################################
# Everything done
#############################################################################
#############################################################################
sub get_options
{
# set the defaults
my $opt = {
input => undef,
output => undef,
as => '',
from => 'txt',
help => '',
as_ascii => '',
as_boxart => '',
as_html => '',
as_svg => '',
as_graphviz => '',
as_txt => '',
as_vcg => '',
as_gdl => '',
as_graphml => '',
debug => 0,
from_txt => '',
from_vcg => '',
from_gdl => '',
from_graphviz => '',
verbose => 0,
version => 0,
parse => 0,
stats => 0,
timeout => 240,
renderer => 'dot',
};
# insert the ones from @external
for my $e (@external) { $opt->{$e} = ''; }
# map the output format to the method to generate the output:
my $formats = {
html => 'html',
txt => 'ascii',
svg => 'svg',
dot => 'graphviz',
vcg => 'vcg',
gdl => 'gdl',
graphml => 'graphml',
};
# insert the ones from @external
for my $e (@external) { $formats->{$e} = 'graphviz'; }
# do we have some options?
if (@ARGV > 0)
{
require Getopt::Long;
my @o = (
"input=s" => \$opt->{input},
"output=s" => \$opt->{output},
"as=s" => \$opt->{as},
"from=s" => \$opt->{from},
"help|?" => \$opt->{help},
"version" => \$opt->{version},
"verbose" => \$opt->{verbose},
"debug=i" => \$opt->{debug},
"parse" => \$opt->{parse},
"as_ascii|ascii" => \$opt->{as_ascii},
"as_html|html" => \$opt->{as_html},
"as_svg|svg" => \$opt->{as_svg},
"as_txt|txt" => \$opt->{as_txt},
"as_vcg|vcg" => \$opt->{as_vcg},
"as_gdl|gdl" => \$opt->{as_gdl},
"as_graphml|graphml" => \$opt->{as_graphml},
"as_graphviz|graphviz|as_dot|dot" => \$opt->{as_graphviz},
"as_boxart|boxart" => \$opt->{as_boxart},
"timeout=i" => \$opt->{timeout},
"renderer=s" => \$opt->{renderer},
"stats" => \$opt->{stats},
"from_txt" => \$opt->{from_txt},
"from_vcg" => \$opt->{from_vcg},
"from_gdl" => \$opt->{from_gdl},
"from_graphviz" => \$opt->{from_graphviz},
);
# insert the ones from @external
for my $e (@external) { push @o, "as_$e|$e" => \$opt->{"as_$e"}; }
return unless Getopt::Long::GetOptions (@o);
}
# allow "as=dot" for easier usage:
$opt->{as} = 'graphviz' if $opt->{as} eq 'dot';
# make the renderer argument sane to avoid --renderer=';rm -fR *':
$opt->{renderer} =~ s/[^a-zA-Z0-9_\\\/\:\.-]//g;
# if there are arguments left, they are input and possible output
$opt->{input} = shift @ARGV if @ARGV;
$opt->{output} = shift @ARGV if @ARGV;
if (!defined $opt->{input})
{
$opt->{input} = \*STDIN;
$opt->{inputname} = 'STDIN';
}
else
{
$opt->{inputname} = $opt->{input};
}
# This code gets confused if the user specified multiple options. Not much
# can be done about that except whack the user with something heavy:
for my $format (qw/ascii boxart html svg txt graphviz vcg gdl graphml/, @external )
{
warn ("Warning: Output format '$format' overrides specified '$opt->{as}'")
if $opt->{"as_$format"} && $opt->{as};
$opt->{as} = $format if $opt->{"as_$format"};
delete $opt->{"as_$format"};
}
if ($opt->{as} =~ $qr_ext)
{
$opt->{output} = $opt->{input} unless defined $opt->{output};
# set some default output name, so the replace works correctly
$opt->{output} = 'graph.txt' if ref($opt->{input});
# two-step process to fix bug #37534 - overwrites input with no extension
# example.txt => example
$opt->{output} =~ s/\.(txt|dot|vcg|gdl|graphml|$external)\z//;
# example => example.png
$opt->{output} .= ".$opt->{as}";
}
if (!defined $opt->{output})
{
$opt->{outputname} = 'STDOUT';
$opt->{output} = \*STDOUT;
# default to ASCII if nothing is known
$opt->{as} = 'ascii' if $opt->{as} eq '';
}
else
{
my $file = $opt->{output};
$opt->{outputname} = $opt->{output};
if ($opt->{as} eq '')
{
$opt->{as} = 'ascii'; # default
$opt->{as} = $formats->{$1} if $file =~ /\.(html|svg|txt|dot|vcg|gdl|graphml|$external)\z/;
}
$opt->{output} = undef;
if ($opt->{as} !~ $qr_ext)
{
# do not clobber the output file if we cannot read the input
return unless ref $opt->{input} || -R $opt->{input};
open $opt->{output}, ">", $file or die ("Cannot write to $file: $!");
}
else
{
# open a pipe to dot/neato etc.
my $file_save = $file;
$file_save =~ s/["'\|;]//g; # remove potentially unsafe characters
open $opt->{output}, "|$opt->{renderer} -T$opt->{as} -o \"$file_save\"" or die ("Cannot open pipe to dot: $!");
binmode $opt->{output}, ':utf8';
}
}
if ($opt->{as} !~ $qr_ext)
{
binmode ($opt->{output}, ':utf8') or die ("Cannot do binmode(output,':utf8')");
}
else
{
$opt->{ext} = $opt->{as};
$opt->{as} = 'graphviz';
}
# convert "from_vcg" to "from=vcg"
for my $format (qw/txt graphviz dot vcg gdl/)
{
$opt->{from} = $format if $opt->{"from_$format"};
delete $opt->{"from_$format"};
}
$opt->{from} = 'graphviz' if $opt->{from} eq 'dot';
die ("Unknown input format '$opt->{from}'")
unless $opt->{from} =~ /^(vcg|gdl|graphviz|txt)\z/;
$opt;
}
__END__
=pod
=head1 NAME
graph-easy - render/convert graphs in/from various formats
=head1 SYNOPSIS
Convert between graph formats and layout/render graphs:
graph-easy [options] [inputfile [outputfile]]
echo "[ Bonn ] - car -> [ Berlin ]" | graph-easy
graph-easy --input=graph.dot --as_ascii
graph-easy --html --output=mygraph.html graph.txt
graph-easy graph.txt graph.svg
graph-easy graph.txt --as_dot | dot -Tpng -o graph.png
graph-easy graph.txt --png
graph-easy graph.vcg --dot
graph-easy graph.dot --gdl
graph-easy graph.dot --graphml
=head1 ARGUMENTS
Here are the most important options, more are listed in the full
documentation:
=over 10
=item --help
Print the full documentation, not just this short overview.
=item --input
Specify the input file name. Example:
graph-easy --input=input.txt
The format will be auto-detected, override it with L<--from>.
=item --output
Specify the output file name. Example:
graph-easy --output=output.txt input.txt
=item --as
Specify the output format. Example:
graph-easy --as=ascii input.txt
Valid formats are:
ascii ASCII art rendering
boxart Unicode Boxart rendering
html HTML
svg Scalable Vector Graphics
graphviz the DOT language
dot alias for "graphviz"
txt Graph::Easy text
vcg VCG (Visualizing Compiler Graphs - a subset of GDL) text
gdl GDL (Graph Description Language) text
graphml GraphML
In addition, the following formats are understood and piped through the program
specified with the --renderer option (default: dot):
bmp Windows bitmap
gif GIF
hpgl HP-GL/2 vector graphic
jpg JPEG
pcl PCL printer language
pdf PDF
png PNG
ps Postscript
ps2 Postscript with PDF notations (see graphviz documentation)
tga Targa bitmap
tif TIFF bitmap
The default format will be determined by the output filename extension,
and is C<ascii>, if the output filename was not set.
You can also use B<ONE> argument of the form C<--as_ascii> or C<--ascii>.
=item --from
Specify the input format. Valid formats are:
graphviz the DOT language
txt Graph::Easy text
vcg VCG text
gdl GDL (Graph Description Language) text
If not specified, the input format is auto-detected.
You can also use B<ONE> argument of the form C<--from_dot>, etc.
=item --renderer
The external program (default: "dot") used to render the output
formats like C<png>, C<jpg> etc. Some choices are "neato", "twopi", "fdp" or "circo".
=item --parse
Input will only be parsed, without any output generation.
Useful in combination with C<--debug=1> or C<--stats>. Example:
graph-easy input.txt --parse --debug=1
=item --stats
Write various statistics about the input graph to STDERR. Best used in
combination with C<--parse>:
graph-easy input.txt --parse --stats
=item --timeout
Set the timeout B<in seconds> for the Graph::Easy layouter that generates
ASCII, HTML, SVG or boxart output. If the layout does not
finish in this time, it will be aborted. Example:
graph-easy input.txt --timeout=500
Conversion to DOT, VCG/GDL, GraphML or plain text ignores the timeout.
The default is 240 seconds (4 minutes).
=item --verbose
Write info regarding the conversion process to STDERR.
=back
=head1 DESCRIPTION
C<graph-easy> reads a description of a graph (a connected network of
nodes and edges, not a pie chart :-) and then converts this to the desired
output format.
By default, the input will be read from STDIN, and the output will go to
STDOUT. The input is expected to be encoded in UTF-8, the output will
also be UTF-8.
It understands the following formats as input:
Graph::Easy http://bloodgate.com/perl/graph/manual/
DOT http://www.graphviz.org/
VCG http://rw4.cs.uni-sb.de/~sander/html/gsvcg1.html
GDL http://www.aisee.com/
The formats are automatically detected, regardless of the input file name,
but you can also explicitly declare your input to be in one specific
format.
The output can be a dump of the graph in one of the following formats:
Graph::Easy http://bloodgate.com/perl/graph/manual/
DOT http://www.graphviz.org/
VCG http://rw4.cs.uni-sb.de/~sander/html/gsvcg1.html
GDL http://www.aisee.com/
GraphML http://graphml.graphdrawing.org/
In addition, C<Graph::Easy> can also create layouts of graphs in
one of the following output formats:
HTML SVG ASCII BOXART
Note that for SVG output, you need to install the module
L<Graph::Easy::As_svg> first.
As a shortcut, you can also specify the output format as 'png', this will
cause C<graph-easy> to pipe the input in graphviz format to the C<dot> program
to create a PNG file in one step. The following two examples are equivalent:
graph-easy graph.txt --dot | dot -Tpng -o graph.png
graph-easy graph.txt --png
X<svg>
X<html>
X<ascii>
X<boxart>
X<png>
X<dot>
X<graphviz>
X<vcg>
X<gdl>
X<graph description language>
X<unicode>
=head1 OTHER ARGUMENTS
C<graph-easy> supports a few more arguments in addition to the ones from above:
=over 10
=item --version
Write version info and exit.
=item --debug=N
Set the debug level (1..3). Warning, this will generate huge
amounts of hard to understand output on STDERR. Example:
graph-easy input.txt --output=test.html --debug=1
=item --png, --dot, --vcg, --gdl, --txt, --ascii, --boxart, --html, --svg
Given exactly one of these options, produces the desired output format.
=back
=head1 EXAMPLES
=head2 ASCII output
echo "[ Bonn ] -- car --> [ Berlin ], [ Ulm ]" | graph-easy
+--------+ car +-----+
| Bonn | -----> | Ulm |
+--------+ +-----+
|
| car
v
+--------+
| Berlin |
+--------+
=head2 Graphviz example output
echo "[ Bonn ] -- car --> [ Berlin ], [ Ulm ]" | graph-easy --dot
digraph GRAPH_0 {
edge [ arrowhead=open ];
graph [ rankdir=LR ];
node [
fontsize=11,
fillcolor=white,
style=filled,
shape=box ];
Bonn -> Ulm [ label=car ]
Bonn -> Berlin [ label=car ]
}
=head2 VCG example output
echo "[ Bonn ] -- car --> [ Berlin ], [ Ulm ]" | graph-easy --vcg
graph: {
title: "Untitled graph"
node: { title: "Berlin" }
node: { title: "Bonn" }
node: { title: "Ulm" }
edge: { label: "car" sourcename: "Bonn" targetname: "Ulm" }
edge: { label: "car" sourcename: "Bonn" targetname: "Berlin" }
}
=head2 GDL example output
GDL (Graph Description Language) is a superset of VCG, and thus the output will
look almost the same as VCG:
echo "[ Bonn ] -- car --> [ Berlin ], [ Ulm ]" | graph-easy --gdl
graph: {
title: "Untitled graph"
node: { title: "Berlin" }
node: { title: "Bonn" }
node: { title: "Ulm" }
edge: { label: "car" source: "Bonn" target: "Ulm" }
edge: { label: "car" source: "Bonn" target: "Berlin" }
}
=head2 GraphML example output
GraphML is XML:
echo "[ Bonn ] -- car --> [ Berlin ], [ Ulm ]" | graph-easy --graphml
<?xml version="1.0" encoding="UTF-8"?>
<graphml xmlns="http://graphml.graphdrawing.org/xmlns"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://graphml.graphdrawing.org/xmlns
http://graphml.graphdrawing.org/xmlns/1.0/graphml.xsd">
<!-- Created by Graph::Easy v0.58 at Mon Aug 20 00:01:25 2007 -->
<key id="d0" for="edge" attr.name="label" attr.type="string"/>
<graph id="G" edgedefault="directed">
<node id="Berlin">
</node>
<node id="Bonn">
</node>
<node id="Ulm">
</node>
<edge source="Bonn" target="Berlin">
<data key="d0">car</data>
</edge>
<edge source="Bonn" target="Ulm">
<data key="d0">car</data>
</edge>
</graph>
<graphml>
=head1 CAVEATS
Please note that it is impossible to convert 100% from one format to another
format since every graph language out there has features that are unique to
only this language.
In addition, the conversion process always converts the input first into an
L<Graph::Easy> graph, and then to the desired output format.
This means that only features and attributes that are actually valid in
Graph::Easy are supported yet. Work in making Graph::Easy an universal
format supporting as much as possible is still in progress.
Attributes that are not yet supported natively by Graph::Easy are converted
to custom attributes with a prefixed C<x-format->, f.i. C<x-dot->. Upon output
to the same format, these are converted back, but conversion to a different
format will lose these attributes.
For a list of what problems still remain, please see the TODO
file in the C<Graph::Easy> distribution on CPAN:
L<http://search.cpan.org/~tels/Graph-Easy/>
If you notice anything wrong, or miss attributes, please file a bug report on
L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Graph-Easy>
so we can fix it and include the missing things into Graph::Easy!
X<bugreport>
=head1 LICENSE
This library is free software; you can redistribute it and/or modify
it under the terms of the GPL.
See the LICENSE file of Graph::Easy for a copy of the GPL.
This product includes color specifications and designs developed by Cynthia
Brewer (L<http://colorbrewer.org/>). See the LICENSE file for the full license
text that applies to these color schemes.
X<gpl>
X<apache-style>
X<cynthia>
X<brewer>
X<colorscheme>
X<license>
=head1 AUTHOR
Copyright (C) 2004 - 2008 by Tels L<http://bloodgate.com>
=head1 SEE ALSO
More information can be found in the online manual of Graph::Easy:
L<http://bloodgate.com/perl/graph/manual/>
See also: L<Graph::Easy>, L<Graph::Easy::Manual>
=cut