Gentoo Forums
Gentoo Forums
Gentoo Forums
Quick Search: in
Generating an Epub from the Gentoo Handbook
View unanswered posts
View posts from last 24 hours

 
Reply to topic    Gentoo Forums Forum Index Other Things Gentoo
View previous topic :: View next topic  
Author Message
keba
Guru
Guru


Joined: 02 Jun 2006
Posts: 304
Location: Switzerland

PostPosted: Sat Jun 30, 2012 12:52 pm    Post subject: Generating an Epub from the Gentoo Handbook Reply with quote

Hi,

I thought it would be nice to have an Epub ebook of the Gentoo Handbook. Since it could not be easily downloaded, I made a bash script, which depends on sed, imagemagick, zip and wget. It is by far not perfect, as no links in the book can be clicked. But it works for me... Changes/Improvements are welcome!

Note: Some lines were broken apart when I copied this script here. You can't just copy-paste this!

Code:

#!/bin/bash

# This script generates an epub file from the online Gentoo Handbook
# Dependencies: wget, ImageMagick,

# Usage: ./handbook2epub

# 1) Create directory structure
echo -n "1) Create directory structure      "
mkdir -p GentooHandbook/META-INF/
mkdir -p GentooHandbook/OEBPS/images/
mkdir -p GentooHandbook/OEBPS/css/
mkdir -p GentooHandbook/OEBPS/text/
echo "application/epub+zip" >> GentooHandbook/mimetype
echo "<?xml version=\"1.0\"?>" >> GentooHandbook/META-INF/container.xml
echo "<container version=\"1.0\" xmlns=\"urn:oasis:names:tc:opendocument:xmlns:container\">" \
     >> GentooHandbook/META-INF/container.xml
echo "    <rootfiles>" >> GentooHandbook/META-INF/container.xml
echo "        <rootfile full-path=\"OEBPS/content.opf\" media-type=\"application/oebps-package+xml\"/>" \
     >> GentooHandbook/META-INF/container.xml
echo "   </rootfiles>" >> GentooHandbook/META-INF/container.xml
echo "</container>" >> GentooHandbook/META-INF/container.xml
echo "done"

# 2) Create a cover for the book
echo -n "2) Create a cover for the book      "
wget -q -O GentooHandbook/OEBPS/images/cover.jpg \
"http://www.gentoo.org/images/backgrounds/gentoo-minimal-1024x768.jpg"
convert -crop 500x700+262+68 GentooHandbook/OEBPS/images/cover.jpg cover2.jpg
convert -font /usr/share/fonts/libertine-ttf/LinBiolinum_RB.ttf -fill white -stroke black \
        -pointsize 72 -gravity center -draw "text 0,100 'The'" -draw "text 0,175 'Gentoo'" \
        -draw "text 0,250 'Handbook'" cover2.jpg GentooHandbook/OEBPS/images/cover.jpg
rm cover2.jpg
echo "done"

# 3) Get the style sheet
echo -n "3) Get the style sheet         "
wget -q -O GentooHandbook/OEBPS/css/main.css "http://www.gentoo.org/css/main.css"
echo "done"

# 4) Generate the TOC
echo -n "4) Generating the TOC         "
today=`date +%Y-%m-%d`
# first part of content.opf
echo "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" >> GentooHandbook/OEBPS/content.opf
echo "<package xmlns=\"http://www.idpf.org/2007/opf\" unique-identifier=\"BookID\" version=\"2.0\">" \
     >> GentooHandbook/OEBPS/content.opf
echo "    <metadata xmlns:dc=\"http://purl.org/dc/elements/1.1/\" xmlns:opf=\"http://www.idpf.org/2007/opf\">" \
     >> GentooHandbook/OEBPS/content.opf
echo "        <dc:contributor opf:role=\"bkp\"></dc:contributor>" >> GentooHandbook/OEBPS/content.opf
echo "        <dc:date opf:event=\"creation\">$today</dc:date>" >> GentooHandbook/OEBPS/content.opf
echo "        <dc:creator opf:role=\"aut\">The Gentoo Foundation</dc:creator>" \
     >> GentooHandbook/OEBPS/content.opf
echo "        <dc:language>en</dc:language>" >> GentooHandbook/OEBPS/content.opf
echo "        <dc:title>The Gentoo Handbook</dc:title>" >> GentooHandbook/OEBPS/content.opf
echo "        <meta name=\"cover\" content=\"cover.jpg\"/>" >> GentooHandbook/OEBPS/content.opf
echo "    </metadata>"  >> GentooHandbook/OEBPS/content.opf
echo "    <manifest>" >> GentooHandbook/OEBPS/content.opf
echo "        <item id=\"ncx\" href=\"toc.ncx\" media-type=\"application/x-dtbncx+xml\"/>" \
     >> GentooHandbook/OEBPS/content.opf
echo "        <item id=\"cover.jpg\" href=\"images/cover.jpg\" media-type=\"image/jpeg\"/>" \
     >> GentooHandbook/OEBPS/content.opf
