[prev in list] [next in list] [prev in thread] [next in thread] 

List:       kde-commits
Subject:    [calligra/export-epub-moji] filters/words/epub: Move all chapter generation to convertContent.
From:       Inge Wallin <inge () lysator ! liu ! se>
Date:       2012-07-31 23:58:52
Message-ID: 20120731235852.CCF1DA60A6 () git ! kde ! org
[Download RAW message or body]

Git commit 6959e6060e0fa893e15ac3100d42fec2e18691db by Inge Wallin.
Committed on 01/08/2012 at 01:58.
Pushed by ingwa into branch 'export-epub-moji'.

Move all chapter generation to convertContent.

This simplifies the code a lot!

M  +5    -28   filters/words/epub/exportepub2.cpp
M  +50   -34   filters/words/epub/htmlconvert.cpp
M  +2    -4    filters/words/epub/htmlconvert.h

http://commits.kde.org/calligra/6959e6060e0fa893e15ac3100d42fec2e18691db

diff --git a/filters/words/epub/exportepub2.cpp b/filters/words/epub/exportepub2.cpp
index 52d581b..b6b91ed 100644
--- a/filters/words/epub/exportepub2.cpp
+++ b/filters/words/epub/exportepub2.cpp
@@ -111,34 +111,11 @@ KoFilter::ConversionStatus ExportEpub2::convert(const \
QByteArray &from, const QB  }
     epub.addContentFile("stylesheet", "OEBPS/styleSheet.css", "text/css", \
cssContent);  
-    // Create html contents
-    QByteArray htmlContent;
-    int startNode = 0;
-    int id = 1;
-    bool moreFiles = false;
-    QString prefix = "body";    // FIXME: Change to "chapter". Beware of hardcoded \
                "body" here and there.
