Conversion of txt novel to mobi novel by using kindlegen (C + + implementation)

Keywords: C++ xml iOS Mobile Windows

I always like to read novels on the kindle. The kindle doesn't hurt my eyes. It can also help control the time of playing mobile phones. But it's a headache to read the online novels in the format of txt on the kindle. Such novels have no catalogue on the kindle, and they are very long. So I always wanted to add a catalog. So there is this article.

In this case, to add a directory, you need to convert a novel in txt format to mobi format. With the help of kindlegen, some of its syntax will not be explained here in detail. Here is a schematic diagram, as follows:

  

 

The general idea is to generate html, ncx and opf files. The code is as follows:

  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?"Preface":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>Tibetan code</text></docTitle>"<<endl<<
"<docAuthor><text>He ma</text></docAuthor>"<<endl<<
    "<navMap>";
fout<<"<navPoint class=\"toc\" id=qianyan playOrder=\"1\">"<<endl<<
                "<navLabel>"<<endl<<
                        "<text>Preface</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>EBook 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 address is as follows: https://github.com/JsonZhangAA/Txt2Mobi , friends who are interested, let's improve together.

Posted by Link on Thu, 30 Jan 2020 06:25:38 -0800