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

List:       kde-commits
Subject:    [calligra/export-epub-moji] filters/words/epub: Change the way to check the tags and now we support
From:       Mojtaba Shahi Senobari <mojtaba.shahi3000 () gmail ! com>
Date:       2012-08-01 11:16:18
Message-ID: 20120801111618.D10F6A6094 () git ! kde ! org
[Download RAW message or body]

Git commit 2df40e1040b7ed66654ca25bd9ddc850fbb4893a by Mojtaba Shahi Senobari.
Committed on 01/08/2012 at 13:12.
Pushed by mojtabashahi into branch 'export-epub-moji'.

Change the way to check the tags and now we support table in table.

M  +130  -186  filters/words/epub/htmlconvert.cpp

http://commits.kde.org/calligra/2df40e1040b7ed66654ca25bd9ddc850fbb4893a

diff --git a/filters/words/epub/htmlconvert.cpp b/filters/words/epub/htmlconvert.cpp
index fc88fd7..3c4a7b8 100644
--- a/filters/words/epub/htmlconvert.cpp
+++ b/filters/words/epub/htmlconvert.cpp
@@ -40,6 +40,12 @@
 
 static void createHtmlHead(KoXmlWriter *writer, QHash<QString, QString> &metaData);
 static void handleTagTable(QDomElement &nodeElement, KoXmlWriter *bodyWriter);
