Permalink
Browse files

numbering

  • Loading branch information...
1 parent 61db707 commit 46e88bb53bdf1b140bda84496d66e6301f89aefd @dann committed May 8, 2010
Showing with 41 additions and 16 deletions.
  1. +28 −13 lib/App/kindlegen.pm
  2. +13 −3 plugins/{w3c → rdfxmlsyntax}
View
@@ -117,17 +117,33 @@ sub _generate_mobi {
sub generate_toc {
my ( $self, $html_url ) = @_;
+ print "Generating TOC ...\n";
my $toc_items
= $self->run_hooks( generate_toc => { html_url => $html_url } );
my $toc = {};
- $toc->{items} = $toc_items;
+ my $new_toc_items = $self->numbering_toc_items($toc_items);
+ $toc->{items} = $new_toc_items;
$toc->{html} = Digest::MD5::md5_hex($html_url) . ".html";
my $toc_content = $self->render_template( 'toc', { toc => $toc } );
+
my $toc_file_path = $self->toc_file_path($html_url);
$self->write_file( $toc_content, $toc_file_path );
$toc_file_path;
}
+sub numbering_toc_items {
+ my ($self, $toc_items) = @_;
+ die 'toc_items is required' unless $toc_items;
+ my @new_toc_items =();
+ my $counter = 1;
+ foreach my $item (@{$toc_items||[]}) {
+ $item->{num} = $counter;
+ $counter++;
+ push @new_toc_items, $item;
+ }
+ return \@new_toc_items;
+}
+
sub toc_file_path {
my ( $self, $url ) = @_;
my $file_path = File::Spec->catfile( $self->{download_dir},
@@ -137,6 +153,7 @@ sub toc_file_path {
sub generate_opf {
my $self = shift;
+ print "Generating OPF ...\n";
# TODO Implement me!
my $book = $self->run_hooks(
@@ -149,15 +166,13 @@ sub generate_opf {
$book->{date} ||= '2010';
$book->{creator} ||= 'dann';
$book->{description} ||= 'description';
- $book->{toc} ||= 'TOC';
- $book->{html} = Digest::MD5::md5_hex( $self->{url} ) . ".html";
- $book->{ncx} = Digest::MD5::md5_hex( $self->{url} ) . ".ncx";
+ $book->{toc} ||= 'toc';
+ $book->{start_page} ||= 'Introduction';
- # FIXME
- $book->{start_page} ||= 'Introduction';
+ $book->{html} = Digest::MD5::md5_hex( $self->{url} ) . ".html";
+ $book->{ncx} = Digest::MD5::md5_hex( $self->{url} ) . ".ncx";
my $opf_content = $self->render_template( 'opf', { book => $book } );
- warn $opf_content;
my $opf_file_path = $self->opf_file_path($html_url);
$self->write_file( $opf_content, $opf_file_path );
@@ -238,15 +253,15 @@ sub download_image {
sub get_images_and_fix_image_tags {
my ( $self, $html, $html_url ) = @_;
- my $tree = HTML::TreeBuilder->new_from_content($html);
- my @imges = $tree->find("img");
+ my $root = HTML::TreeBuilder->new_from_content($html);
+ my @imges = $root->find("img");
foreach my $img (@imges) {
my $image_url = URI->new_abs( $img->attr('src'), $html_url );
my $image_file_path = $self->download_image($image_url);
my $image_file_name = $self->image_file_name($image_url);
$img->attr( "src", $image_file_name );
}
- $tree->as_HTML;
+ $root->as_HTML;
}
sub load_plugins {
@@ -453,9 +468,9 @@ __DATA__
<text>BOOK</text>
</docTitle>
<navMap>
-? for my $item (@{$toc->{items}}) {
- <navPoint id="navPoint-1" playOrder="1">
- <navLabel><text><?= $item->{text} ?></text></navLabel><content src="<?= $toc->{html} ?><?= $item->{anchor}?>"/>
+? for my $item (@{$toc->{items}|| []}) {
+ <navPoint id="navPoint-<?= $item->{num} ?>" playOrder="<?= $item->{num} ?>">
+ <navLabel><text><?= $item->{text} ?></text></navLabel><content src="<?= $toc->{html} ?><?= $item->{anchor} ?>"/>
</navPoint>
? }
</navMap>
@@ -1,5 +1,5 @@
-name 'w3c';
-description 'w3c specs toc';
+name 'websocket api';
+description 'websocket api';
author 'dann';
use Web::Scraper;
@@ -8,7 +8,7 @@ use URI;
hook generate_toc => 0, sub {
my $args = shift;
my $target_url = $args->{html_url};
- return unless $target_url =~ /www.w3.org\/TR\//;
+ return [] unless $target_url =~ /www\.w3\.org\/TR\/REC-rdf-syntax/;
my $scraper = scraper {
process 'a', 'tocitems[]' => '@href';
@@ -21,6 +21,7 @@ hook generate_toc => 0, sub {
my $toc_anchor = $item->as_string;
if($toc_anchor =~ m/(#section-)(.*$)/) {
my $toc_item ={};
+ warn $2;
$toc_item->{text} = $2;
$toc_item->{anchor} = $1 . $2;
push @toc, $toc_item;
@@ -30,4 +31,13 @@ hook generate_toc => 0, sub {
return \@toc;
};
+hook generate_bookinfo => 0, sub {
+ my $args = shift;
+ my $target_url = $args->{html_url};
+ return {} unless $target_url =~ /www\.w3\.org\/TR\/REC-rdf-syntax/;
+
+ my $book = {};
+ return $book;
+};
+

0 comments on commit 46e88bb

Please sign in to comment.