Skip to content

Instantly share code, notes, and snippets.

@edwardtoday
Last active December 30, 2015 00:19
Show Gist options
  • Save edwardtoday/7748789 to your computer and use it in GitHub Desktop.
Save edwardtoday/7748789 to your computer and use it in GitHub Desktop.
script to convert file from txt in gb18030 encoding to mobi
#!/bin/sh
echo "convert to utf8"
iconv -c -f GB18030 -t UTF8 $1 > $1.tmp
echo "txt cleanup"
sed 's/.$//' $1.tmp > $1.tmp.tmp && mv $1.tmp.tmp $1.tmp # dos2unix
sed '/^$/d;G' $1.tmp > $1.tmp.tmp && mv $1.tmp.tmp $1.tmp # double spacing
sed 's/^[ \t]*//;s/[ \t]*$//' $1.tmp > $1.tmp.tmp && mv $1.tmp.tmp $1.tmp # delete leading and trailing spaces
sed 's/#//' $1.tmp > $1.tmp.tmp && mv $1.tmp.tmp $1.tmp # delete # char
sed 's/^第.*[卷部篇]/##&/' $1.tmp > $1.tmp.tmp && mv $1.tmp.tmp $1.tmp # make headings
sed 's/^第.*[章节集回]/###&/' $1.tmp > $1.tmp.tmp && mv $1.tmp.tmp $1.tmp # make headings
echo "convert to html"
markdown -o $1.html $1.tmp
echo "<HTML><BODY>" | cat - $1.html > $1.tmp && mv $1.tmp $1.html
echo "</BODY></HTML>" >> $1.html
echo "convert to mobi"
kindlegen $1.html -c2 -verbose -o $2
rm $1.html
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment