Skip to content

Instantly share code, notes, and snippets.

Created December 28, 2012 23:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save anonymous/4403186 to your computer and use it in GitHub Desktop.
Save anonymous/4403186 to your computer and use it in GitHub Desktop.
HPMoR epub e-book builder. It requires Nokogiri & gepub gems.
#!/usr/bin/env bash
# This is an RVM Project .rvmrc file, used to automatically load the ruby
# development environment upon cd'ing into the directory
# First we specify our desired <ruby>[@<gemset>], the @gemset name is optional,
# Only full ruby name is supported here, for short names use:
# echo "rvm use rbx" > .rvmrc
environment_id="rbx-head@hpmor"
# Uncomment the following lines if you want to verify rvm version per project
# rvmrc_rvm_version="1.17.0 (stable)" # 1.10.1 seams as a safe start
# eval "$(echo ${rvm_version}.${rvmrc_rvm_version} | awk -F. '{print "[[ "$1*65536+$2*256+$3" -ge "$4*65536+$5*256+$6" ]]"}' )" || {
# echo "This .rvmrc file requires at least RVM ${rvmrc_rvm_version}, aborting loading."
# return 1
# }
# First we attempt to load the desired environment directly from the environment
# file. This is very fast and efficient compared to running through the entire
# CLI and selector. If you want feedback on which environment was used then
# insert the word 'use' after --create as this triggers verbose mode.
if [[ -d "${rvm_path:-$HOME/.rvm}/environments"
&& -s "${rvm_path:-$HOME/.rvm}/environments/$environment_id" ]]
then
\. "${rvm_path:-$HOME/.rvm}/environments/$environment_id"
[[ -s "${rvm_path:-$HOME/.rvm}/hooks/after_use" ]] &&
\. "${rvm_path:-$HOME/.rvm}/hooks/after_use" || true
if [[ $- == *i* ]] # check for interactive shells
then echo "Using: $(tput setaf 2)$GEM_HOME$(tput sgr0)" # show the user the ruby and gemset they are using in green
else echo "Using: $GEM_HOME" # don't use colors in non-interactive shells
fi
else
# If the environment file has not yet been created, use the RVM CLI to select.
rvm --create use "$environment_id" || {
echo "Failed to create RVM environment '${environment_id}'."
return 1
}
fi
require 'rubygems'
require 'nokogiri'
require 'open-uri'
require 'fileutils'
require 'gepub'
desc "Fetch the book from hpmor.com"
task :fetch do
unless File.exists? 'book-source/images/cover.jpg'
puts "Fetching: cover.jpg"
unless Dir.exists? "book-source/images"
FileUtils.mkdir_p "book-source/images"
end
open 'http://oi55.tinypic.com/hvq9fm.jpg' do |img|
File.open 'book-source/images/cover.jpg', 'w' do |f|
f.write img.read
end
end
end
unless Dir.exists? "book-source/chapters"
FileUtils.mkdir_p "book-source/chapters"
end
doc = Nokogiri::HTML(open('http://www.fanfiction.net/s/5782108'))
doc.css('#chap_select').first.css('option').each do |chapter|
n = chapter['value']
file_name = "book-source/chapters/#{"%03d" % n}.xhtml"
unless File.exists? file_name
title = chapter.text
puts "Fetching: #{title}"
chapter_doc = Nokogiri::HTML(open("http://www.fanfiction.net/s/5782108/#{n}/Harry-Potter-and-the-Methods-of-Rationality"))
content = chapter_doc.css('#storytext').children.map{|node| node.to_xml.strip }.join("\n")
page = <<-HTML.gsub(/^\s{8}/, '').strip
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:epub="http://www.idpf.org/2007/ops"
xml:lang="en-US" lang="en-US">
<head>
<meta charset="utf-8" />
<title>#{title}</title>
</head>
<body>
<article>
<header>
<h1>#{title}</h1>
</header>
#{content}
</article>
</body>
</html>
HTML
File.open file_name, 'w' do |f|
f.write page
end
end
end
end
desc "Package e-pub"
task :epub do
book = GEPUB::Book.new
book.version = 3.0
book.set_primary_identifier 'http://www.fanfiction.net/s/5782108', 'http://www.fanfiction.net/s/5782108', 'URL'
book.language = 'en'
book.add_title 'Harry Potter and the Methods of Rationality', nil, GEPUB::TITLE_TYPE::MAIN
book.add_creator 'Eliezer Yudkowsky'
book.add_item('images/cover.jpg').add_content(File.open('book-source/images/cover.jpg')).cover_image
book.ordered do
Dir['book-source/chapters/*.xhtml'].sort.each_with_index do |file_name, i|
href = file_name.gsub(%r[^book-source/], '')
html = Nokogiri::HTML(open(file_name))
book.add_item(href).add_content(File.open file_name).toc_text(html.css('title')[0].text)
end
end
book.generate_epub 'rationality.epub'
end
task :default => [:fetch, :epub]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment