# $Id: Cleaneval.pm 356 2008-02-03 16:46:25Z michal $

package Victor::Output::Cleaneval;

use strict;
use warnings;
use open ':locale';

use Exporter;
use vars qw(@ISA @EXPORT);
@ISA = qw(Exporter);
@EXPORT = qw(&output_cleaneval);

sub output_cleaneval {
	my ($opt, $blocks) = @_;
	my @close;
	my $out;
	if (!$opt->{output}) {
		$out = \*STDOUT;
	} elsif (ref($opt->{output}) eq "GLOB") {
		$out = $opt->{output};
	} else {
		open($out, ">", $opt->{output}) || die "can't open $opt->{output}: $!\n";
		push(@close, $out);
	}

	if ($opt->{url}) {
		print $out "URL: $opt->{url}\n\n";
	}
	my $n = 0;
	foreach my $block (@$blocks) {
		if (!$block->{class}) {
			next;
		}
		$n++;
		if ($block->{class} eq "header") {
			print $out "<h> $block->{text}\n";
		} elsif ($block->{class} eq "paragraph") {
			print $out "<p> $block->{text}\n";
		} elsif ($block->{class} eq "list") {
			print $out "<l> $block->{text}\n";
		} elsif ($block->{class} eq "continuation") {
			print $out "$block->{text}\n";
		} elsif ($block->{class} eq "other") {
			# nop
		} else {
			die "unknown class: $block->{class}\n";
		}
	}
	return $n;
}
