#!/usr/bin/perl -Tw

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

$VERSION = '0.14';

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

my $timeout = 6; 		# set hard timeout
 
# 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;

  # We do NOT check the return value, because it will be false for "A", and
  # true for "köln". (why?)

  utf8::decode($txt);

  binmode STDOUT, ':utf8' or die ("binmode STDOUT, ':utf8' failed: $!");

  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|boxart)\z/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

    print $css, $html;
    }
  elsif ($output =~ /boxart/)
    {
    # output as Unicode box drawing, suitable for embedding into HTML
    print $graph->as_boxart_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.

The C<encoding> parameter is currently ignored, the input is expected
to be in C<utf-8>.

=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
