Skip to content

Commit

Permalink
initial import of markdown-kindle 0.2
Browse files Browse the repository at this point in the history
  • Loading branch information
eastein committed Mar 18, 2012
0 parents commit 758ee38
Show file tree
Hide file tree
Showing 5 changed files with 264 additions and 0 deletions.
30 changes: 30 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<A name="toc1-0" title="Publish Markdown to your Kindle" />
# Publish Markdown to your Kindle

Like Markdown? Like your Kindle? Want your Markdown documents processed and effortlessly delivered to your Kindle? This is a simple set of scripts for making that happen. They are 'tp' for converting markdown to PDF, 'smtpfile' for sending a file as a MIME attachment, and 'kind', for running the others.

They're pretty easy to use. Extract the scripts in your `$PATH` and set up an alias:

alias kindle='kind [email protected] [email protected] smtp.yourserver.com $1'

Your smtp server as specified must be willing to relay mail to free.kindle.com and/or kindle.com.

I haven't tried it @kindle.com (to allow 3G delivery) because I don't care for the charges. I imagine it would work, though.

Once you've done this, you should be able to simply toss your markdown files onto the Kindle.

eastein@horus:~/docs/txt/chicago$ kindle Chicago\ Neighborhoods\ \&\ Apartments.txt
Delivered Chicago Neighborhoods & Apartments.pdf.
eastein@horus:~/docs/txt/chicago$

<A name="toc1-19" title="Dependencies" />
# Dependencies

You'll want...

* Python 2.6
* dnspython
* markdown
* hml2ps
* ps2pdf
* realpath
28 changes: 28 additions & 0 deletions README.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# Publish Markdown to your Kindle

Like Markdown? Like your Kindle? Want your Markdown documents processed and effortlessly delivered to your Kindle? This is a simple set of scripts for making that happen. They are 'tp' for converting markdown to PDF, 'smtpfile' for sending a file as a MIME attachment, and 'kind', for running the others.

They're pretty easy to use. Extract the scripts in your `$PATH` and set up an alias:

alias kindle='kind [email protected] [email protected] smtp.yourserver.com $1'

Your smtp server as specified must be willing to relay mail to free.kindle.com and/or kindle.com.

I haven't tried it @kindle.com (to allow 3G delivery) because I don't care for the charges. I imagine it would work, though.

Once you've done this, you should be able to simply toss your markdown files onto the Kindle.

eastein@horus:~/docs/txt/chicago$ kindle Chicago\ Neighborhoods\ \&\ Apartments.txt
Delivered Chicago Neighborhoods & Apartments.pdf.
eastein@horus:~/docs/txt/chicago$

# Dependencies

You'll want...

* Python 2.6
* dnspython
* markdown
* hml2ps
* ps2pdf
* realpath
78 changes: 78 additions & 0 deletions kind
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
#!/bin/sh

# script by Eric Stein http://cons.truct.org

# This program is free software. It comes without any warranty, to
# the extent permitted by applicable law. You can redistribute it
# and/or modify it under the terms of the Do What The Fuck You Want
# To Public License, Version 2, as published by Sam Hocevar. See
# http://sam.zoy.org/wtfpl/COPYING for more details.

if [ ! -f "$4" ]
then
echo "no such file: $4"
exit 1
fi

BNAME=`basename "$4"`
if [ $? -gt 0 ]
then
echo "no basename."
exit 1
fi
LNAME=`realpath "$4"`
if [ $? -gt 0 ]
then
echo "no realpath."
exit 1
fi

echo $BNAME | egrep -q '\.txt$'
if [ $? -gt 0 ]
then
echo "not a .txt, aborting."
exit 1
fi

PDFNAME=`echo ${BNAME}|sed -e 's/^\(.*\)\.txt$/\1/g'`.pdf

TDIR=`mktemp -d`
if [ $? -gt 0 ]
then
echo "Failed to create temp dir."
clean_up
fi

clean() {
cd
if [ -d $TDIR ]
then
rm -rf $TDIR
fi
}

