#! /usr/bin/perl -w 

#
# The "produce_slides" script: Given some textual content as produced
# by the "getcontent" script, produce a new Impress presentation file.
#
# By Paul Barry, paul.barry@itcarlow.ie
#

use strict;

use OpenOffice::OODoc;

sub title_slide {
    my $title   = shift;
    my $content = shift;

return<<SLIDE;
<draw:page draw:name="page1" draw:style-name="dp1" draw:id="1" draw:master-page-name="lyt-cool" presentation:presentation-page-layout-name="AL1T0"><draw:text-box presentation:style-name="pr1" draw:text-style-name="P1" draw:layer="layout" svg:width="23.911cm" svg:height="3.508cm" svg:x="2.057cm" svg:y="1.743cm" presentation:class="title"><text:p text:style-name="P1">$title</text:p></draw:text-box><draw:text-box presentation:style-name="pr2" draw:text-style-name="P2" draw:layer="layout" svg:width="23.911cm" svg:height="13.231cm" svg:x="2.057cm" svg:y="5.838cm" presentation:class="subtitle"><text:p text:style-name="P2">$content</text:p></draw:text-box><presentation:notes><draw:page-thumbnail draw:style-name="gr1" draw:layer="layout" svg:width="13.697cm" svg:height="10.28cm" svg:x="3.645cm" svg:y="2.853cm" draw:page-number="1" presentation:class="page"/><draw:text-box presentation:style-name="pr3" draw:text-style-name="P2" draw:layer="layout" svg:width="14.509cm" svg:height="11.411cm" svg:x="3.248cm" svg:y="14.13cm" presentation:class="notes" presentation:placeholder="true"/></presentation:notes></draw:page>
SLIDE
}

sub sourcecode_slide {
    my $title   = shift;
    my @content = @_;

    my $xml =<<SLIDESTART;
<draw:page draw:name="page3" draw:style-name="dp1" draw:id="3" draw:master-page-name="lyt-cool" presentation:presentation-page-layout-name="AL2T1"><draw:text-box presentation:style-name="pr1" draw:text-style-name="P2" draw:layer="layout" svg:width="23.911cm" svg:height="3.508cm" svg:x="2.057cm" svg:y="1.743cm" presentation:class="title"><text:p text:style-name="P1">$title</text:p></draw:text-box><draw:text-box presentation:style-name="pr4" draw:text-style-name="P4" draw:layer="layout" svg:width="23.911cm" svg:height="13.231cm" svg:x="2.057cm" svg:y="5.838cm" presentation:class="outline"><text:unordered-list text:style-name="L3">
SLIDESTART

    chomp $xml;

    foreach my $line ( @content )
    {
        my $snippet =<<CODE;
<text:list-item><text:p text:style-name="P7"><text:span text:style-name="T3">$line</text:span></text:p></text:list-item>
CODE
        chomp $snippet;

        $xml = $xml . $snippet;
    }

    $xml = $xml . <<SLIDEFINISH;
</text:unordered-list></draw:text-box><presentation:notes><draw:page-thumbnail draw:style-name="gr1" draw:layer="layout" svg:width="13.697cm" svg:height="10.28cm" svg:x="3.645cm" svg:y="2.853cm" draw:page-number="3" presentation:class="page"/><draw:text-box presentation:style-name="pr5" draw:text-style-name="P1" draw:layer="layout" svg:width="14.509cm" svg:height="11.411cm" svg:x="3.248cm" svg:y="14.13cm" presentation:class="notes" presentation:placeholder="true"/></presentation:notes></draw:page>
SLIDEFINISH

    return $xml;
}

