I wanted to read the web preview of the Django Book’s second edition on my Kindle. Besides the fact that all image links are broken on that website and have apparently been so for some time, I prefer to have these things in the DRM-free MobiPocket / Kindle format. Of course I couldn’t find this anywhere, so I rolled my own based on the book’s SVN repository.

On this page you can download the MobiPocket version of the book and the HTML source files I generated to make it. You can also read on for the skinny on how you can do this yourself.

This procedure works best on a unix-like machine, as we’re going to use grep and sed along with some Python.

1. We start by doing a checkout of the reStructuredText sources of the book, moving the linked graphics into the same directory as the reStructuredText txt files and then creating a grepindex.txt file that will serve as the basis for our table of contents index.txt:

svn co http://djangobook.com/svn/branches/2.0/ 20svn
cd 20svn
find graphics/ -name *.png -exec mv {} . \;
grep -h "^Chapter [0-9]*:" *.txt > grepindex.txt


2. The grepindex.txt will now be converted to something more reStructuredText-like using this script, called grepindex2index.py:

# first do:
# grep -h "^Chapter [0-9]*:\|^Appendix [A-Z]:" *.txt > grepindex.txt
# then:
# python grepindex2index.py grepindex.txt > index.txt

import re
import sys

===================
The Django Book 2.0
===================

Copyright 2006, 2007, 2008, 2009 Adrian Holovaty and Jacob Kaplan-Moss.

This ebook version was prepared by Charl Botha <http://charlbotha.com/> from
the SVN at http://djangobook.com/svn/branches/2.0/ on 2011-04-25, and is
hosted by <http://vxlabs.com/>.

"""

def main():
f1 = open(sys.argv[1])

# this will match on "Chapter 10: the title" or "Appendix B: another title"
# groups 0: chapter 10 or appendix A; 1: 10 (or None), 2: A (or None), 3: title
pat = re.compile('(^Chapter\s*([0-9]*)|^Appendix\s*([A-Z])):\s*(.*)$') chapters = [] appendices = [] for l in f1: # Chapter 10: Advanced Models -> Chapter 10: Advanced Models <chapter10.html>_ mo = pat.match(l) if mo.groups()[1] is not None: chapters.append("* Chapter %s: %s <chapter%02d.html>_" % (mo.groups()[1],mo.groups()[3],int(mo.groups()[1]))) else: appendices.append("* Appendix %s: %s <appendix%s.html>_" % (mo.groups()[2],mo.groups()[3],mo.groups()[2])) print "\n".join(chapters) print "\n".join(appendices) if __name__ == "__main__": main()  Save this to a script called grepindex2index.py, then invoke it with: python grepindex2index.py grepindex.txt > index.txt  3. We’ll then proceed to fix all chapter references with the following bit of sed: sed -i "s/\.\.\/$chapter[0-9]*$\//\1.html/g" chapter*txt  (this will change all “../chapter??/” links to just “chapter??.html”) 4. Everything is now ready to be converted to HTML: for i in *.txt; do rst2html$i echo \$i | cut -f 1 -d ..html; done


5. After downloading this django desktop background as cover image, I dragged and dropped the top-level index.html file onto the free Calibre software to import it, and then used the edit metadata function to set the cover image. After this, I converted the imported files to MobiPocket remembering to add the word “appendix” to the chapter detection xpath expression in the “structured detection” section of the conversion dialogue.

That’s all there’s to it! Have a good read.

• But Django Book, even 2.0 version, isn’t still based on the very very very old 1.0 version? I think it is very outdated so far. Isn’t it?

• It seems to cover up to 1.1 as far as I can see. The last SVN change was in September of 2009.

It remains a brilliant resource to learn Django from. I’d be curious if you know of any specific points where it’s outdated.

• Hi I tried to open book using using the iReader app for android. It kind of works, but it shows some strange characters in the code blocks (Â, latin capital A with a circumflex on top). I’d really appreciate if you could point me in the right direction on how to fix it. It looks like a character encoding issue, but I’m not entirely sure.

Thanks,

Zinahe

• I followed the steps and generated a fresh copy today.
Here it is, if anyone needs the updated(?) version (2mb):
http://dl.dropbox.com/u/5137/kindle-share/djangobook2-kindle/Django%20Book%202.0%2C%20The%20-%20Unknown.mobi

With the front-image (zipped 1.5mb) :
http://dl.dropbox.com/u/5137/kindle-share/djangobook2-kindle/djangobook2-kindle.zip

• @cpbotha: This is great! Thanks!
@Vimal George: Thanks for the book!