-    while (1) {
-        // first: conversion status. Must be OK, otherwise we break off the process
-        // second: true if there is more to convert
-        QPair<KoFilter::ConversionStatus, bool> pair
-            = convertContent(odfStore, m_meta, &epub, htmlContent
-                             , m_pBreakStylesList, startNode, moreFiles);
-
-        if (pair.first != KoFilter::OK) {
-            delete odfStore;
-            return status;
-        }
-
-        QString fileId = prefix + QString::number(id);
-        QString fileName = "OEBPS/" + fileId + ".html";
-        epub.addContentFile(fileId, fileName, "application/xhtml+xml", htmlContent);
-        if (!pair.second) { 
-            break;
-        }
-
-        htmlContent.clear();
-        moreFiles = true;
-        id++;
+    // Create html contents.
+    status = convertContent(odfStore, m_meta, &epub, m_pBreakStylesList);
+    if (status != KoFilter::OK) {
+        delete odfStore;
+        return status;
     }
 
     // ----------------------------------------------------------------
diff --git a/filters/words/epub/htmlconvert.cpp b/filters/words/epub/htmlconvert.cpp
index 78e3372..fc88fd7 100644
--- a/filters/words/epub/htmlconvert.cpp
+++ b/filters/words/epub/htmlconvert.cpp
@@ -363,28 +363,22 @@ KoFilter::ConversionStatus stylesParse(KoStore *odfStore,
 }
 
 
-QPair<KoFilter::ConversionStatus, bool> convertContent(KoStore *odfStore, \
QHash<QString, QString> &metaData, +KoFilter::ConversionStatus convertContent(KoStore \
*odfStore, QHash<QString, QString> &metaData,  EpubFile *epub,
-                                          QByteArray &result, QList<QString> \
                &breakBeforeStylesList,
-                                                       int &startNode, bool \
flagMoreHtml) +                                          QList<QString> \
&breakBeforeStylesList)  {
     if (!odfStore->open("content.xml")) {
         kDebug(30517) << "Can not open content.html .";
-        return qMakePair(KoFilter::FileNotFound, false);
+        return KoFilter::FileNotFound;
     }
 
-    QBuffer outBuf(&result);
-    KoXmlWriter *bodyWriter = new KoXmlWriter(&outBuf);
-
-    bodyWriter->startElement("html");
-
-    createHtmlHead(bodyWriter, metaData);
+    QByteArray htmlContent;
+    QBuffer *outBuf = new QBuffer(&htmlContent);
+    KoXmlWriter *bodyWriter = new KoXmlWriter(outBuf);
 
     // ----------------------------------------------------------------
     // Parse body from content.xml
 
-    bodyWriter->startElement("body");
-
     QDomDocument doc("content");
     QString errorMsg;
     int errorLine, errorColumn;
@@ -392,7 +386,7 @@ QPair<KoFilter::ConversionStatus, bool> convertContent(KoStore \
*odfStore, QHash<  kDebug() << "Error occured while parsing content.xml "
                  << errorMsg << " in Line: " << errorLine
                  << " Column: " << errorColumn;
-        return qMakePair(KoFilter::ParsingError, false);
+        return KoFilter::ParsingError;
     }
 
     QDomNode currentNode;
@@ -404,20 +398,19 @@ QPair<KoFilter::ConversionStatus, bool> convertContent(KoStore \
*odfStore, QHash<  currentNode = doc.documentElement().firstChild();
     currentNode = currentNode.nextSibling();
     currentNode = currentNode.firstChild().firstChild().nextSibling();
-
-    // Set currentNode to the node we want to operate on next. 
-    if (flagMoreHtml) {
-        for (int i = 0; i <startNode; i++){
-            currentNode = currentNode.nextSibling();
-        }
-
-        sibl = startNode;
-    }
     nodeElement = currentNode.toElement();
 
-    QDomNode pNode;
+    // Write the beginning of the output.
+    bodyWriter->startElement("html");
+    bodyWriter->addAttribute("xmlns", "http://www.w3.org/1999/xhtml");
+    createHtmlHead(bodyWriter, metaData);
+    bodyWriter->startElement("body");
 
-    bool pFlag = false;  // True if inside a top level <p> tag.
+    QString prefix = "body";    // FIXME: Change to "chapter". Beware of hardcoded \
"body" here and there. +    int      id = 1;            // Number of current output \
chapter. +    bool     pFlag = false;     // True if inside a top level <p> tag.
+    QDomNode pNode;
+    bool     firstNode = true;  // so that we don't create a new chapter at the \
first paragraph.  while (!nodeElement.isNull() || pFlag) {
 
         //kDebug(30517) << nodeElement.tagName() <<pFlag;
@@ -426,19 +419,34 @@ QPair<KoFilter::ConversionStatus, bool> convertContent(KoStore \
                *odfStore, QHash<
         if (nodeElement.tagName() == "p" || nodeElement.tagName() =="h") {
 
             // A break-before in the style means create a new chapter here,
-            // but only if it is a top-level paragraph.
+            // but only if it is a top-level paragraph and not at the very first \
                node.
             if (breakBeforeStylesList.contains(nodeElement.attribute("style-name"))
-                && startNode != sibl
+                && !firstNode
                 && !pFlag)
             {
-                // This paragraph is at top level so we should close the html file \
and return. +                // This paragraph is at top level so we should close
+                // the html file and start on the next file.
                 bodyWriter->endElement();
                 bodyWriter->endElement();
-                // Save this node for next html file.
-                startNode = sibl;
-                // Return
-                odfStore->close();
-                return qMakePair(KoFilter::OK, true);
+
+                // Write output file to the epub object.
+                QString fileId = prefix + QString::number(id);
+                QString fileName = "OEBPS/" + fileId + ".html";
+                epub->addContentFile(fileId, fileName, "application/xhtml+xml", \
htmlContent); +
+                // Prepare for the next file.
+                htmlContent.clear();
+                delete bodyWriter;
+                delete outBuf;
+                outBuf = new QBuffer(&htmlContent);
+                bodyWriter = new KoXmlWriter(outBuf);
+                id++;
+
+                // Write the beginning of the output for the next file.
+                bodyWriter->startElement("html");
+                bodyWriter->addAttribute("xmlns", "http://www.w3.org/1999/xhtml");
+                createHtmlHead(bodyWriter, metaData);
+                bodyWriter->startElement("body");
             }
 
             if (nodeElement.tagName() == "p")
@@ -600,19 +608,27 @@ QPair<KoFilter::ConversionStatus, bool> convertContent(KoStore \
*odfStore, QHash<  
         currentNode = currentNode.nextSibling();
         nodeElement = currentNode.toElement();
+        firstNode = false;
     }
 
     bodyWriter->endElement(); // body
     bodyWriter->endElement(); // html
 
+    // Write output of the last file to the epub object.
+    QString fileId = prefix + QString::number(id);
+    QString fileName = "OEBPS/" + fileId + ".html";
+    epub->addContentFile(fileId, fileName, "application/xhtml+xml", htmlContent);
+
+    delete bodyWriter;
+    delete outBuf;
+
     odfStore->close();
-    return qMakePair(KoFilter::OK, false);
+    return KoFilter::OK;
 }
 
 
 void createHtmlHead(KoXmlWriter *writer, QHash<QString, QString> &metaData)
 {
-    writer->addAttribute("xmlns", "http://www.w3.org/1999/xhtml");
     writer->startElement("head");
 
     writer->startElement("title");
diff --git a/filters/words/epub/htmlconvert.h b/filters/words/epub/htmlconvert.h
index c30c465..c8b84a0 100644
--- a/filters/words/epub/htmlconvert.h
+++ b/filters/words/epub/htmlconvert.h
@@ -36,11 +36,9 @@ KoFilter::ConversionStatus stylesParse(KoStore *odfStore,
                                        QHash<QString, QList<QString> > &styles,
                                        QHash<QString, QString> &stylesInUse,
                                        QList<QString> &breakBeforeStylesList);
-QPair<KoFilter::ConversionStatus, bool> convertContent(KoStore *input, \
QHash<QString, QString> &metaData, +KoFilter::ConversionStatus convertContent(KoStore \
*odfStore, QHash<QString, QString> &metaData,  EpubFile *epub,
-                                          QByteArray &result,
-                                          QList<QString> &breakBeforeStylesList,
-                                          int &startNode, bool flagMoreHtml);
+                                          QList<QString> &breakBeforeStylesList);
 
 #endif // HTMLCONVERT_H
 


[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic