Xu Xiaodong avatar Xu Xiaodong committed 789ee1f

initial commit

Comments (0)

Files changed (2)

+Convert HTML to Mobi to read on Kindle.
+#!/usr/bin/env perl
+#
+# name:      convert html to mobi
+# author:    Xu Xiaodong <xxdlhy@gmail.com>
+# license:   GPL
+# created:   2011 Mar  5
+# modified:  2011 Apr 13
+#
+
+use Modern::Perl;
+use Tie::File;
+use Getopt::Long;
+use autodie;
+
+my ( $start, $end, $title, $author, $help ) = ( 0, 0, '', '', 0 );
+
+GetOptions(
+    's|start=i'  => \$start,
+    'e|end=i'    => \$end,
+    't|title=s'  => \$title,
+    'a|author=s' => \$author,
+    'h|help'     => \$help,
+);
+
+if ($help) {
+    ( my $me = $0 ) =~ s!.*/!!g;
+    say <<"USAGE";
+
+    Usage: $me -s <start> -e <end> -t <title> -a <author> -h
+
+    -s <start>  Trim start lines of HTML
+    -e <end>    Trim end lines of HTML
+    -t <title>  Specify title
+    -a <author> Specify author
+    -h          Display help
+USAGE
+}
+
+if ( $start and $end ) {
+    trim_html($start, $end);
+}
+
+if ($title) {
+    cat_html($title);
+    make_mobi($title);
+
+    add_mobi_author($title, $author) if $author;
+}
+
+# Trim HTML -- trim_html $start, $end
+sub trim_html {
+    my ( $start, $end ) = @_;
+
+    opendir my ($dir_fh), '.';
+    my @files = grep {/\.html?/} readdir $dir_fh;
+
+    for my $file (@files) {
+        tie my @lines, 'Tie::File', $file;
+
+        @lines = @lines[ $start .. ( @lines - ( $end + 1 ) ) ];
+
+        for (@lines) {
+            s!
+            s!bgcolor="black"\s!!;
+            s!\A<UL>(<p)!$1!;
+
+            #s!\A<div class="htmltoc">.*\Z!!;
+            #s!\A<a href.*</a><br />(?:</p></div>)?!!;
+            s!\A(<blockquote>)<table.*?><tr><td>!$1!;
+            s!\A</td></tr></table><p><p>!!;
+            s!\A<hr.*\Z!!;
+
+            s!¶!!;
+        }
+
+        untie @lines;
+    }
+}
+
+# Cat HTML -- cat_html $title
+sub cat_html {
+    my $title    = shift;
+    my $filename = $title . '.html';
+
+    open my ($in_fh), '<', 'filenames';
+    my @names = <$in_fh>;
+
+    for (@names) {
+        chomp;
+        system "cat $_ >> \Q$filename\E";
+    }
+
+    close $in_fh;
+
+    tie my @lines, 'Tie::File', $filename;
+    my $html_header
+        = "<html>\n<head>\n<title>$title</title>\n</head>\n<body>";
+    my $html_footer = "</body>\n</html>";
+
+    unshift @lines, $html_header;
+    push @lines, $html_footer;
+
+    untie @lines;
+
+    system "tidy -m -asxhtml \Q$filename\E";
+}
+
+# Make MOBI -- make_mobi $title
+sub make_mobi {
+    my $title     = shift;
+    my $html_file = $title . '.html';
+
+    system "kindlegen -c2 -unicode \Q$html_file\E";
+}
+
+# Add author to MOBI -- add_mobi_author $title, $author
+sub add_mobi_author {
+    my ( $title, $author ) = @_;
+    my $mobi_file = $title . '.mobi';
+
+    system "set_mobi_author.pl \Q$author\E \Q$mobi_file\E";
+}
+
+__END__
+
+=pod
+
+=head1 NAME
+
+html2mobi.pl - Convert HTML to MOBI format
+
+=head1 SYNOPSIS
+
+	html2mobi.pl -s <start> -e <end> -t <title> -a <author> -h
+	
+	-s start    Trim start lines of HTML
+	-e end      Trim end lines of HTML
+	-t title    Specify title
+	-a author   Specify author
+	-h          Display help
+
+=head1 AUTHOR
+
+Xu Xiaodong <xxdlhy@gmail.com>
+
+=head1 COPYRIGHT AND LICENSE
+
+This software is copyright (c) 2011 by Xu Xiaodong.
+
+This program is free software; you can redistribute it and/or
+modify it under the terms of the GNU General Public License
+as published by the Free Software Foundation; either version 2
+of the License, or (at your option) any later version.
+
+=cut
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.