echo "        <item id=\"main.css\" href=\"css/main.css\" media-type=\"text/css\"/>" \
     >> GentooHandbook/OEBPS/content.opf
echo "        <item id=\"coverpage.xhtml\" href=\"text/coverpage.xhtml\" media-type=\"application/xhtml+xml\"/>" \
     >> GentooHandbook/OEBPS/content.opf
# second part of content.opf
echo "    <spine toc=\"ncx\">" >> GentooHandbook/OEBPS/content2.opf
echo "        <itemref idref=\"coverpage.xhtml\"/>" >> GentooHandbook/OEBPS/content2.opf
# toc.ncx
echo "<?xml version=\"1.0\"?>" >> GentooHandbook/OEBPS/toc.ncx
echo "<!DOCTYPE ncx PUBLIC \"-//NISO//DTD ncx 2005-1//EN\"" >> GentooHandbook/OEBPS/toc.ncx
echo "    \"http://www.daisy.org/z3986/2005/ncx-2005-1.dtd\">" >> GentooHandbook/OEBPS/toc.ncx
echo "" >> GentooHandbook/OEBPS/toc.ncx
echo "<ncx xmlns=\"http://www.daisy.org/z3986/2005/ncx/\" version=\"2005-1\">" >> GentooHandbook/OEBPS/toc.ncx
echo "    <head>" >> GentooHandbook/OEBPS/toc.ncx
echo "        <meta name=\"dtb:uid\" content=\"9C911FF4-B596-4E4B-AD98-D9D9A0956533\"/>" >> GentooHandbook/OEBPS/toc.ncx
echo "        <meta name=\"dtb:depth\" content=\"1\"/>" >> GentooHandbook/OEBPS/toc.ncx
echo "        <meta name=\"dtb:totalPageCount\" content=\"0\"/>" >> GentooHandbook/OEBPS/toc.ncx
echo "        <meta name=\"dtb:maxPageNumber\" content=\"0\"/>" >> GentooHandbook/OEBPS/toc.ncx
echo "    </head>" >> GentooHandbook/OEBPS/toc.ncx
echo "    <docTitle>" >> GentooHandbook/OEBPS/toc.ncx
echo "        <text>The Gentoo Handbook</text>" >> GentooHandbook/OEBPS/toc.ncx
echo "    </docTitle>" >> GentooHandbook/OEBPS/toc.ncx
echo "    <navMap>" >> GentooHandbook/OEBPS/toc.ncx
echo "done"

# 5) Now the text
#----------------
# 5)a) Part 1, Index
echo "5) Get the text"

echo -n "   Generating the coverpage      "
echo "<?xml version=\"1.0\" encoding=\"utf-8\"?>" >> GentooHandbook/OEBPS/text/coverpage.xhtml
echo "<html xmlns=\"http://www.w3.org/1999/xhtml\" xml:lang=\"en\">" >> GentooHandbook/OEBPS/text/coverpage.xhtml
echo "  <head>" >> GentooHandbook/OEBPS/text/coverpage.xhtml
echo "    <meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\"/>" \
     >> GentooHandbook/OEBPS/text/coverpage.xhtml
echo "    <title>Cover</title>" >> GentooHandbook/OEBPS/text/coverpage.xhtml
echo "    <style type=\"text/css\" title=\"override_css\">" >> GentooHandbook/OEBPS/text/coverpage.xhtml
echo "      @page {padding: 0pt; margin:0pt}" >> GentooHandbook/OEBPS/text/coverpage.xhtml
echo "      body { text-align: center; padding:0pt; margin: 0pt; background-color : #000000;}" \
     >> GentooHandbook/OEBPS/text/coverpage.xhtml
echo "    </style>" >> GentooHandbook/OEBPS/text/coverpage.xhtml
echo "  </head>" >> GentooHandbook/OEBPS/text/coverpage.xhtml
echo "  <body>" >> GentooHandbook/OEBPS/text/coverpage.xhtml
echo "    <div>    <img height=\"100%\" alt=\"Cover\" src=\"../images/cover.jpg\" />" \
     >> GentooHandbook/OEBPS/text/coverpage.xhtml
echo "    </div>" >> GentooHandbook/OEBPS/text/coverpage.xhtml
echo "  </body>" >> GentooHandbook/OEBPS/text/coverpage.xhtml
echo "</html>" >> GentooHandbook/OEBPS/text/coverpage.xhtml
echo "done"

