#!/usr/bin/perl -w ## daisyreader.pl for DaisyPlayer in /home/yan/Daisy/ToLove ## ## Made by Yannick PLASSIARD ## Login ## ## Started on Wed Jun 30 10:08:47 2004 Yannick PLASSIARD ## Last update Thu Jul 1 21:48:56 2004 Yannick PLASSIARD ## ## send comments to me. ;-) ## use strict; my $debug = 0; # 1 if you want lots of output my $version = '0.2'; my $mode; # refers to the current viewing mode my $daisydir = '/mnt/cdrom'; my $choice; my $view = 1; my $continue_reading = 0; my %cells; my %chapters; my %pages; my %book; $| = 1; ##### SUBS ##### sub getline($) { my $default = shift; my $res = <>; $res = $default if ($res =~ /^$/); chomp($res); return $res; } ## parse_toc: Parses HTML and XML files to build cells' index. sub parse_toc { my $line = 'Line';; my $id = 1; my $id_page = 1; my $id_chapter = 1; unless (open(NCC, "$daisydir/ncc.html") or open(NCC, "$daisydir/NCC.html")) { print "Error opening the NCC file: $!\n"; print "Please ensure that this is a Daisy-book format.\n"; exit(1); } while (defined($line)) { $line = ; next if (!defined($line)); if ($line =~ /\(.+)\<\/title\>/i) { $book{'title'} = $1; $book{'title'} = "Unspecified" if ($1 =~ /^\s*$/); } if ($line =~ /\/i) { $book{'format'} = $1; $book{'format'} = "Unspecified" if ($1 =~ /^\s*$/); } if ($line =~ /\/i) { $book{'publisher'} = $1; $book{'publisher'} = "Unspecified" if ($book{'publisher'} =~ /^\s*$/); } if ($line =~ /\/i) { $book{'identifier'} = $1; $book{'identifier'} = "Unspecified" if ($book{'identifier'} =~ /^\s*$/); } if ($line =~ /\/i) { $book{'time'} = $1; $book{'time'} = "Not Available" if ($1 =~ /^\s*$/); } if ($line =~ /\/i) { $book{'date'} = $1; $book{'date'} = "Not available" if ($1 =~ /^\s*$/); } if ($line =~ /\/i) { $book{'author'} = $1; $book{'author'} = "Unspecified" if ($1 =~ /^\s*$/); } if ($line =~ /\<[aA]\s[hH][rR][eE][fF]\=\"(.*)\.smil#(.*)\"\>(.*)\<\/[aA]\>/i) { $cells{$id}{'title'} = $3; $cells{$id}{'smil'} = "$1.smil"; $cells{$id}{'part'} = $2; # We also put it in the page / chapter index depending on the title if ($cells{$id}{'title'} =~ /^\d+$/) { $pages{$id_page}{'title'} = $cells{$id}{'title'}; $pages{$id_page}{'smil'} = $cells{$id}{'smil'}; $pages{$id_page}{'part'} = $cells{$id}{'part'}; $id_page++; } else { $chapters{$id_chapter}{'title'} = $cells{$id}{'title'}; $chapters{$id_chapter}{'smil'} = $cells{$id}{'smil'}; $chapters{$id_chapter}{'part'} = $cells{$id}{'part'}; $id_chapter++; } $id++; } } close(NCC); } ## select_chapter: presents a choice of cells and asks the user for one. sub select_chapter() { my $end = 0; my $start = 0; $mode = \%pages if ($view == 0); $mode = \%chapters if ($view == 1); $mode = \%cells if ($view == 2); $continue_reading = 0; print "Chapter List:\n"; for (my $id = 1; defined(${$mode}{$id}); ) { my $i; system("clear"); for ($i = 0; $i < 23 && defined(${$mode}{$id}); $i++) { print "$id: ${$mode}{$id++}{'title'}\n"; } print "Press enter to see next page ..." if ($i == 23); <> if ($i == 23); } print "\aYour choice [default 1] ? "; $choice = getline(1); print "Choice [$choice].\n" if (defined($debug)); $view = 0 if ($choice =~ /^p$/i); $view = 1 if ($choice =~ /^h$/i); $view = 2 if ($choice =~ /^c$/i); if ($choice =~ /^[qQ]$/) { print "Thank you for using DaisyReader!\n"; exit 0; } if ($choice =~ /^\d+$/) { my $quit = 0; for (my $id = 1; $quit != 1; $id++) { $quit = 1 if (!defined($cells{$id})); print "${$mode}{$choice}{'title'} -> $cells{$id}{'title'}\n" if (defined($debug)); if ($cells{$id}{'title'} eq ${$mode}{$choice}{'title'}) { $quit = 1; $start = $id; } } for (my $id = $start + 1, $quit = 0; $quit != 1; $id++) { $quit = 1 if (!defined($cells{$id})); if ($cells{$id}{'title'} eq ${$mode}{$choice + 1}{'title'}) { print "Setting the end to $cells{$id}{'title'} -> ${$mode}{$choice + 1}{'title'}\n" if (defined($debug)); $end = $id; $quit = 1; } } &read_chapter($start, $end); if ($continue_reading == 1) { $start++; while (defined($cells{$start})) { &read_chapter($start); $start++; } } print "Press enter to go back to the list..."; <>; } } ## read_chapter: attempts to read a specified chapter. sub read_chapter($$) { my $id = shift; my $end = shift; my $line = 'Line'; my $choice = undef; if (!defined($cells{$id})) { print "\aERROR: invalid chapter ID.\n"; print "Press enter to continue..."; <>; return; } while (!defined($choice) && $continue_reading == 0) { print "Only read this cell/page/chapter [default yes] ? "; $choice = getline('yes'); $continue_reading = 0 if ($choice =~ /^[Yy]/); $continue_reading = 1 if ($choice =~ /^[Nn]/); $choice = undef if ($choice !~ /^[YnyN]/); } print "Reading chapter $cells{$id}{'title'} ...\n"; $end = $id + 1 if (!defined($end) || $end == 0); print "Playing from $id to $end.\n"; for (my $i = $id; $i < $end; $i++) { # print "$i\n"; my $next_cell = 0; my $smilfile = $daisydir . '/' . $cells{$i}{'smil'}; unless(open(SMIL, $smilfile)) { print "\aCannot open chapter-specific file $daisydir/$cells{$i}{'smil'}...\n"; print "Press enter to continue..."; <>; return ; } $line = 'Line'; while (defined($line)) { $line = ; next if (!defined($line)); if ($line =~ /.*id=\"(.*)\"/) { # print "Found ID $1 in $smilfile\n"; if ($1 eq $cells{$i}{'part'}) { # print "Found ID $1\n"; $line = while ($line !~ /.*\; return; } while ($line =~ /\/) { my $start = $2; my $file = $1; my $end = $3; $start =~ s/s$//g; $end =~ s/s$//g; my $duration = sprintf "%.3f", $end - $start; print "Playing $file from $start to $end ($duration) ...\n"; $next_cell = 1; system("madplay -v --start=$start -t $duration $daisydir/$file") == 0 or return; $line = ; } } } } close(SMIL); if ($next_cell == 0) { print "Chapter's ID $cells{$id}{'title'} was not found in any SMIL file.\n"; print "Press enter to continue..."; <>; } $next_cell = 0; } } ## MAIN PART print "DaisyReader.pl version $version starting.\n"; print "Copyright (C) 2004 Yannick PLASSIARD .\n"; print "This program is FREE SOFTWARE. Please see the COPYING file included with this\n"; print "program to know what FREE SOFTWARE means.\n"; print "\nPlease enter the path to your book [$daisydir] ? "; $choice = &getline($daisydir); # Just check if the directory is valid. opendir(DIR, $choice) || die"Cannot open Daisy Directory $choice: $!\nExiting.\n"; closedir(DIR); # Scanning the table of contents opening the ncc.html file. print "Scanning table of contents...\n"; $daisydir = $choice; &parse_toc(); print "Book's informations\n----------------------\n"; foreach my $k (keys(%book)) { print "$k : $book{$k}\n"; } print "Pess enter to continue ..."; <>; while (1) { &select_chapter(); }