+static void handleTagImage(QDomElement &nodeElement, KoXmlWriter *bodyWriter);
+static void handleTagP(QDomElement &nodeElement, KoXmlWriter *bodyWriter);
+static void handleCharacterData(QDomNode &node, KoXmlWriter *bodyWriter);
+static void handleTagSpan(QDomElement &nodeElement, KoXmlWriter *bodyWriter);
+static void handleTagPageBreak(QDomElement &nodeElement, KoXmlWriter *bodyWriter);
+static void handleTagH(QDomElement &nodeElement, KoXmlWriter *bodyWriter);
 
 
 KoFilter::ConversionStatus stylesParse(KoStore *odfStore,
@@ -391,7 +397,6 @@ KoFilter::ConversionStatus convertContent(KoStore *odfStore, \
QHash<QString, QStr  
     QDomNode currentNode;
     QDomElement nodeElement;    // currentNode as Element
-    int sibl = 0;
 
     // Find the first node that has actual content.
     // FIXME: This should be made more robust.
@@ -408,21 +413,16 @@ KoFilter::ConversionStatus convertContent(KoStore *odfStore, \
QHash<QString, QStr  
     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;
+    while (!nodeElement.isNull()) {
 
-        //  Check for tag table
-        if (nodeElement.tagName() == "p" || nodeElement.tagName() =="h") {
+        //kDebug(30517) << nodeElement.tagName() <<pFlag;
+        if (nodeElement.tagName() == "p") {
 
             // A break-before in the style means create a new chapter here,
             // but only if it is a top-level paragraph and not at the very first \
                node.
-            if (breakBeforeStylesList.contains(nodeElement.attribute("style-name"))
-                && !firstNode
-                && !pFlag)
+            if (breakBeforeStylesList.contains(nodeElement.attribute("style-name")))
+
             {
                 // This paragraph is at top level so we should close
                 // the html file and start on the next file.
@@ -449,166 +449,30 @@ KoFilter::ConversionStatus convertContent(KoStore *odfStore, \
QHash<QString, QStr  bodyWriter->startElement("body");
             }
 
-            if (nodeElement.tagName() == "p")
-                bodyWriter->startElement("p");
-            else
-                bodyWriter->startElement("h1");
-
-            if (nodeElement.hasAttribute("style-name")) {
-                QString styleClass = nodeElement.attribute("style-name");
-                bodyWriter->addAttribute("class", styleClass);
-            }
-
-            QDomNode node = currentNode.firstChild();
-
-            if (node.isCharacterData()) {
-                QDomCharacterData charData = node.toCharacterData();
-                pFlag = true;
-                bodyWriter->addTextNode(charData.data());
-                pNode = currentNode;
-                currentNode = node.nextSibling();
-                nodeElement = currentNode.toElement();
-                continue;
-            }
-            else if (node.isElement()) {
-                pFlag = true;
-                pNode = currentNode;
-                currentNode = currentNode.firstChild();
-                nodeElement = currentNode.toElement();
-                continue;
-            }
-            else {
-                QString text = nodeElement.text();
-                bodyWriter->addTextNode(text);
-            }
+            handleTagP(nodeElement, bodyWriter);
         }
-        else if (nodeElement.tagName() == "span") {
-
-            // This is for span tag about font-style and font-weight
-            // that i should add them with tags b and i and should be other
-            // tags what should i do? Should i check all ?
-            bodyWriter->startElement("span");
-
-            if (nodeElement.hasAttribute("style-name")) {
-                QString styleClass = nodeElement.attribute("style-name");
-                bodyWriter->addAttribute("class", styleClass);
-            }
-
-            QString text = nodeElement.text();
-            bodyWriter->addTextSpan(text);
-            bodyWriter->endElement(); // span
-
-            QDomNode node = currentNode.nextSibling();
-
-            if (node.isCharacterData()) {
-                //kDebug(30517) <<"isCharacterData";
-                QDomCharacterData charData = node.toCharacterData();
-                pFlag = true;
-                bodyWriter->addTextNode(charData.data());
-                currentNode = node.nextSibling();
-                nodeElement = currentNode.toElement();
-                continue;
-            }
-            else if (node.isElement()) {
-                pFlag = true;
-                currentNode = currentNode.firstChild();
-                nodeElement = currentNode.toElement();
-                continue;
-            }
-
-            currentNode = pNode;
-            nodeElement = currentNode.toElement();
+        else if (nodeElement.tagName() == "h") {
+            handleTagH(nodeElement, bodyWriter);
         }
-        else if (nodeElement.tagName() == "s") {
-           currentNode = currentNode.nextSibling();
-           nodeElement = currentNode.toElement();
+        else if (nodeElement.tagName() == "span") {
+            handleTagSpan(nodeElement, bodyWriter);
         }
         else if (nodeElement.tagName() == "table") {
             // Handle table
-
             handleTagTable(nodeElement, bodyWriter);
-
-            // FIXME: I don't understand this at all. /IW
-            if (pFlag) {
-                if (currentNode.nextSibling().isNull()) {
-                    currentNode = pNode;
-                    nodeElement = currentNode.toElement();
-                    pFlag = false;
-                }
-            }
-        } 
+        }
         else if (nodeElement.tagName() == "frame")  {
             // Handle frame
-
-            if (!pFlag) {
-                bodyWriter->startElement("div");
-            }
-
-            bodyWriter->startElement("img");
-            bodyWriter->addAttribute("class", nodeElement.attribute("style-name"));
-            bodyWriter->addAttribute("alt", "(No Description)");
-
-            // Check image tag to find image source
-            QDomElement imgElement = currentNode.firstChild().toElement();
-
-            QString imgSrc = imgElement.attribute("href").section('/', 1); // I like \
                section :)
-            bodyWriter->addAttribute("src", imgSrc);
-            //bodyWriter->endElement();
-
-            if (pFlag) {
-                // FIXME: What's going on here?? /IW
-                if (currentNode.nextSibling().isNull()) {
-                    bodyWriter->endElement(); // Why isn't the img tag *always* \
                ended?
-                    currentNode = pNode;
-                    nodeElement = currentNode.toElement();
-                    pFlag = false;
-                }
-            }
-            else
-                bodyWriter->endElement(); // div
+            bodyWriter->startElement("div");
+            handleTagImage(nodeElement, bodyWriter);
+            bodyWriter->endElement(); // end div
         }
         else if (nodeElement.tagName() == "soft-page-break") {
-            bodyWriter->addTextNode(nodeElement.text().toUtf8());
-            if (pFlag) {
-                if (currentNode.nextSibling().isNull()) {
-                   bodyWriter->endElement();
-                    currentNode = pNode;
-                    nodeElement = currentNode.toElement();
-                    pFlag = false;
-                }
-            }
-        }
-        // Ignore unknown tag
-        else {
-            if (!pFlag) {
-                currentNode = currentNode.nextSibling();
-                nodeElement = currentNode.toElement();
-                continue;
-            }
-        }
-
-        if (pFlag) {
-            currentNode = pNode;
-            nodeElement = currentNode.toElement();
-            pFlag = false;
+            handleTagPageBreak(nodeElement, bodyWriter);
         }
 
-        // FIXME: This is a bad optimization that makes the code super
-        //        difficult to follow!  Wherever there is a
-        //        startElement() there should also be an
-        //        endElement(). The calls to endElement() should not
-        //        be optimized this way. It leads to bugs. For
-        //        instance, the case "Ignore unknown tag" above has no
-        //        call to startElement() at all. Yet we have this here
-        //        endElement(). :(
-        bodyWriter->endElement(); // tagName()
-
-        if (!pFlag)
-            sibl++;
-
         currentNode = currentNode.nextSibling();
         nodeElement = currentNode.toElement();
-        firstNode = false;
     }
 
     bodyWriter->endElement(); // body
@@ -670,8 +534,7 @@ void createHtmlHead(KoXmlWriter *writer, QHash<QString, QString> \
&metaData)  void handleTagTable(QDomElement &nodeElement, KoXmlWriter *bodyWriter)
 {
     bodyWriter->startElement("table");
-    QString styleClass = nodeElement.attribute("style-name");
-    bodyWriter->addAttribute("class", styleClass);
+    bodyWriter->addAttribute("class", nodeElement.attribute("style-name"));
     bodyWriter->addAttribute("border", "1");
 
     QDomNode tableNode = nodeElement.firstChild();
@@ -694,7 +557,7 @@ void handleTagTable(QDomElement &nodeElement, KoXmlWriter \
*bodyWriter)  
     //===== table-row ======
 
-    while (tableElement.tagName() == "table-row") { // FIXME: we dont have a tag for \
table-column. +    while (tableElement.tagName() == "table-row") {
         bodyWriter->startElement("tr");
 
         // ===== table-cell ======
@@ -710,31 +573,18 @@ void handleTagTable(QDomElement &nodeElement, KoXmlWriter \
*bodyWriter)  QDomElement cellTextElement = cellTextNode.toElement();
 
             while (!cellTextElement.isNull()) {
-                bodyWriter->startElement(cellTextElement.tagName().toUtf8());
-                bodyWriter->addAttribute("class", \
                cellTextElement.attribute("style-name"));
-
-                QDomNode node = cellTextNode.firstChild();
-                QDomElement elemet = node.toElement();
-
-                // This is for span tag about font-style and font-weight
-                // that i should add them with tagz b and i and should be other
-                // tags what should i do? shoudl i check all ?
-                if (node.isElement()) {
-                    if (elemet.hasAttribute("style-name")) {
-                        bodyWriter->startElement("span");
-                        bodyWriter->addAttribute("class", \
                elemet.attribute("style-name"));
-                        QString text = cellTextElement.text();
-                        bodyWriter->addTextNode(text);
-                        bodyWriter->endElement(); // span
-                    }
-                }
-                else {
-                    bodyWriter->addTextNode(cellTextElement.text());
-                }
-                bodyWriter->endElement(); // tagName(p)
+                QString cellTag = cellTextElement.tagName();
+
+                if (cellTag == "p")
+                    handleTagP(cellTextElement, bodyWriter);
+                else if (cellTag == "table")
+                    handleTagTable(cellTextElement, bodyWriter);
+                else if (cellTag == "frame")
+                    handleTagImage(cellTextElement, bodyWriter);
+
                 cellTextNode = cellTextNode.nextSibling();
                 cellTextElement = cellTextNode.toElement();
-            } // end while write tag p
+            }
             bodyWriter->endElement(); // td
 
             cellNode = cellNode.nextSibling();
@@ -746,7 +596,101 @@ void handleTagTable(QDomElement &nodeElement, KoXmlWriter \
*bodyWriter)  tableElement = tableNode.toElement();
     } // end while write tag row
 
-    // FIXME: Apparently the tag is ended in the main loop. 
-    //        That "optimization" is bad and should be removed.
-    //bodyWriter->endElement(); // table
+    bodyWriter->endElement();
+}
+
+void handleTagImage(QDomElement &nodeElement, KoXmlWriter *bodyWriter)
+{
+    bodyWriter->startElement("img");
+    bodyWriter->addAttribute("class", nodeElement.attribute("style-name"));
+    bodyWriter->addAttribute("alt", "(No Description)");
+
+    // Check image tag to find image source
+    QDomElement imgElement = nodeElement.firstChild().toElement();
+
+    QString imgSrc = imgElement.attribute("href").section('/', 1);
+    bodyWriter->addAttribute("src", imgSrc);
+    bodyWriter->endElement(); // end img
+}
+
+void handleTagP(QDomElement &nodeElement, KoXmlWriter *bodyWriter)
+{
+    bodyWriter->startElement("p");
+
+    if (nodeElement.hasAttribute("style-name")) {
+        QString styleClass = nodeElement.attribute("style-name");
+        bodyWriter->addAttribute("class", styleClass);
+    }
+
+    QDomNode node = nodeElement.firstChild();
+    QDomElement element = node.toElement();
+    // We have characterData or image or span or s  or soft-page break in a tag p
+    // FIXME: we should add if there are more tags.
+
+    while (!node.isNull()) {
+
+        if (node.isCharacterData())
+            handleCharacterData(node, bodyWriter);
+        else if (element.tagName() == "span")
+            handleTagSpan(element, bodyWriter);
+        else if (element.tagName() == "frame")
+            handleTagImage(element, bodyWriter);
+        else if (element.tagName() == "soft-page-break")
+            handleTagPageBreak(element, bodyWriter);
+
+        node = node.nextSibling();
+        element = node.toElement();
+    }
+    bodyWriter->endElement();
+}
+
+void handleCharacterData(QDomNode &node, KoXmlWriter *bodyWriter)
+{
+    QDomCharacterData charData = node.toCharacterData();
+    bodyWriter->addTextNode(charData.data());
+}
+
+void handleTagSpan(QDomElement &nodeElement, KoXmlWriter *bodyWriter)
+{
+    bodyWriter->startElement("span");
+    if (nodeElement.hasAttribute("style-name"))
+        bodyWriter->addAttribute("class",  nodeElement.attribute("style-name"));
+    bodyWriter->addTextSpan(nodeElement.text());
+    bodyWriter->endElement(); // span
+}
+
+void handleTagPageBreak(QDomElement &nodeElement, KoXmlWriter *bodyWriter)
+{
+    bodyWriter->addTextNode(nodeElement.text().toUtf8());
+}
+
+void handleTagH(QDomElement &nodeElement, KoXmlWriter *bodyWriter)
+{
+    bodyWriter->startElement("h1");
+
+    if (nodeElement.hasAttribute("style-name")) {
+        QString styleClass = nodeElement.attribute("style-name");
+        bodyWriter->addAttribute("class", styleClass);
+    }
+
+    QDomNode node = nodeElement.firstChild();
+    QDomElement element = node.toElement();
+    // We have characterData or image or span or s  or soft-page break in a tag p
+    // FIXME: we should add if there are more tags.
+
+    while (!node.isNull()) {
+
+        if (node.isCharacterData())
+            handleCharacterData(node, bodyWriter);
+        else if (element.tagName() == "span")
+            handleTagSpan(element, bodyWriter);
+        else if (element.tagName() == "frame")
+            handleTagImage(element, bodyWriter);
+        else if (element.tagName() == "soft-page-break")
+            handleTagPageBreak(element, bodyWriter);
+
+        node = node.nextSibling();
+        element = node.toElement();
+    }
+    bodyWriter->endElement();
 }


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

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