#Get the text
parts=4
number=0
for (( h = 1; h <= parts; h++ )); do
   number=$(( number + 1 ))
   echo -n "   Part $h, Index         "
   mkdir -p GentooHandbook/OEBPS/text/p$h/text/
   wget -q -O GentooHandbook/OEBPS/text/p$h/text/index$h.html \
        "http://www.gentoo.org/doc/en/handbook/handbook-x86.xml?style=printable&part=$h"
   # first part of content.opf
   echo "        <item id=\"index$h.html\" href=\"text/p$h/text/index$h.html\" media-type=\"application/html\"/>" \
        >> GentooHandbook/OEBPS/content.opf
   # second part of content.opf
   echo "        <itemref idref=\"index$h.html\"/>" >> GentooHandbook/OEBPS/content2.opf
   # toc.ncx
   text=`cat GentooHandbook/OEBPS/text/p$h/text/index$h.html | grep h1 | sed -n "s/<h1>//;s/<\/h1>//;1 p ;1 q"`
   echo "      <navPoint id=\"navPoint-1\" playOrder=\"1\">" >> GentooHandbook/OEBPS/toc.ncx
   echo "        <navLabel>" >> GentooHandbook/OEBPS/toc.ncx
   echo "          <text>$text</text>" >> GentooHandbook/OEBPS/toc.ncx
   echo "        </navLabel>" >> GentooHandbook/OEBPS/toc.ncx
   echo "        <content src=\"text/p$h/text/index$h.html\"/>" >> GentooHandbook/OEBPS/toc.ncx
   echo "done"
   mkdir -p GentooHandbook/OEBPS/text/p$h/text/
   if [ $h == 1 ]; then
      chaps=12
   elif [ $h == 2 ]; then
      chaps=5
   elif [ $h == 3 ]; then
      chaps=6
   elif [ $h == 4 ]; then
      chaps=6
   fi
   for (( i = 1; i <= chaps; i++ )); do
      number=$(( number + 1 ))
      chap=$(( $i + 1 ))
      echo -n "     Part $h, Chapter $i         "
      wget -q -O GentooHandbook/OEBPS/text/p$h/text/chap$h-$i.html \
           "http://www.gentoo.org/doc/en/handbook/handbook-x86.xml?style=printable&part=$h&chap=$i"
      # first part of content.opf
      echo "        <item id=\"chap$h-$i.html\" href=\"text/p$h/text/chap$h-$i.html\" media-type=\"application/html\"/>" \
           >> GentooHandbook/OEBPS/content.opf
      # second part of content.opf
      echo "        <itemref idref=\"chap$h-$i.html\"/>" >> GentooHandbook/OEBPS/content2.opf
      # toc.ncx
      text=`cat GentooHandbook/OEBPS/text/p$h/text/chap$h-$i.html | grep h1 | sed -n "s/<h1>//;s/<\/h1>//;1 p ;1 q"`
      echo "        <navPoint id=\"navPoint-$number\" playOrder=\"$number\">" >> GentooHandbook/OEBPS/toc.ncx
      echo "          <navLabel>" >> GentooHandbook/OEBPS/toc.ncx
      echo "            <text>$text</text>" >> GentooHandbook/OEBPS/toc.ncx
      echo "          </navLabel>" >> GentooHandbook/OEBPS/toc.ncx
      echo "          <content src=\"text/p$h/text/chap$h-$i.html\"/>" >> GentooHandbook/OEBPS/toc.ncx
      echo "        </navPoint>" >> GentooHandbook/OEBPS/toc.ncx
      echo "done"
   done
   echo "      </navPoint>" >> GentooHandbook/OEBPS/toc.ncx
done

# 6) Finishing
echo -n "6) Finishing            "
# first part of content.opf
echo "    </manifest>" >> GentooHandbook/OEBPS/content.opf
# second part of content.opf
echo "    </spine>" >> GentooHandbook/OEBPS/content2.opf
echo "    <guide>" >> GentooHandbook/OEBPS/content2.opf
echo "       <reference type=\"cover\" title=\"Cover\" href=\"text/coverpage.xhtml\" />" >> GentooHandbook/OEBPS/content2.opf
echo "    </guide>" >> GentooHandbook/OEBPS/content2.opf
echo "</package>" >> GentooHandbook/OEBPS/content2.opf
# merge the 2 parts of content.opf
cat GentooHandbook/OEBPS/content2.opf >> GentooHandbook/OEBPS/content.opf
rm GentooHandbook/OEBPS/content2.opf
# toc.ncx
echo "    </navMap>" >> GentooHandbook/OEBPS/toc.ncx
echo "</ncx>" >> GentooHandbook/OEBPS/toc.ncx
echo "done"

# 7) Generate the epub
echo -n "7) Generating the Epub         "
cd GentooHandbook
zip -q -r ../GentooHandbook.epub .
cd ..
rm -rf GentooHandbook
echo "done"

_________________
Prayer can change the world!


Last edited by keba on Mon Jul 02, 2012 8:48 pm; edited 2 times in total
Back to top
View user's profile Send private message
freke
Apprentice
Apprentice


Joined: 23 Jan 2003
Posts: 153

PostPosted: Mon Jul 02, 2012 6:04 pm    Post subject: Reply with quote

Interesting... think I'll have to give this a try tomorrow :)
Back to top
View user's profile Send private message
Display posts from previous:   
Reply to topic    Gentoo Forums Forum Index Other Things Gentoo All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum