<%ARGS>
$i => undef
@Format => undef
$record => undef
$maxitems => undef
$Depth => undef
$Warning => undef
$TrailingNewline => 1
</%ARGS>

<%PERL>
my $item;
foreach my $column (@Format) {
    if ( $column->{title} eq 'NEWLINE' ) {
	#while ($item < $maxitems) {
	    #$m->out('"');
	    #$item++;
	#}
	#$item = 0;
        #$m->out("\"\t");
        #$m->flush_buffer();
        $m->out( "\n" );
        next;
    }
    $item++;
    #$m->out('<td class="collection-as-table">');
    foreach my $subcol ( @{ $column->{output} } ) {
        if ( $subcol =~ /^__(.*?)__$/o ) {
            my $col = $1;
            my $value = $m->comp('/AssetTracker/Elements/RTx__Asset/ColumnMap', Name => $col, Attr => 'value');

            if ( $value && ref($value)) {
		# All HTML snippets are returned by the callback function
		# as scalar references.  Data fetched from the objects are
		# plain scalars, and needs to be escaped properly.
#$RT::Logger->crit("Column: $col Values: " . join(':',(&{ $value } ( $record, $i ))));
		$m->out( 
		    #map { ref($_) ? $$_ : $m->interp->apply_escapes( $_ => 'h' ) }
		    map { my $out = ref($_) ? $$_ : $_; $out =~ s/\t/ \[TAB\] /g; $out}
		    &{ $value } ( $record, $i )
	        );
            } else {
		# Simple value; just escape it.
                #$m->out( $m->interp->apply_escapes( $value => 'h' ) );
                $value =~ s/\t/ \[TAB\] /g;
                $m->out( $value );
            }
        }
        else {
            my $value = Encode::decode_utf8($subcol);
            $value =~ s/\t/ \[TAB\] /g;
            $m->out( $value );
        }
    }
    $m->out("\t");
}
#$m->out("\n");
</%PERL>
<%FILTER>
# replace line breaks with comma-space unless it is the last one, which will get scrubbed
s/\<br\>([^\t])/, $1/ig;

# no HTML
my $scrubber = HTML::Scrubber->new( deny => [ qw(*) ] );
#$scrubber->comment(0);
$_ = ( $scrubber->scrub($_) );

# The only newline should be at the end of the line
s/\n//g;
$_ .= "\n" if $TrailingNewline;
</%FILTER>
