#!/usr/bin/perl -Tw

BEGIN
  {
  $|++;				# output buffer off
  unshift @INC, 'lib';		# use local modules first
  }

$VERSION = '0.12';

use strict;
# use warnings;			# be lean
use Graph::Easy 0.30;
use Graph::Easy::Parser;

my $timeout = 6; 		# set hard timeout
my $debug = 0;			# set to 1 to see some debug output
 
# wrong number of options?
if (@ARGV < 2 || @ARGV > 3)
  {
  require Pod::Usage;		# do not load this unless nec.
  Pod::Usage::pod2usage(-2);	# print help and exit
  }

eval
  {
  local $SIG{ALRM} = sub { die "graphcnv took more than $timeout seconds to parse and layout graph\n" };
  alarm $timeout;

  my $parser = Graph::Easy::Parser->new();

  my ($txt, $encoding, $output) = @ARGV;
  if ($debug)
    { 
    open FILE, ">logfile.log" or die ("Cannot open logfile.log: $1");
    print "<!-- Arguments:\n '", join ("', '", $txt, $encoding), "' -->\n";
    }

  my $graph = $parser->from_text($txt);		# create a graph object

  # the "\n" prevents double line nr double line nr reports
  die ($parser->error()."\n") if $parser->error();

  # if user specified output like "graph { output: ascii; }", use it
  $output = $graph->attribute('graph', 'output') unless $output;
  # fallback to html output
  $output = 'html' unless $output;
  die ("Invalid output format '$output'\n") if $output !~ /^(html|ascii|svg)$/i;

  if ($output =~ /html/i)
    {
    my $css = "<style type='text/css'><!--\n" . $graph->css() . "--></style>";
    my $html = $graph->as_html();

    $html =~ s/\n+\z//;				# remove trailing "\n"

    # mediawiki doesn't like leading spaces and empty lines in the CSS
    $css =~ s/(^|\n)\s+/$1/g;			# spaces at front

    if ($debug)
      {
      print FILE $css, $html;
      close FILE;
      }

    print $css, $html;
    }
  elsif ($output =~ /ascii/)
    {
    # output as ASCII, suitable for embedding into HTML
    print $graph->as_ascii_html();
    }
  else
    {
    # output as SVG, suitable for embedding into HTML
    my $svg = $graph->as_svg();
   
    # mediawiki doesn't like leading spaces and empty lines in the SVG
    $svg =~ s/(^|\n)\s+/$1/g;			# spaces at front
    $svg =~ s/\n{2,}/\n/g;			# empty lines
    print $svg;
    }

  # disable alarm
  alarm 0;
  };

if ($@) 
  {
  # propagate unexpected errors
  print "<strong class='error'>graphcnv error: $@</strong>\n";
  }

1;

__END__

=pod

=head1 NAME

graphcnv - convert textual graph description to ASCII/HTML

=head1 SYNOPSIS

	perl -T graphcnv graph-as-text encoding [outputtype]

Examples:

	perl -T graphcnv '[ Bonn ] --> [ Berlin ]' 'utf-8'
	perl -T graphcnv '[Bonn] --> [Berlin]' 'utf-8' 'ascii'

=head1 DESCRIPTION

Turns a given textual graph representation into a pretty graph. Uses
L<Graph::Easy> behind the scenes to do all the hard work.

=head1 VERSIONS

Please see the CHANGES file for a complete version history.

=head1 LICENSE

This library is free software; you can redistribute it and/or modify
it under the same terms of the GPL version2.

See the LICENSE file for information.

=head1 AUTHOR

(c) by Tels bloodgate.com 2005

=head1 SEE ALSO

<L<Graph::Easy>, L<http://bloodgate.com/perl/graph>.

=cut