sub bullet_slide {
    my $title   = shift;
    my @bullets = @_;

    my $xml =<<BULLETSBEGIN;
<draw:page draw:name="page2" draw:style-name="dp1" draw:id="2" draw:master-page-name="lyt-cool" presentation:presentation-page-layout-name="AL2T1"><draw:text-box presentation:style-name="pr1" draw:text-style-name="P1" draw:layer="layout" svg:width="23.911cm" svg:height="3.508cm" svg:x="2.057cm" svg:y="1.743cm" presentation:class="title"><text:p text:style-name="P1">$title</text:p></draw:text-box><draw:text-box presentation:style-name="pr4" draw:text-style-name="P3" draw:layer="layout" svg:width="23.911cm" svg:height="13.231cm" svg:x="2.057cm" svg:y="5.838cm" presentation:class="outline"><text:unordered-list text:style-name="L3">
BULLETSBEGIN

    chomp $xml;

    foreach my $bullet ( @bullets )
    {
        my $snippet =<<BULLET;
<text:list-item><text:p text:style-name="P4"><text:span text:style-name="T1">$bullet</text:span></text:p></text:list-item>
BULLET
        chomp $snippet;

        $xml = $xml . $snippet;
    }

    $xml = $xml . <<BULLETSFINISH;
</text:unordered-list></draw:text-box><presentation:notes><draw:page-thumbnail draw:style-name="gr1" draw:layer="layout" svg:width="13.697cm" svg:height="10.28cm" svg:x="3.645cm" svg:y="2.853cm" draw:page-number="2" presentation:class="page"/><draw:text-box presentation:style-name="pr5" draw:text-style-name="P1" draw:layer="layout" svg:width="14.509cm" svg:height="11.411cm" svg:x="3.248cm" svg:y="14.13cm" presentation:class="notes" presentation:placeholder="true"/></presentation:notes></draw:page>
BULLETSFINISH

    return $xml;
}

my $presentation = OpenOffice::OODoc::Document->new 
    (
        file    => 'blank.sxi',
        member  => 'content'
    );

my $chapter        = shift;

my $last_slide     = 2;   # Set this to one less than number of slides in 
                          # the blank.sxi presentation document.

my $maxcount       = 0;

my $title_title    = '';
my $title_content  = '';
my $code_title     = '';
my @code_content   = ( '' );
my $bullet_title   = '';
my @bullet_content = ( '' );

while ( <> )
{
    if ( /CHAPTERTITLE: (.*)/ )
    {
        $title_title = $1;
        chomp $title_title;

        next;
    }
    if ( /CHAPTERCONTENT: (.*)/ )
    {
        $title_content = $1;
        chomp $title_content;

        $presentation->insertElement( '//draw:page', 
                           $last_slide++, 
                           title_slide( $title_title, $title_content ),
                           position => 'after' );
        $title_title    = '';
        $title_content  = '';

        next;
    }

    if ( /GRAPHICCAPTION: (.*)/ )
    {
        $title_title = $1;
        chomp $title_title;

        next;
    }
    if ( /GRAPHICNAME: (.*)/ )
    {
        $title_content = $1;
        chomp $title_content;

        $presentation->insertElement( '//draw:page', 
                           $last_slide++, 
                           title_slide( $title_title, $title_content ),
                           position => 'after' );
        $title_title    = '';
        $title_content  = '';

        next;
    }

    if ( /BULLETTITLE: (.*)/ )
    {
        $bullet_title = $1;
        chomp $bullet_title;

        while ( <> )
        {
            if ( /BULLETCONTENT: (.*)/ )
            {
                my $bullet = $1;

                chomp $bullet;
                push @bullet_content, $bullet;
            }
            else
            {
                last;
            }
        }

        $presentation->insertElement( '//draw:page', 
                           $last_slide++, 
                           bullet_slide( $bullet_title, @bullet_content ),
                           position => 'after' );
        $bullet_title   = '';
        @bullet_content = ( '' );

        last if eof;
        redo;
    }

    if ( /STARTCODE/ )
    {
        while ( <> )
        {
            if ( /STOPCODE/ )
            {
                last;
            }

            chomp;
            push @code_content, $_;
        }

        $presentation->insertElement( '//draw:page', 
                           $last_slide++, 
                           sourcecode_slide( $code_title, @code_content ),
                           position => 'after' );
        $code_title     = '';
        @code_content   = ( '' );

    }

    if ( /STARTMAXIM/ )
    {
        my $maxim = <>;  # Get the maxim on next line.

        chomp $maxim;
 
        $maxcount++;

        $presentation->insertElement( '//draw:page', 
                           $last_slide++, 
                           title_slide( "Maxim $chapter.$maxcount", $maxim ),
                           position => 'after' );
        $title_title    = '';
        $title_content  = '';

        next;
    }
}

$presentation->save( 'chapter' . $chapter . '.sxi' );


