Skip to content

Instantly share code, notes, and snippets.

@posaunehm
Created January 10, 2014 16:04
Show Gist options
  • Save posaunehm/8357077 to your computer and use it in GitHub Desktop.
Save posaunehm/8357077 to your computer and use it in GitHub Desktop.
http://twelve-factor-ja.herokuapp.com/ をepub/mobi化するスクリプト
# -*- coding: utf-8 -*-
require 'nokogiri'
require 'open-uri'
require "FileUtils"
target = "http://twelve-factor-ja.herokuapp.com"
html = Nokogiri::HTML(open(target))
top_article = html.css(".abstract")
url_list = html.css(".concrete h2 > a").collect{|a| target + a["href"]}
article_list = url_list.collect{|url|
article_doc = Nokogiri::HTML(open(url))
article_doc.css(".abstract").first
}
article_list.each{ |article_doc|
article_doc.css("img").each{ |ele|
path =target + ele["src"]
fileName = File.basename(path)
dir = "img/"
filepath = dir + fileName
FileUtils.mkdir_p(dir) unless FileTest.exist?(dir)
open(filepath, 'wb') do |output|
open(path) do |data|
output.write(data.read)
end
end
ele["src"] = filepath
}
}
open("temp.html", "w"){ |f|
f.puts(top_article)
article_list.each {|article|
f.puts(article)
}
}
title = html.title;
author = "Adam Wiggins"
open("temp_title.txt", "w"){ |f|
f.puts("% #{title}")
f.puts("% #{author}")
}
`pandoc temp.html -o "#{title}.md"`
`pandoc temp_title.txt "#{title}.md" -o "#{title}.epub"`
`ebook-convert "#{title}.epub" "#{title}.mobi"`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment