Skip to content

Instantly share code, notes, and snippets.

@cinsk
Created November 1, 2013 11:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cinsk/7264302 to your computer and use it in GitHub Desktop.
Save cinsk/7264302 to your computer and use it in GitHub Desktop.
Generate E-book friendly html files from http://www.artima.com/pins1ed/ See the comment in the source.
#!/usr/bin/env ruby
require 'rubygems'
require 'fileutils'
#
# Generate HTMLs for e-book from HTML files of "Programming in Scala".
#
# Usage:
#
# $ chmod +x ./program-in-scala.rb
# $ wget -k -r -np http://www.artima.com/pins1ed/
# $ ./program-in-scala.rb www.artima.com/pins1ed html
# $ cp -a www.artima/pins1ed/images html/
#
# Then, use Calibre to generate e-book from html/indexP.html
#
OUTDIR="html"
unless ARGV.length == 2
$stderr.write "Usage: #{$PROGRAM_NAME} SRC-DIR DST-DIR\n"
exit 1
end
$SRCDIR = ARGV[0]
$OUTDIR = ARGV[1]
begin
FileUtils.mkdir_p($OUTDIR)
rescue StandardError => e
end
def convert_link line
line.gsub(/href="(.*)\.html/, "href=\"\\1P.html")
end
Dir.glob(File.join($SRCDIR, '*P.html')) do |src|
in_header = false
in_footnote = false
File.open(File.join(OUTDIR, File.basename(src)), 'w') do |outf|
File.open(src, 'r').each do |line|
if line =~ /^<body>/
in_header = true
outf.write "<body>\n"
else
if in_header
if line =~ /^<BR>/
in_header = false
end
else
if line =~ /^<center>/
in_footnote = true
else
if in_footnote
if line =~ /^<\/body>/
in_footnote = false
outf.write "</div>\n</body>"
end
else
outf.write convert_link(line)
end
end
end
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment