Skip to content

Instantly share code, notes, and snippets.

@cubarco
Last active December 13, 2015 05:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cubarco/e1b8265a3bc2001790ca to your computer and use it in GitHub Desktop.
Save cubarco/e1b8265a3bc2001790ca to your computer and use it in GitHub Desktop.
Convert txt file to mobi with monospace font using kindlegen.
#!/usr/bin/env bash
if [ "$#" -ne 2 ]; then
echo "usage: $0 IN_FILE OUT_FILE"
exit
fi
infile=$1; shift
outfile=$1
title=${infile%.*}
temphtml=$(mktemp /tmp/XXXXXXXXX.html)
{
echo '<html>'
echo "<title>$title</title>"
echo '<body>'
echo '<pre>'
sed 's/&/\&amp;/g; s/</\&lt;/g; s/>/\&gt;/g; s/"/\&quot;/g; s/'"'"'/\&#39;/g' $infile
echo '</pre>'
echo '</body>'
echo '</html>'
} > $temphtml
tempmobi=$(mktemp /tmp/XXXXXXXXX.mobi)
cd /tmp/
# kindlegen does not accept output file contains directory path.
kindlegen $temphtml -o $(basename $tempmobi)
cd $OLDPWD
mv $tempmobi $outfile
rm $temphtml
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment