利用kindlegen实现txt格式小说转换为mobi格式小说(C++实现)

0
8

   一直以来喜欢在kindle上看小说,kindle不伤眼,也可以帮助控制玩手机的时间。但在kindle上看txt格式的网络小说就很头疼了,这类小说在kindle上是没有目录的,而且篇幅巨长。所以一直以来我都想加上目录。于是就有了这篇文章。

  在这里,要想加上目录,就要将txt格式的小说转换为mobi格式。我借助了kindlegen,它的一些语法不再这里详说了,给个示意图,如下:

  

  总思路就是生成html和ncx、opf文件,代码如下:

  1.read.cpp

  

#include <iostream>
#include <string>
#include <fstream>
#include <vector>
#include <regex>
#include <windows.h>
#include "productHtml.h"

using namespace std;

int main(int argc,char * * argv)
{
    fstream fin(argv[1],fstream::in);
    string test="";
    vector<string> vec_menu;
    vector<string> vec_text;
    string rs="^[0-9]";
    regex expression(rs);
    smatch wm;
    string str_text="<p>";

    while(getline(fin,test))
    {
        if (regex_search (test,wm,expression)) {
            vec_menu.push_back(test);
            //cout<<str_text<<endl;
            str_text+="</p>";
            vec_text.push_back(str_text);
            str_text="<p>";
        }
        else
        {
            str_text+=test;
            str_text+="<br/>";
        }
    }
    vec_text.push_back(str_text);
    cout<<vec_menu.size()<<" "<<vec_text.size()<<endl;
    for(int i=0;i<vec_text.size();i++)
    {
        /*
        cout<<vec_menu[i]<<endl;
        cout<<vec_text[i+1]<<endl;
        */
        string str=vec_text[i];
        string str_name=i==0?"前言":vec_menu[i-1];
        productHtml(str_name,str,i);
    }
    productNcx(vec_menu);
    productOpf(vec_menu);
    cout<<"finished"<<endl;
    fin.close();

    int ret=(int)ShellExecute(NULL,"open","kindlegen.exe","test.opf",NULL,SW_SHOWNORMAL);
    cout<<"ret: "<<ret<<endl;
    return 0;
}

2.productHtml.h

#include <iostream>
#include <string>
#include <fstream>

using namespace std;

void productHtml(string str_name,string str_text,int no)
{
    string fileName="./chapter"+to_string(no)+".html";
    fstream fout(fileName,ios::out);
    fout<<"<html>"<<endl<<"<head>"<<endl<<"<meta http-equiv=\"content-type\" content=\"text/html; charset=utf-8\">"
        <<endl<<"<link rel=\"stylesheet\" type=\"text/css\" href=\"online.css\">"
        <<endl<<"</head>"<<endl<<"<body>";
    fout<<"<title>"<<str_name<<"</title>"<<endl<<str_text;
    fout<<" </body>"<<endl<<"</html>";

    fout.close();
}

void productNcx(vector<string> & menu_name)
{

    fstream fout("./menu.ncx",ios::out);
    fout<<"<?xml version=\"1.0\" encoding=\"UTF-8\"?>"<<endl<<"<!DOCTYPE ncx PUBLIC \"-//NISO//DTD ncx 2005-1//EN\" \"http://www.daisy.org/z3986/2005/ncx-2005-1.dtd\">"
<<endl<<"<ncx xmlns=\"http://www.daisy.org/z3986/2005/ncx/\" version=\"2005-1\" xml:lang=\"en-US\">"<<endl<<"<head>"<<endl<<
"<meta name=\"dtb:uid\" content=\"BookId\"/>"<<endl<<
"<meta name=\"dtb:depth\" content=\"2\"/>"<<endl<<
"<meta name=\"dtb:totalPageCount\" content=\"0\"/>"<<endl<<
"<meta name=\"dtb:maxPageNumber\" content=\"0\"/>"<<endl<<
"</head>"<<endl<<
"<docTitle><text>藏地密码</text></docTitle>"<<endl<<
"<docAuthor><text>何马</text></docAuthor>"<<endl<<
    "<navMap>";
fout<<"<navPoint class=\"toc\" id=qianyan playOrder=\"1\">"<<endl<<
                "<navLabel>"<<endl<<
                        "<text>前言</text>"<<endl<<
                "</navLabel>"<<endl<<
                "<content src=\"chapter0.html\"/>"<<endl<<
        "</navPoint>";
    for(int i=0;i<menu_name.size();i++)
    {
        fout<<"<navPoint class=\"toc\" id=\""+menu_name[i]+"\" playOrder=\"1\">"<<endl<<
                    "<navLabel>"<<endl<<
                            "<text>"+menu_name[i]+"</text>"<<endl<<
                    "</navLabel>"<<endl<<
                    "<content src=\"chapter"+to_string(i+1)+".html\"/>"<<endl<<
            "</navPoint>";
    }
    fout<<" </navMap>"<<endl<<"</ncx>";
    fout.close();
    cout<<"finished ncx"<<endl;
}

void productOpf(vector<string> & menuName)
{
    fstream fout("./test.opf",fstream::out);
    fout<<"<package xmlns=\"http://www.idpf.org/2007/opf\" version=\"2.0\" unique-identifier=\"BookId\">"<<endl<<
    "<metadata xmlns:dc=\"http://purl.org/dc/elements/1.1/\" xmlns:opf=\"http://www.idpf.org/2007/opf\">"<<endl<<
    "<dc:title>电子书标题</dc:title>"<<endl<<
    "<dc:language>en-us</dc:language>"<<endl<<
    "</metadata>"<<endl<<
    "<manifest>";
    fout<<"<item id=\"My_Table_of_Contents\" media-type=\"application/x-dtbncx+xml\" href=\"menu.ncx\"/>"<<endl;
    for(int i=0;i<menuName.size()+1;i++)
    {
        fout<<"<item id=\"item"+to_string(i)+"\" media-type=\"application/xhtml+xml\" href=\"chapter"+to_string(i)+".html\"/>"<<endl;
    }
    fout<<"</manifest>"<<endl<<"<spine toc=\"My_Table_of_Contents\">";
    for(int i=0;i<menuName.size()+1;i++)
    {
        fout<<"<itemref idref=\"item"+to_string(i)+"\"/>"<<endl;
    }
    fout<<" </spine>"<<endl<<"</package>";
    fout.close();
    cout<<"finished opf"<<endl;
}

git地址如下:https://github.com/JsonZhangAA/Txt2Mobi,有兴趣的朋友,大家一起改良。

<

发布回复

请输入评论!
请输入你的名字