Last active
January 2, 2016 09:39
-
-
Save posaunehm/8284534 to your computer and use it in GitHub Desktop.
プログラミング Coq(http://www.iij-ii.co.jp/lab/techdoc/coqt/coqt1.html) から電子書籍を作ります。 Nokogiriとcalibre コマンドラインツール必須。
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
require 'nokogiri' | |
require 'open-uri' | |
require "FileUtils" | |
target = "http://www.iij-ii.co.jp/lab/techdoc/coqt/" | |
html = Nokogiri::HTML(open(target)) | |
url_list = html.css("#home-main ol li a").collect{|a| target + a["href"]} | |
article_list = url_list.collect{|url| | |
article_doc = Nokogiri::HTML(open(url)) | |
article_doc.css("#home-main").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| | |
article_list.each {|article| | |
f.puts(article) | |
} | |
} | |
title = html.title; | |
author = "池渕未来" | |
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