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/export= epub2.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 =3D 0; - int id =3D 1; - bool moreFiles =3D false; - QString prefix =3D "body"; // FIXME: Change to "chapter". Beware of= hardcoded "body" here and there. - while (1) { - // first: conversion status. Must be OK, otherwise we break off th= e process - // second: true if there is more to convert - QPair pair - =3D convertContent(odfStore, m_meta, &epub, htmlContent - , m_pBreakStylesList, startNode, moreFiles); - - if (pair.first !=3D KoFilter::OK) { - delete odfStore; - return status; - } - - QString fileId =3D prefix + QString::number(id); - QString fileName =3D "OEBPS/" + fileId + ".html"; - epub.addContentFile(fileId, fileName, "application/xhtml+xml", htm= lContent); - if (!pair.second) { = - break; - } - - htmlContent.clear(); - moreFiles =3D true; - id++; + // Create html contents. + status =3D convertContent(odfStore, m_meta, &epub, m_pBreakStylesList); + if (status !=3D KoFilter::OK) { + delete odfStore; + return status; } = // ---------------------------------------------------------------- diff --git a/filters/words/epub/htmlconvert.cpp b/filters/words/epub/htmlco= nvert.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 *odfSt= ore, } = = -QPair convertContent(KoStore *odfStore, = QHash &metaData, +KoFilter::ConversionStatus convertContent(KoStore *odfStore, QHash &metaData, EpubFile *epub, - QByteArray &result, QList &breakBeforeStylesList, - int &startNode, boo= l flagMoreHtml) + QList &breakBeforeStyle= sList) { 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 =3D new KoXmlWriter(&outBuf); - - bodyWriter->startElement("html"); - - createHtmlHead(bodyWriter, metaData); + QByteArray htmlContent; + QBuffer *outBuf =3D new QBuffer(&htmlContent); + KoXmlWriter *bodyWriter =3D new KoXmlWriter(outBuf); = // ---------------------------------------------------------------- // Parse body from content.xml = - bodyWriter->startElement("body"); - QDomDocument doc("content"); QString errorMsg; int errorLine, errorColumn; @@ -392,7 +386,7 @@ QPair 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 convertConten= t(KoStore *odfStore, QHash< currentNode =3D doc.documentElement().firstChild(); currentNode =3D currentNode.nextSibling(); currentNode =3D currentNode.firstChild().firstChild().nextSibling(); - - // Set currentNode to the node we want to operate on next. = - if (flagMoreHtml) { - for (int i =3D 0; i startElement("html"); + bodyWriter->addAttribute("xmlns", "http://www.w3.org/1999/xhtml"); + createHtmlHead(bodyWriter, metaData); + bodyWriter->startElement("body"); = - bool pFlag =3D false; // True if inside a top level

tag. + QString prefix =3D "body"; // FIXME: Change to "chapter". Beware of= hardcoded "body" here and there. + int id =3D 1; // Number of current output chapter. + bool pFlag =3D false; // True if inside a top level

tag. + QDomNode pNode; + bool firstNode =3D true; // so that we don't create a new chapter= at the first paragraph. while (!nodeElement.isNull() || pFlag) { = //kDebug(30517) << nodeElement.tagName() < convertConten= t(KoStore *odfStore, QHash< if (nodeElement.tagName() =3D=3D "p" || nodeElement.tagName() =3D= =3D"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("styl= e-name")) - && startNode !=3D sibl + && !firstNode && !pFlag) { - // This paragraph is at top level so we should close the h= tml 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 =3D sibl; - // Return - odfStore->close(); - return qMakePair(KoFilter::OK, true); + + // Write output file to the epub object. + QString fileId =3D prefix + QString::number(id); + QString fileName =3D "OEBPS/" + fileId + ".html"; + epub->addContentFile(fileId, fileName, "application/xhtml+= xml", htmlContent); + + // Prepare for the next file. + htmlContent.clear(); + delete bodyWriter; + delete outBuf; + outBuf =3D new QBuffer(&htmlContent); + bodyWriter =3D 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() =3D=3D "p") @@ -600,19 +608,27 @@ QPair convertConten= t(KoStore *odfStore, QHash< = currentNode =3D currentNode.nextSibling(); nodeElement =3D currentNode.toElement(); + firstNode =3D false; } = bodyWriter->endElement(); // body bodyWriter->endElement(); // html = + // Write output of the last file to the epub object. + QString fileId =3D prefix + QString::number(id); + QString fileName =3D "OEBPS/" + fileId + ".html"; + epub->addContentFile(fileId, fileName, "application/xhtml+xml", htmlCo= ntent); + + delete bodyWriter; + delete outBuf; + odfStore->close(); - return qMakePair(KoFilter::OK, false); + return KoFilter::OK; } = = void createHtmlHead(KoXmlWriter *writer, QHash &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/htmlconv= ert.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 > &st= yles, QHash &stylesInUs= e, QList &breakBeforeStylesLi= st); -QPair convertContent(KoStore *input, QHa= sh &metaData, +KoFilter::ConversionStatus convertContent(KoStore *odfStore, QHash &metaData, EpubFile *epub, - QByteArray &result, - QList &breakBeforeStyle= sList, - int &startNode, bool flagMoreHtm= l); + QList &breakBeforeStyle= sList); = #endif // HTMLCONVERT_H =20