Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

initial checkin.

  • Loading branch information...
commit 5e9f53953b7c99f2415d52ef49be850528fb6546 0 parents
@agentzh authored
Showing with 172 additions and 0 deletions.
  1. +2 −0  .gitignore
  2. +32 −0 README
  3. +24 −0 samples/genmobi.sh
  4. +114 −0 src2html.pl
2  .gitignore
@@ -0,0 +1,2 @@
+*~
+*.swp
32 README
@@ -0,0 +1,32 @@
+This project provides utilities to help generating .mobi ebook files
+directly from arbitrary source trees.
+
+The src2html.pl script can generate an HTML tree from the source tree that you specify, for example:
+
+ perl src2html.pl /path/to/my/src/tree/ 'Your Book Title'
+
+And then you can generate a mobi file using Calibre
+( http://calibre-ebook.com/ ):
+
+ ebook-convert /path/to/my/src/tree/index.html my-src.mobi \
+ --output-profile kindle_dx --no-inline-toc \
+ --title "Your Book Title" --publisher 'Your Name' \
+ --language en --authors 'Your Author Name'
+
+The ebook-convert utility is provided by Calibre, see its online
+documentation for full usage:
+
+ http://calibre-ebook.com/user_manual/cli/ebook-convert.html
+
+Well you need both perl and python ;)
+
+Currently only the following files will be searched by
+src2html.pl according to their file extensions:
+
+ .c .cpp .h
+ .tt .js .pl .php .t .pod .xml .conf .pm6
+ .lzsql .lzapi .grammar .lua .java .sql
+
+You can edit the related regex in the Perl source of the src2html.pl
+to add or remove extensions that it will recognize.
+
24 samples/genmobi.sh
@@ -0,0 +1,24 @@
+#!/bin/bash
+
+# this sample script is to generate three .mobi
+# ebooks from the nginx source tree.
+# just put this script in the root directory of
+# your nginx source tree, and ensure both the src2html.pl
+# script (from src2kindle) and the ebook-convert utility
+# (from Calibre) are in your PATH.
+
+name=`pwd|perl -e '$d=<>;$d=~s{.*/}{}g;print $d'`
+
+echo "Generating HTMLs for $name..."
+src2html.pl src $name
+
+echo "Generating .mobi file for $name..."
+rm -rf *.mobi
+for dir in core event http; do
+ ebook-convert src/$dir/index.html $name-$dir.mobi \
+ --output-profile kindle_dx --no-inline-toc \
+ --title "$name $dir" --publisher agentzh \
+ --language en --authors 'Igor Sysoev'
+done
+cp -uv *.mobi ~/mobi/
+
114 src2html.pl
@@ -0,0 +1,114 @@
+#!/usr/bin/env perl
+
+#use Smart::Comments::JSON '##';
+use strict;
+use warnings;
+
+sub process_dir ($);
+sub write_src_html ($$);
+sub write_index ($$);
+
+my $dir = shift or die "No source directory specified.\n";
+my $pkg_name = shift or die "No pkg name specified.\n";
+process_dir($dir);
+
+sub process_dir ($) {
+ my $dir = shift;
+ opendir my $dh, $dir or die "Can't open $dir for reading: $!\n";
+ my @items;
+ while (my $entity = readdir($dh)) {
+ # entity: $entity
+ if (-f "$dir/$entity" && !-l "$dir/$entity" && ($entity =~ /\.(?:c(?:pp)?|h|tt|js|pl|php|t|pod|xml|conf|pm6?|lzsql|lzapi|grammar|lua|java|sql)$/ || $entity eq 'README')) {
+ ## file: $entity
+ write_src_html($dir, $entity);
+ push @items, [file => $entity];
+ } elsif (-d "$dir/$entity" && $entity !~ /^\./) {
+ ## dir: $entity
+ my $count = process_dir("$dir/$entity");
+ if ($count) {
+ push @items, [dir => $entity];
+ }
+ }
+ }
+ close $dh;
+ if (@items) {
+ write_index($dir, \@items);
+ }
+ return scalar @items;
+}
+
+sub write_src_html ($$) {
+ my ($dir, $entity) = @_;
+
+ my $infile = "$dir/$entity";
+ #my $src;
+ open my $in, $infile or
+ die "Can't open $infile for reading: $!\n";
+ local $/;
+ my $src = <$in>;
+ close $in;
+
+ if ($src =~ /PRETEND TO BE IN Parse::RecDescent NAMESPACE/s) {
+ $src = 'Omitted parser file generated automatically by Parse::RecDescent';
+ } else {
+ $src =~ s/\n\n\n+/\n\n/gs;
+ $src =~ s/[ \t]+\n/\n/gs;
+ $src =~ s/\t/ /gs;
+ $src =~ s/\&/\&amp;/g;
+ $src =~ s/ /&nbsp;/gs;
+ $src =~ s/</\&lt;/g;
+ $src =~ s/>/\&gt;/g;
+ $src =~ s/"/\&quot;/g;
+ $src =~ s/\n/<br\/>/g;
+ }
+
+ my $outfile = "$dir/$entity.html";
+ open my $out, ">$outfile" or
+ die "Can't open $outfile for writing: $!\n";
+ print $out <<_EOC_;
+<html>
+ <head>
+ <title>$infile - $pkg_name</title>
+ </head>
+ <body>
+ <h3>$infile - $pkg_name</h3>
+ <code>$src</code>
+ </body>
+</html>
+_EOC_
+ close $out;
+ warn "Wrote $outfile\n";
+}
+
+sub write_index ($$) {
+ my ($dir, $ritems) = @_;
+ my $outfile = "$dir/index.html";
+ open my $out, ">$outfile" or
+ die "Can't open $outfile for writing: $!\n";
+
+ print $out <<_EOC_;
+<html>
+<head>
+ <title>$dir/ - $pkg_name</title>
+</head>
+<body>
+ <h3>$dir/ - $pkg_name</h3>
+ <ul>
+_EOC_
+ for my $item (sort { $a->[1] cmp $b->[1] } @$ritems) {
+ my ($type, $entity) = @$item;
+ if ($type eq 'file') {
+ print $out qq{ <li><a href="$entity.html">$entity</a></li>\n};
+ } else {
+ print $out qq{ <li><a href="$entity/index.html">$entity/</a></li>\n};
+ }
+ }
+ print $out <<_EOC_;
+ </ul>
+</body>
+</html>
+_EOC_
+ close $out;
+ warn "Wrote $outfile\n";
+}
+
Please sign in to comment.
Something went wrong with that request. Please try again.