Created
January 5, 2013 05:29
-
-
Save hakobera/4459938 to your computer and use it in GitHub Desktop.
Send markdown document to Kindle. Required: ruby-1.9, kindlegen, [gem]action_mailer, [gem]makrdown and Gmail account Usage: md2kindle [your-markdown-document]
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
#! /usr/bin/env ruby | |
require 'markdown' | |
src_file = ARGV[0] | |
File.open(src_file) do |src| | |
content = Markdown.new(src.read).to_html | |
template = DATA.read | |
puts template.gsub(/#\{content\}/, content) | |
end | |
__END__ | |
<html> | |
<head> | |
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> | |
<style type="text/css"> | |
h2, h3, h4 { | |
padding-bottom: 0.1em; | |
border-bottom: 1px solid #666; | |
} | |
pre { | |
padding: 0.25em 0.5em; | |
background-color: #eee; | |
} | |
pre > code { | |
font-size: 2em; | |
} | |
</style> | |
</head> | |
<body> | |
#{content} | |
</body> | |
</html> |
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
#! /usr/bin/env ruby | |
require 'pathname' | |
require 'tmpdir' | |
script_path = File.expand_path(File.dirname(__FILE__)) | |
markdown_file = ARGV[0] | |
base_name = File.basename(markdown_file) | |
html_file = Pathname(base_name).sub_ext('.html').to_s | |
mobi_file = Pathname(base_name).sub_ext('.mobi').to_s | |
Dir.mktmpdir do |tmpdir| | |
html_file = "#{tmpdir}/#{Pathname(base_name).sub_ext('.html').to_s}" | |
mobi_file = Pathname(base_name).sub_ext('.mobi').to_s | |
puts "Convert #{markdown_file} to #{html_file}" | |
system("#{script_path}/md2html.rb #{markdown_file} > #{html_file}") | |
puts "Convert #{html_file} to #{mobi_file}" | |
system("kindlegen #{html_file} -o #{mobi_file}") | |
puts "Send #{mobi_file} to kindle" | |
system("#{script_path}/send_to_kindle.rb #{tmpdir}/#{mobi_file}") | |
end | |
puts "Done" |
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
#! /usr/bin/env ruby | |
require 'rubygems' | |
require 'action_mailer' | |
class KindleMailer < ActionMailer::Base | |
def send_to_kindle(filename) | |
base_name = File.basename(filename) | |
attachments[base_name] = { | |
content: File.read(filename, :mode => 'rb'), | |
transfer_encoding: :binary | |
} | |
mail( | |
to: '[your-kindle-mail]', | |
from: '[your-gmail]', | |
subject: base_name, | |
body: filename | |
) | |
end | |
end | |
ActionMailer::Base.delivery_method = :smtp | |
ActionMailer::Base.perform_deliveries = true | |
ActionMailer::Base.raise_delivery_errors = true | |
ActionMailer::Base.smtp_settings = { | |
:enable_starttls_auto => true, | |
:address => 'smtp.gmail.com', | |
:port => 587, | |
:domain => 'gmail.com', | |
:authentication => :plain, | |
:user_name => '[your-gmail-account]', | |
:password => '[your-gmail-password]' | |
} | |
filename = ARGV[0] | |
KindleMailer.send_to_kindle(filename).deliver |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment