#!/usr/bin/perl -w ############################################################################# # This example is a bit outdated, please use the new bin/grapheasy script - # which is after "make install" available in your system as simple as # "grapheasy" on any command line prompt. ############################################################################# # This script tries to generate graphs from all the files in t/syntax/ # and outputs the result as an HTML page. # Use it like: # examples/syntax.pl >test.html # and then open test.html in your favourite browser. BEGIN { chdir 'examples' if -d 'examples'; use lib '../lib'; } use strict; use warnings; use Graph::Easy::Parser; my $parser = Graph::Easy::Parser->new( debug => 0); my ($name, $template, $sep, @dirs) = @ARGV; $name = 'Graph::Easy Test page' unless $name; $template = 'syntax.tpl' unless $template; my @toc = (); open FILE, $template or die ("Cannot read 'syntax.tpl': $!"); local $/ = undef; my $html = ; close FILE; my $output = ''; my $ID = '0'; # generate the parts and push their names into @toc gen_graphs($parser, @dirs); my $toc = '\n"; # insert the TOC $html =~ s/##TOC##/ $toc /; $html =~ s/##NAME##/ $name /; $html =~ s/##HTML##/ $output /; $html =~ s/##time##/ scalar localtime() /eg; $html =~ s/##version##/$Graph::Easy::VERSION/eg; print $html; # all done; 1; ############################################################################# sub gen_graphs { # for all files in a dir, generate a graph from it my $parser = shift; @dirs = qw/syntax stress/ unless @dirs; foreach my $dir (@dirs) { _for_all_files($parser, $dir); } } sub _for_all_files { my ($parser, $dir) = @_; opendir DIR, "../t/$dir" or die ("Cannot read dir '../t/$dir': $!"); my @files = readdir DIR; closedir DIR; foreach my $file (sort @files) { my $f = "../t/$dir/" . $file; next unless -f $f; # not a file? print STDERR "# at file $f\n"; open FILE, "$f" or die ("Cannot read '$f': $!"); local $/ = undef; my $input = ; close FILE; my $graph = $parser->from_text( $input ); if (!defined $graph) { my $error = $parser->error(); $output .= "

$dir/$file

" . "Top -^\n". "
\n". "Error: Could not parse input from $file: $error". "
Input was:\n" . "
$input
\n". "
\n"; next; } $graph->timeout(100); $graph->layout(); if ($graph->error()) { my $error = $graph->error(); $output .= "

$dir/$file

" . "Top -^\n". "
\n". "Error: $error". "
Input was:\n" . "
$input
\n". "
\n"; next; } $output .= out ($input, $graph, 'html', $dir, $file); } } sub out { my ($txt,$graph,$method,$dir, $file) = @_; $method = 'as_' . $method; # set unique ID for CSS $graph->id($ID++); my $t = $graph->nodes() . ' Nodes, ' . $graph->edges . ' Edges'; my $n = $dir."_$file"; $dir = ucfirst($dir); # get comment $txt =~ /^\s*#\s*(.*)/; my $comment = ucfirst($1 || ''); my $link; $link = $1 if $txt =~ /\n#\s*(http.*)/; my $name = $comment || $t; push @toc, [ $n, $name ]; my $out = "\n"; if (!$sep) { $out .= "

$dir: $name

\n" . "Top -^\n". "
\n"; $out .= "Error: " . $graph->error() if $graph->error(); my $input = "
\n" . "

Input

\n" . "
$txt
\n
" . "
\n" . "

As Text

\n" . "
" . $graph->as_txt() . "
\n
"; $out .= $input . "
\n" . "

As HTML:

\n" . $graph->$method() . "\n
\n"; $out .= "
 
\n\n"; } else { $out .= "

$name

\n"; $out .= "Top -^\n"; $out .= "Source\n" if $link; $out .= "Error: " . $graph->error() if $graph->error(); $out .= $graph->$method() . "\n" . "
\n\n"; # write out the input/text } $out; }