From ca2b91cb4a898a9b4dee4415562b383d30e4b1a6 Mon Sep 17 00:00:00 2001 From: Georgy Litvinov Date: Fri, 24 Jul 2020 14:59:41 +0200 Subject: [PATCH] Refacoted paragraph splitting --- .../java/w2phtml/xhtml/ODFPageSplitter.java | 49 +++++++------------ 1 file changed, 19 insertions(+), 30 deletions(-) diff --git a/src/main/java/w2phtml/xhtml/ODFPageSplitter.java b/src/main/java/w2phtml/xhtml/ODFPageSplitter.java index 500471c..4fccd55 100644 --- a/src/main/java/w2phtml/xhtml/ODFPageSplitter.java +++ b/src/main/java/w2phtml/xhtml/ODFPageSplitter.java @@ -37,9 +37,8 @@ public class ODFPageSplitter { StyleWithProperties style = null; if ((nodeName.equals(TEXT_P) || nodeName.equals(TEXT_H))) { //If SPB not the first node - Node paraFirstPart = handleParagraph(child); - if (paraFirstPart != null){ - onode.insertBefore(paraFirstPart, child); + + if (handleParagraph(child)){ style = ofr.getParStyle(Misc.getAttribute(child, TEXT_STYLE_NAME)); } } else if (nodeName.equals(TABLE_TABLE)) { @@ -149,9 +148,8 @@ public class ODFPageSplitter { } else { String childName = child.getNodeName(); if (childName.equals(TEXT_P)) { - Node paraFirstPart = handleParagraph(child); - if (paraFirstPart != null) { - indexBodyFirstPart.appendChild(paraFirstPart); + if (handleParagraph(child)) { + indexBodyFirstPart.appendChild(child.getPreviousSibling()); } return indexBodyFirstPart; } else @@ -239,7 +237,6 @@ public class ODFPageSplitter { //Node name String nodeName = listItemChild.getNodeName(); if (containsSPB(listItemChild)){ - Node listItemChildFirstPart = listItemChild.cloneNode(false); //Break if SPB if (nodeName.equals(TEXT_SOFT_PAGE_BREAK)) { //Remove SPB.Return result @@ -250,9 +247,8 @@ public class ODFPageSplitter { } } else if (nodeName.equals(TEXT_H) || nodeName.equals(TEXT_P)) { - Node paraFirstPart = handleParagraph(listItemChild); - if (paraFirstPart != null){ - listItemFirstPart.appendChild(paraFirstPart); + if (handleParagraph(listItemChild)){ + listItemFirstPart.appendChild(listItemChild.getPreviousSibling()); dataMoved=true; } } @@ -516,19 +512,17 @@ public class ODFPageSplitter { if (nodeName.equals(TEXT_SOFT_PAGE_BREAK)){ // remove inner soft page break node cellNode.removeChild(cellChildNode); - } else if (nodeName.equals(TEXT_H) || nodeName.equals(TEXT_P)) { - Node paraFirstPart = handleParagraph(cellChildNode); - if (paraFirstPart != null){ - cellFirstPart.appendChild(paraFirstPart); + if (handleParagraph(cellChildNode)){ + cellFirstPart.appendChild(cellChildNode.getPreviousSibling()); dataMoved=true; } } else if (nodeName.equals(TEXT_LIST)) { - Node listFirstPart = cellChildNode.cloneNode(false); - if (handleList(cellChildNode)){ - dataMoved=true; - } - } + if (handleList(cellChildNode)){ + cellFirstPart.appendChild(cellChildNode.getPreviousSibling()); + dataMoved=true; + } + } break; } else { cellFirstPart.appendChild(cellChildNode); @@ -557,15 +551,12 @@ public class ODFPageSplitter { if ((child.getNodeType() == Node.ELEMENT_NODE)) { String nodeName = child.getNodeName(); if (containsSPB(child)){ - Node childFirstPart = child.cloneNode(false); if (nodeName.equals(TEXT_SOFT_PAGE_BREAK)){ // remove inner soft page break node section.removeChild(child); - } else if (nodeName.equals(TEXT_H) || nodeName.equals(TEXT_P)) { - Node paraFirstPart = handleParagraph(child); - if (paraFirstPart != null){ - sectionFirstPart.appendChild(paraFirstPart); + if (handleParagraph(child)){ + sectionFirstPart.appendChild(child.getPreviousSibling()); dataMoved=true; } } else if (nodeName.equals(TEXT_TABLE_OF_CONTENT)) { @@ -620,7 +611,8 @@ public class ODFPageSplitter { Element sectionEl = (Element) section; sectionEl.setAttribute("last", "true"); } - private static Node handleParagraph(Node para) { + private static boolean handleParagraph(Node para) { + Node parent = para.getParentNode(); //System.out.println("handlePara"); Node paraFirstPart = para.cloneNode(false); boolean dataMoved = false; @@ -668,15 +660,12 @@ public class ODFPageSplitter { } addHyphen(para,paraFirstPart); - - if (dataMoved) { removeIndent(para); - return paraFirstPart; + parent.insertBefore(paraFirstPart, para); } - - return null; + return dataMoved; } private static void handleSpan(Node span) {