Created
January 10, 2014 16:04
-
-
Save posaunehm/8357077 to your computer and use it in GitHub Desktop.
http://twelve-factor-ja.herokuapp.com/ をepub/mobi化するスクリプト
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- 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