Skip to content

Instantly share code, notes, and snippets.

@pointlessone
Forked from anonymous/.rvmrc
Last active December 10, 2015 07:48
Show Gist options
  • Save pointlessone/4403191 to your computer and use it in GitHub Desktop.
Save pointlessone/4403191 to your computer and use it in GitHub Desktop.
HPMoR epub e-book builder task It requires Nokogiri and gepub gems.
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