<?xml version="1.0"?>
<%

my %stylemap = (
    Text_body => 'para',
    Subtitle => 'subtitle',
    Source_Code => 'programlisting',
);

my %head2sect = (
    1 => 'sect1',
    2 => 'sect2',
    3 => 'sect3',
    4 => 'sect4',
    5 => 'sect5',
    6 => 'sect6',
);

use vars qw/$heading_level/;

$heading_level = 0;

my %cache;

sub get_actual_style {
    my ($node) = @_;
    my $autostyle = $node->getAttribute('text:style-name');
    return $cache{$autostyle} if $cache{$autostyle};
    my $actualstyle = findvalue(
    	"translate(/office:document
	/office:automatic-styles
	/style:style[\@style:name='$autostyle']
	/\@style:parent-style-name, ' ', '_')");
    $cache{$autostyle} = $stylemap{$actualstyle} || $actualstyle;
    return $cache{$autostyle};
}

$t->{'text:sequence-decls'}{testcode} = sub { 0; };

# For headings we put in \n before and \n after.
$t->{'text:h'}{testcode} = sub {
    my ($node, $t2) = @_;
    my $actualstyle = get_actual_style($node);
    if ($actualstyle =~ /^Heading_(\d)$/i) {
        my $level = $1;
        
        $t2->{pre} = $t2->{post} = '';
        
        while ($level <= $heading_level) {
            $t2->{pre} .= "</$head2sect{$heading_level}>\n";
            $heading_level--;
        }
        
        while ($level > $heading_level + 1) {
            $heading_level++;
            $t2->{pre} .= "<$head2sect{$heading_level}>\n";
        }
        
        $heading_level = $level;
        $t2->{pre} .= "<$head2sect{$heading_level}>\n<title>";
        $t2->{post} .= "</title>";
        return 1;
    }
    
    # fallback in case that doesn't match!
    $t2->{pre} = "\n<${actualstyle}>";
    $t2->{post} = "</${actualstyle}>\n";
    return 1;
};

# p is same as headings, but without the \n's
$t->{'text:p'}{testcode} = sub {
    my ($node, $t2) = @_;
    my $actualstyle = get_actual_style($node);
    $t2->{pre} = "<${actualstyle}>";
    $t2->{post} = "</${actualstyle}>";
    return 1;
};

# make lists more docbook-like
$t->{'text:ordered-list'}{pre} = '<orderedlist>';
$t->{'text:ordered-list'}{post} = '</orderedlist>';

$t->{'text:unordered-list'}{pre} = '<itemizedlist>';
$t->{'text:unordered-list'}{post} = '</itemizedlist>';

$t->{'text:list-item'}{pre} = '<listitem>';
$t->{'text:list-item'}{post} = '</listitem>';

my %spancache;
# span is same as HTML span. Here we extract fo:* attributes
# and turn them into CSS styles.
$t->{'text:span'}{testcode} = sub {
    my ($node, $t) = @_;
    my $autostyle = $node->getAttribute('text:style-name');

    my $pre = $spancache{$autostyle};
    
    if (!$pre) {
        # get the actual style node
        my ($property) = findnodes(
    	    "/office:document
	    /office:automatic-styles
	    /style:style[\@style:name='$autostyle']
            /style:properties");

        $pre = "<span style=\"";

        # Then loop through all fo:* attributes
        foreach my $fo ($property->findnodes('@fo:*')) {
            my $value = $fo->getValue;
            $value =~ s/Helmet/Arial/; # fix OpenOffice font name
            $pre .= $fo->getLocalName . ': ' . $value . '; ';
        }

        $pre .= '">';
        $spancache{$autostyle} = $pre;
    }
    $t->{pre} = $pre;
    $t->{post} = "</span>";
    return 1;
};

# $t->{'text:line-break'}{pre} = "<br />\n";
$t->{'text:line-break'}{post} = "\n";

$t->{'text:s'}{testcode} = sub {
    my ($node, $t2) = @_;
    if (my $count = $node->getAttribute('text:c')) {
        $t2->{pre} = '&#160;' x $count;
    }
    else {
        $t2->{pre} = '&#160;';
    }
    return 1;
};

%><article>
<artheader xmlns:dc="http://purl.org/dc/elements/1.1/">
<!-- here we are only interested in dublin-core tags,
which we copy verbatim -->
<%
# here we don't use apply_templates only because we get too many 
# namespace declarations if we do, so this creates a simpler version.
# Old version was: 
#  apply_templates('/office:document/office:meta/dc:*')
foreach my $meta (findnodes('/office:document/office:meta/dc:*')) {
    print "<", $meta->getName, ">",
            $meta->string_value, "</", $meta->getName, ">\n";
}
%></artheader>
<%
# Note this is equivalent to apply_templates('...'), 
# but we see the output faster this way.
for my $node (findnodes('/office:document/office:body/*')) {
    print apply_templates($node), "\n";
}
%>
<%
while ($heading_level) {
    print "</$head2sect{$heading_level}>\n";
    $heading_level--;
}
%>
</article>
