From eb479e7f62218f538a4ea37ce16152a921e7a159 Mon Sep 17 00:00:00 2001 From: George Litvinov Date: Mon, 22 Apr 2019 16:04:39 +0300 Subject: [PATCH] Split files by page --- src/main/java/writer2latex/util/Misc.java | 11 --- .../java/writer2latex/xhtml/Converter.java | 2 +- .../writer2latex/xhtml/DocumentSeparator.java | 85 ++++++++++++++----- .../writer2latex/xhtml/TextConverter.java | 57 ++++++------- .../java/writer2latex/xhtml/XhtmlConfig.java | 6 +- 5 files changed, 100 insertions(+), 61 deletions(-) diff --git a/src/main/java/writer2latex/util/Misc.java b/src/main/java/writer2latex/util/Misc.java index 48334d1..6e1755c 100644 --- a/src/main/java/writer2latex/util/Misc.java +++ b/src/main/java/writer2latex/util/Misc.java @@ -321,17 +321,6 @@ public class Misc{ return false; } - public static boolean isElement(String name, Node node) { - - if (!isElement(node)){ - return false; - } - - if (((Element) node).getTagName().equals(name)){ - return true; - } - return false; - } /* Utility method to determine if a Node is a specific Element */ diff --git a/src/main/java/writer2latex/xhtml/Converter.java b/src/main/java/writer2latex/xhtml/Converter.java index bb586da..bbb2a03 100644 --- a/src/main/java/writer2latex/xhtml/Converter.java +++ b/src/main/java/writer2latex/xhtml/Converter.java @@ -644,7 +644,7 @@ public class Converter extends ConverterBase { htmlDoc = new XhtmlDocument(getOutFileName(++nOutFileIndex,false),nType); htmlDoc.setConfig(config); if (template!=null) { htmlDoc.readFromTemplate(template); } - else if (bNeedHeaderFooter) { htmlDoc.createHeaderFooter(); } + /*else if (bNeedHeaderFooter) { htmlDoc.createHeaderFooter(); }*/ outFiles.add(nOutFileIndex,htmlDoc); converterResult.addDocument(htmlDoc); diff --git a/src/main/java/writer2latex/xhtml/DocumentSeparator.java b/src/main/java/writer2latex/xhtml/DocumentSeparator.java index 96d877f..af28216 100644 --- a/src/main/java/writer2latex/xhtml/DocumentSeparator.java +++ b/src/main/java/writer2latex/xhtml/DocumentSeparator.java @@ -20,7 +20,9 @@ public class DocumentSeparator { private static final String SECTIONS = "sections"; private static final String DIV = "div"; private int splitLevel = 0; - + private boolean splitByPages = true; + private int lastSplitPageNum = 1; + private static LinkedList headerStack = new LinkedList(); private static boolean pageOpened = false; //headings none @@ -29,6 +31,7 @@ public class DocumentSeparator { private static String pageSeparation = "sections"; private Converter converter = null; private XhtmlConfig config = null; + private Node prevPageNode = null; public DocumentSeparator(XhtmlConfig config,Converter converter) { this.config = config; @@ -38,7 +41,7 @@ public class DocumentSeparator { splitLevel = config.getXhtmlSplitLevel(); } - protected Node processHeading(Node currentNode, Node hnode, int pageNum) { + protected Node processOutlineLevel(Node currentNode, Node hnode, int pageNum) { //Get outline level String sLevel = getAttribute(currentNode, XMLString.TEXT_OUTLINE_LEVEL); @@ -50,33 +53,53 @@ public class DocumentSeparator { return hnode; } int curLevel = Integer.parseInt(sLevel); + if (needSplitFiles(curLevel,pageNum)){ + prevPageNode = hnode; + } if (pageOpened) { hnode = closePage(hnode); } - - if (headingSeparation.equals(SECTIONS)){ + + if (headingSeparation.equals(SECTIONS)) { closeCommentHeadings(hnode, curLevel); - } - // TODO: If split then move page div to new document. Dont split in text. Make option in menu - if (splitLevel >= curLevel && (converter.outFileHasContent() || converter.getOutFileIndex() == 1)) { - System.out.println("Split happened"); - hnode = converter.nextOutFile(); } - //Place to split headings - - if (headingSeparation.equals(SECTIONS)){ + + if (needSplitFiles(curLevel,pageNum)) { + hnode = splitFiles(hnode); + lastSplitPageNum = pageNum; + } + + if (headingSeparation.equals(SECTIONS)) { openCommentHeading(hnode, title); headerStack.offerFirst(Integer.parseInt(sLevel)); + } - if (!noPageSeparation() && !noHeadingSeparation()){ - hnode = openPage(hnode, pageNum); + if (!noPageSeparation()) { + hnode = openPage(hnode, pageNum); } + + return hnode; } + + private Node splitFiles(Node hnode) { + hnode = converter.nextOutFile(); + return hnode; + } + + private boolean needSplitFiles(int curLevel,int pageNum) { + if (splitLevel >= curLevel && (converter.outFileHasContent() || converter.getOutFileIndex() == 1)) { + if (lastSplitPageNum != pageNum) { + return true; + } + + } + return false; + } - protected static Node processPageBreak(Node currentNode, Node hnode, Integer pageNum){ + protected Node processPageBreak(Node currentNode, Node hnode, Integer pageNum){ if (noPageSeparation()){ return hnode; } @@ -121,11 +144,14 @@ public class DocumentSeparator { return hnode; } - private static Node openPageDiv(Node node,int pageNum){ + private Node openPageDiv(Node node,int pageNum){ if (node == null){ System.out.println("Error: node is null on openPageDiv"); return node; } + if (prevPageNode != null && splitByPages) { + arrangePageDivs(node,pageNum); + } Document doc = node.getOwnerDocument(); Element openBlock = (Element) doc.createElement(DIV); openBlock.setAttribute("class", "pageNum"); @@ -136,9 +162,30 @@ public class DocumentSeparator { return openBlock; } + private void arrangePageDivs(Node node, int pageNum) { + Document newdoc = node.getOwnerDocument(); + Document olddoc = prevPageNode.getOwnerDocument(); + String prevPageNum = getAttribute(prevPageNode, "page"); + + if (prevPageNum != null && prevPageNum.equals(Integer.toString(pageNum))) { + if (isElement(prevPageNode, DIV)) { + Node importedNode = newdoc.importNode(prevPageNode, true); + node.appendChild(importedNode); + Node prevDocContent = prevPageNode.getParentNode(); + if (prevDocContent != null) { + prevDocContent.removeChild(prevPageNode); + } + } + } + + //no more arrange needed till next file separation + prevPageNode = null; + + } + private static Node exitPageDiv(Node node){ - while ( !isRoot(node) && !isElement(DIV, node) ){ + while ( !isRoot(node) && !isElement( node,DIV) ){ node = node.getParentNode(); } Node result = node.getParentNode(); @@ -149,7 +196,7 @@ public class DocumentSeparator { return result; } - private static String getTitle(Node currentNode) { + public static String getTitle(Node currentNode) { Node content = currentNode.cloneNode(true); NodeList contentNodes = content.getChildNodes(); String title = null; @@ -228,7 +275,7 @@ public class DocumentSeparator { pageOpened = false; return hnode; } - protected static Node openPage(Node hnode, Integer pageNum){ + protected Node openPage(Node hnode, Integer pageNum){ if (pageSeparation.equals(SECTIONS)){ openPageComment(hnode, pageNum); pageOpened = true; diff --git a/src/main/java/writer2latex/xhtml/TextConverter.java b/src/main/java/writer2latex/xhtml/TextConverter.java index 27d3259..6836921 100644 --- a/src/main/java/writer2latex/xhtml/TextConverter.java +++ b/src/main/java/writer2latex/xhtml/TextConverter.java @@ -1675,27 +1675,11 @@ public class TextConverter extends ConverterHelper { return hnode; } -/* if (pageTags.equals("none")) { - return hnode; - }*/ Integer newPageNumber = null; - if (style != null) { - // If style:paragraph-properties extists and contain - // style:page-number - String newPageNumberProperty = style.getParProperty(XMLString.STYLE_PAGE_NUMBER, true); - if (newPageNumberProperty != null) { - // Truncate auto and other string values - newPageNumberProperty = newPageNumberProperty.replaceAll("[^0-9]", ""); - if (!newPageNumberProperty.isEmpty()) { - // Save new page number - newPageNumber = Integer.parseInt(newPageNumberProperty); - } - - } - } + newPageNumber = getPageNumber(style, newPageNumber); if (currentMasterPage == null && style != null) { hnode = StartMasterPage(hnode, style, newPageNumber); - hnode = docSep.processHeading(currentNode, hnode, pageNum); + hnode = docSep.processOutlineLevel(currentNode, hnode, pageNum); } else if (checkMasterPageBreak(style) || checkHardBreakBefore(style) || breakBeforeNextNode) { // Insert footnotes inFootnote = true; @@ -1713,7 +1697,7 @@ public class TextConverter extends ConverterHelper { fitPageNumberToMasterPageStyle(); } if (hasOutlineLevel(currentNode)) { - hnode = docSep.processHeading(currentNode, hnode, pageNum); + hnode = docSep.processOutlineLevel(currentNode, hnode, pageNum); } else { hnode = docSep.processPageBreak(currentNode, hnode, pageNum); @@ -1725,15 +1709,8 @@ public class TextConverter extends ConverterHelper { return hnode; } else { - hnode = docSep.processHeading(currentNode, hnode, pageNum); + hnode = docSep.processOutlineLevel(currentNode, hnode, pageNum); } - - - //Create method to process Section -> Something -> Foo-break-before - - /*currentNode.getNextSibling() - if (currentNode.getNextSibling()) - */ if (checkHardBreakAfter(style)) { breakBeforeNextNode = true; @@ -1744,6 +1721,24 @@ public class TextConverter extends ConverterHelper { return hnode; } + private Integer getPageNumber(StyleWithProperties style, Integer newPageNumber) { + if (style != null) { + // If style:paragraph-properties extists and contain + // style:page-number + String newPageNumberProperty = style.getParProperty(XMLString.STYLE_PAGE_NUMBER, true); + if (newPageNumberProperty != null) { + // Truncate auto and other string values + newPageNumberProperty = newPageNumberProperty.replaceAll("[^0-9]", ""); + if (!newPageNumberProperty.isEmpty()) { + // Save new page number + newPageNumber = Integer.parseInt(newPageNumberProperty); + } + + } + } + return newPageNumber; + } + private Node StartMasterPage(Node hnode, StyleWithProperties style, Integer newPageNumber) { if (checkMasterPageBreak(style)) { @@ -1890,10 +1885,14 @@ public class TextConverter extends ConverterHelper { inFooter = false; return node; } - private static boolean hasOutlineLevel(Node node) { - if (Misc.isElement(node) + private boolean hasOutlineLevel(Node node) { + if (Misc.isElement(node) && Misc.getAttribute(node, XMLString.TEXT_OUTLINE_LEVEL) != null && !Misc.getAttribute(node, XMLString.TEXT_OUTLINE_LEVEL).isEmpty()) { + String title = docSep.getTitle(node).trim(); + if (title == null || title.isEmpty()) { + return false; + } return true; } return false; diff --git a/src/main/java/writer2latex/xhtml/XhtmlConfig.java b/src/main/java/writer2latex/xhtml/XhtmlConfig.java index cffa5ae..1f47ff5 100644 --- a/src/main/java/writer2latex/xhtml/XhtmlConfig.java +++ b/src/main/java/writer2latex/xhtml/XhtmlConfig.java @@ -440,7 +440,11 @@ public class XhtmlConfig extends writer2latex.base.ConfigBase { public String getXhtmlUplink() { return options[UPLINK].getString(); } public String getXhtmlDirectoryIcon() { return options[DIRECTORY_ICON].getString(); } public String getXhtmlDocumentIcon() { return options[DOCUMENT_ICON].getString(); } - public String getHeadingTags() { return options[HEADING_TAGS].getString(); } + public String getHeadingTags() { + if ( ((IntegerOption) options[SPLIT_LEVEL]).getValue() != 0) { + return "none"; + } + return options[HEADING_TAGS].getString(); } public String getPageTags() { return options[PAGE_TAGS].getString(); } public boolean exportLetterSpacing() { return ((BooleanOption) options[EXPORT_LETTER_SPACING]).getValue(); }