clean_up() {
clean
exit 1
}

cd $TDIR
if [ $? -gt 0 ]
then
echo "Couldn't cd to temp dir."
clean_up
fi

tp "$LNAME" "$PDFNAME"
if [ $? -gt 0 ]
then
echo "Failed to convert to PDF."
clean_up
fi

smtpfile "$PDFNAME" $1 $2 $3 $SMTPSERVER
if [ $? -eq 0 ]
then
echo "Delivered $PDFNAME."
fi
clean
72 changes: 72 additions & 0 deletions smtpfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
#!/usr/bin/env python

# most code from http://docs.python.org/library/email-examples.html, but ported to work on python 2.6.
# Original script Copyright 2001-2010 Python Software Foundation; All Rights Reserved
# Modifications Copyright 2011 Eric Stein
# Modded to simply send one file by Eric Stein http://cons.truct.org
# Licensed GPL2

import smtplib
import mimetypes
import os
import re
import sys
import dns.resolver

from email import encoders
from email.mime.multipart import MIMEMultipart
from email.mime.base import MIMEBase
from email.message import Message

em_matcher = re.compile('^(.*)@(.*)$')

em_attach = sys.argv[1]
em_from = sys.argv[2]
em_to = sys.argv[3]
smtp_server = sys.argv[4]
if smtp_server == 'autoselect' :
try :
for x in dns.resolver.query(to_domain, 'MX') :
smtp_server = x.to_text().split(' ')[1]
except :
print 'failed to look up the mx... [x]'
sys.exit(1)

if not em_matcher.match(em_from) :
raise RuntimeError("bad email from address")

matched = em_matcher.match(em_to)
if not matched :
raise RuntimeError("bad email to address")

trash, to_domain = matched.groups()

outer = MIMEMultipart()
outer['To'] = em_to
outer['From'] = em_from
outer.preamble = 'MIME file attached..\n'

if not os.path.isfile(em_attach) :
raise RuntimeError("%s is not a file" % em_attach)

ctype, encoding = mimetypes.guess_type(em_attach)
if ctype is None or encoding is not None:
# No guess could be made, or the file is encoded (compressed), so
# use a generic bag-of-bits type.
ctype = 'application/octet-stream'
maintype, subtype = ctype.split('/', 1)
fp = open(em_attach, 'rb')
msg = MIMEBase(maintype, subtype)
msg.set_payload(fp.read())
fp.close()
encoders.encode_base64(msg)

msg.add_header('Content-Disposition', 'attachment', filename=em_attach)
outer.attach(msg)

composed = outer.as_string()

s = smtplib.SMTP()
s.connect(smtp_server)
s.sendmail(em_from, em_to, composed)
s.quit()
56 changes: 56 additions & 0 deletions tp
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
#!/bin/sh

# script by Eric Stein http://cons.truct.org

# This program is free software. It comes without any warranty, to
# the extent permitted by applicable law. You can redistribute it
# and/or modify it under the terms of the Do What The Fuck You Want
# To Public License, Version 2, as published by Sam Hocevar. See
# http://sam.zoy.org/wtfpl/COPYING for more details.

TXT=$1
PDF=$2

H=markdown
PS=html2ps
PD=ps2pdf

if [ ! -f "$TXT" ]
then
exit "$TXT does not exist"
exit 1
fi

touch "$PDF"

if [ ! -f "$PDF" ]
then
exit "$PDF could not be created"
exit 1
fi

which $H>/dev/null
if [ $? -gt 0 ]
then
echo "$H is not a program."
exit 1
fi
which $PD>/dev/null
if [ $? -gt 0 ]
then
echo "$PD is not a program."
exit 1
fi
which $PS>/dev/null
if [ $? -gt 0 ]
then
echo "$PS is not a program."
exit 1
fi

$H "$TXT"|$PS|$PD - "$PDF"
if [ $? -gt 0 ]
then
echo "Failed to process."
exit 1
fi

0 comments on commit 758ee38

Please sign in to comment.