Created
September 4, 2012 03:55
-
-
Save lqik2004/3616362 to your computer and use it in GitHub Desktop.
Convert Text2Mobi
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
bangzhu (){ | |
cat << EOF | |
用法: `basename $0` 文件名 [书名] [作者] | |
其中文件名是必须的,书名、作者什么的可以不添加,书名默认为文件名。作者则留空。至于出版社什么的就都浮云了。其实修改也很简单,但是我不需要所以就从简了。 | |
EOF | |
} | |
if [ $1 ] | |
then | |
myfn="${1%%.txt}" | |
else | |
bangzhu | |
exit | |
fi | |
if [ $2 ] | |
then | |
shuming="$2" | |
else | |
shuming="$myfn" | |
fi | |
echo "测试文件格式" | |
if [ "`file $1 |grep UTF-8`" ] | |
then | |
cp $1 work.txt | |
elif [ "`file $1 |grep UTF-16`" ] | |
then | |
iconv -f utf16 -t utf8 $1 -o work.txt | |
else | |
iconv -f gbk -t utf8 $1 -o work.txt | |
fi | |
if [ ! "`file work.txt |grep UTF-8`" ] | |
then | |
echo "编码不支持,请手动转成utf8再试" | |
exit | |
fi | |
######################################################## | |
echo "尝试将目录下的图片文件转换成封面" | |
if [ ! -f "cover.gif" ] | |
then | |
find . -iname "*.gif" -exec convert {} cover.gif \; 2>/dev/null | |
find . -iname "*.jpeg" -exec convert {} cover.gif \; 2>/dev/null | |
find . -iname "*.png" -exec convert {} cover.gif \; 2>/dev/null | |
find . -iname "*.jpg" -exec convert {} cover.gif \; 2>/dev/null | |
fi | |
if [ -f "cover.gif" ] | |
then | |
echo "封面创建成功。" | |
else | |
echo "封面创建失败。请安装ImageMagick,然后找张图片放在目录下" | |
fi | |
###################################################### | |
echo "开始转换,处理文本……" | |
sed -i 's/^ *//g' work.txt | |
sed -i 's/^ *//g' work.txt | |
sed -i 's/^\s*//g' work.txt | |
sed -i '/^$/d' work.txt | |
sed -i '/^[[]]*$/d' work.txt | |
sed -i '/^\s*$/d' work.txt | |
sed -i 's/^/ /g' work.txt | |
sed -i 's/\*/zhegeshixinghao/g' work.txt | |
awk '/ 第[0-9|一|二|三|四|五|六|七|八|九|十|百|千|万|零| | |]*[章|卷|节||集]+/{sub(/^/,"## ")}{print $0} ' work.txt > temp | |
sed -i 's/$/<br>/g' temp | |
./markdown temp >work.html | |
sed -i 's/<p><br>/<p>/g' work.html | |
echo "markdown ok" | |
cp work.html bak.html | |
sed -i 's/zhegeshixinghao/\*/g' work.html | |
awk -vi=0 '{if ($0 ~ "<h2>") {i++;cpt=sub(/ /,"");print($0"@"i) > "list" }} {print($0) > i".html"}' work.html | |
################################################################################################################ | |
cat >he <<EOF | |
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"> | |
<html xmlns="http://www.w3.org/1999/xhtml"> | |
<head> | |
<title>$myfn</title> | |
<meta http-equiv="Content-Type" content="text/html; charset=utf8" /> | |
<link rel="stylesheet" href="KUG.css" type="text/css" /> | |
</head> | |
<body> | |
EOF | |
cat >bo <<EOF | |
</body> | |
</html> | |
EOF | |
for i in *.html ;do cat he $i bo >temp;mv temp $i;done | |
rm he | |
rm bo | |
if [ -f list ] | |
then | |
sed -i 's/<h2>//g' list | |
sed -i 's/<\/h2>//g' list | |
sed -i 's/<br>//g' list | |
####################################################################################################### | |
echo "为创建html文件作准备" | |
while read j | |
do | |
biaoti=`echo $j|awk -F@ '{print $1}'` | |
fname=`echo $j|awk -F@ '{print $2}'` | |
echo " <item id=\"item${fname}\" media-type=\"application/xhtml+xml\" href=\"${fname}.html\"></item>" >>ma | |
echo " <itemref idref=\"item${fname}\"/>" >>sp | |
echo " <navPoint class=\"chapter\" id=\"chapter_${fname}\" playOrder=\"${fname}\"> | |
<navLabel> | |
<text>${biaoti}</text> | |
</navLabel> | |
<content src=\"${fname}.html\"/> | |
</navPoint>" >>nav | |
echo "<li><a href=\"${fname}.html\">${biaoti}</a></li>" >>toc | |
done <list | |
###################################################################################################### | |
echo "开始创建${myfn}.opf" | |
cat >"${myfn}".opf<<EOF | |
<?xml version="1.0" encoding="utf-8"?> | |
<package xmlns="http://www.idpf.org/2007/opf" version="2.0" unique-identifier="BookId"> | |
<metadata xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:opf="http://www.idpf.org/2007/opf"> | |
<dc:title>${shuming}</dc:title> | |
<dc:language>en-US</dc:language> | |
<meta name="cover" content="My_Cover" /> | |
<dc:identifier id="BookId" opf:scheme="ISBN"></dc:identifier> | |
<dc:creator>${3}</dc:creator> | |
<dc:publisher></dc:publisher> | |
<dc:subject></dc:subject> | |
<dc:date></dc:date> | |
<dc:description></dc:description> | |
</metadata> | |
<manifest> | |
<!-- HTML content files [mandatory] --> | |
EOF | |
cat ma >>"${myfn}".opf | |
rm ma | |
cat >> "${myfn}".opf<<EOF | |
<item id="itemx" media-type="application/xhtml+xml" href="toc.html"></item> | |
<item id="My_Table_of_Contents" media-type="application/x-dtbncx+xml" href="KUG.ncx"/> | |
<item id="My_Cover" media-type="image/gif" href="cover.gif"/> | |
</manifest> | |
<spine toc="My_Table_of_Contents"> | |
<!-- the spine defines the linear reading order of the book --> | |
<itemref idref="itemx"/> | |
EOF | |
cat sp >>"${myfn}".opf | |
rm sp | |
cat >>"${myfn}".opf<<EOF | |
</spine> | |
<guide> | |
<reference type="toc" title="Table of Contents" href="toc.html"></reference> | |
</guide> | |
</package> | |
EOF | |
################################################################################# | |
echo "开始创建KUG.ncx" | |
cat >KUG.ncx <<EOF | |
<?xml version="1.0" encoding="UTF-8"?> | |
<!DOCTYPE ncx PUBLIC "-//NISO//DTD ncx 2005-1//EN" | |
"http://www.daisy.org/z3986/2005/ncx-2005-1.dtd"> | |
<!-- | |
For a detailed description of NCX usage please refer to: | |
http://www.idpf.org/2007/opf/OPF_2.0_final_spec.html#Section2.4.1 | |
--> | |
<ncx xmlns="http://www.daisy.org/z3986/2005/ncx/" version="2005-1" xml:lang="en-US"> | |
<head> | |
<meta name="dtb:uid" content="BookId"/> | |
<meta name="dtb:depth" content="2"/> | |
<meta name="dtb:totalPageCount" content="0"/> | |
<meta name="dtb:maxPageNumber" content="0"/> | |
</head> | |
<docTitle><text>${shuming}</text></docTitle> | |
<docAuthor><text>${3}</text></docAuthor> | |
<navMap> | |
<navPoint class="toc" id="toc" playOrder="0"> | |
<navLabel> | |
<text>目录</text> | |
</navLabel> | |
<content src="toc.html"/> | |
</navPoint> | |
EOF | |
cat nav >>KUG.ncx | |
rm nav | |
cat >>KUG.ncx <<EOF | |
</navMap> | |
</ncx> | |
EOF | |
############################################################################## | |
echo "开始创建toc.html" | |
cat >toc.html <<EOF | |
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"> | |
<html xmlns="http://www.w3.org/1999/xhtml"> | |
<head><title>Table of Contents</title> | |
<meta http-equiv="Content-Type" content="text/html; charset=utf8" /> | |
</head> | |
<body> | |
<div> | |
<h1><b>目录</b></h1> | |
<div><ul> | |
EOF | |
cat toc >>toc.html | |
rm toc | |
cat >>toc.html <<EOF | |
</ul></div> | |
<h1 class="centered">* * *</h1> | |
</div> | |
</body> | |
</html> | |
EOF | |
################################################################################# | |
mulu=0 | |
else | |
mulu=1 | |
sed -i 's/zhegeshixinghao/\*/g' bak.html | |
echo "开始创建${myfn}.opf" | |
cat >"${myfn}".opf<<EOF | |
<?xml version="1.0" encoding="utf-8"?> | |
<package xmlns="http://www.idpf.org/2007/opf" version="2.0" unique-identifier="BookId"> | |
<metadata xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:opf="http://www.idpf.org/2007/opf"> | |
<dc:title>${shuming}</dc:title> | |
<dc:language>en-US</dc:language> | |
<meta name="cover" content="My_Cover" /> | |
<dc:identifier id="BookId" opf:scheme="ISBN"></dc:identifier> | |
<dc:creator>${3}</dc:creator> | |
<dc:publisher></dc:publisher> | |
<dc:subject></dc:subject> | |
<dc:date></dc:date> | |
<dc:description></dc:description> | |
</metadata> | |
<manifest> | |
<!-- HTML content files [mandatory] --> | |
<item id="itemx" media-type="application/xhtml+xml" href="bak.html"></item> | |
<item id="My_Cover" media-type="image/gif" href="cover.gif"/> | |
</manifest> | |
<spine toc="My_Table_of_Contents"> | |
<!-- the spine defines the linear reading order of the book --> | |
<itemref idref="itemx"/> | |
</spine> | |
<guide> | |
</guide> | |
</package> | |
EOF | |
fi | |
############################################################################# | |
echo "开始创建KUG.css" | |
cat >KUG.css<<EOF | |
/* Style Definitions */ | |
p | |
{ | |
margin-top: 1em; | |
text-indent: 0em | |
} | |
.pagebreak | |
{ | |
page-break-before: always; | |
} | |
.centered | |
{ | |
text-align: center; | |
} | |
.bottom | |
{ | |
vertical-align: text-bottom; | |
} | |
.tablehead | |
{ | |
text-align: center; | |
font-weight: bold | |
} | |
H1 {margin-top: 1em} | |
H2 {margin-top: 1em} | |
H3 {margin-top: 1em} | |
H4 {margin-top: 1em} | |
EOF | |
############################################################################## | |
echo "开始转换成mobi" | |
./kindlegen "${myfn}".opf | |
rm *.html 2>/dev/null | |
rm KUG.css 2>/dev/null | |
rm KUG.ncx 2>/dev/null | |
rm "${myfn}".opf 2>/dev/null | |
rm list 2>/dev/null | |
rm work.txt 2>/dev/null | |
echo | |
echo | |
if (( $mulu == 0 )) | |
then | |
echo " ${myfn}.mobi 创建完成了,这本书带目录的" | |
else | |
echo " ${myfn}.mobi 创建完成了,可惜无法创建目录。" | |
fi | |
echo | |
echo |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
最新Python版本在EzRead项目的主文件EzRead.py中
https://github.com/lqik2004/EzRead