From fa2bc287c05813f24ab2652f2982da509f6cab03 Mon Sep 17 00:00:00 2001
From: Georgy Litvinov <git@litvinovg.pro>
Date: Mon, 10 Feb 2020 18:57:44 +0100
Subject: [PATCH] Refactoring

---
 .../writer2latex/xhtml/content/Separator.java |  20 +-
 .../xhtml/content/TextParser.java             | 344 ++++++++----------
 2 files changed, 155 insertions(+), 209 deletions(-)

diff --git a/src/main/java/writer2latex/xhtml/content/Separator.java b/src/main/java/writer2latex/xhtml/content/Separator.java
index 6963d85..f0e4750 100644
--- a/src/main/java/writer2latex/xhtml/content/Separator.java
+++ b/src/main/java/writer2latex/xhtml/content/Separator.java
@@ -81,7 +81,7 @@ public class Separator {
 			headerStack.offerFirst(Integer.parseInt(sLevel));
 
 		}
-		if (!noPageSeparation()) {
+		if (pagination) {
 			hnode = openPage(hnode, pageNum);
 		}
 			
@@ -106,7 +106,7 @@ public class Separator {
 	}
 	
 	public Node processPageBreak(Node currentNode, Node hnode, Integer pageNum){
-		if (noPageSeparation()){
+		if (!pagination){
 			return hnode;
 		}
 		if (pageOpened) {
@@ -124,7 +124,7 @@ public class Separator {
 	 * Opens main document section heading tag
 	 */
 	public Node startDocument(Node hnode, String title, int pageNum){
-		if (noHeadingSeparation() && noPageSeparation()){
+		if (noHeadingSeparation() && !pagination){
 			return hnode;
 		}
 		if(headingSeparation.equals(SECTIONS)){
@@ -137,7 +137,7 @@ public class Separator {
 	}
 	//Method to close open tags at the end of the document
 	public Node endDocument(Node hnode){
-		if (noHeadingSeparation() && noPageSeparation()){
+		if (noHeadingSeparation() && !pagination){
 			return hnode;
 		}
 		if (pageOpened){
@@ -299,15 +299,12 @@ public class Separator {
 		return hnode;
 	}
 	public Node openPage(Node hnode, Integer pageNum){
-		if (pageOpened == true) {
+		if (pageOpened == true || !pagination) {
 			return hnode;
 		}
-		if (pagination){
-			hnode = openPageDiv(hnode, pageNum);
-			pageOpened = true;
-		} 
+		hnode = openPageDiv(hnode, pageNum);
+		pageOpened = true;
 		hnode = enterPageContainer(hnode);
-		
 		return hnode;
 	}
 	
@@ -340,9 +337,6 @@ public class Separator {
 	private static boolean noHeadingSeparation() {
 		return headingSeparation.equals(NONE);
 	}
-	private static boolean noPageSeparation() {
-		return !pagination;
-	}
 	private static String openHeadingCommentText(String title) {
 		String comment = "<Section>\n<Description>\n<Metadata name=\"Title\">" + title
 				+ "</Metadata>\n</Description>";
diff --git a/src/main/java/writer2latex/xhtml/content/TextParser.java b/src/main/java/writer2latex/xhtml/content/TextParser.java
index c74af6e..06f702d 100644
--- a/src/main/java/writer2latex/xhtml/content/TextParser.java
+++ b/src/main/java/writer2latex/xhtml/content/TextParser.java
@@ -66,7 +66,7 @@ public class TextParser extends Parser {
 	private int nSplitAfter = 150000;
 	// TODO: Collect soft page breaks between table rows
 	private boolean bPendingPageBreak = false; // We have encountered a page break which should be inserted asap
-    private int splitResultsLevel = 0;  // The outline level at which to split files (0=no split)
+    private int splitHeadingLevel = 0;  // The outline level at which to split files (0=no split)
     private int nRepeatLevels = 5; // The number of levels to repeat when splitting (0=no repeat)
     private int nLastSplitLevel = 1; // The outline level at which the last split occurred
     private int nDontSplitLevel = 0; // if > 0 splitting is forbidden
@@ -115,7 +115,6 @@ public class TextParser extends Parser {
 	//Current master page name
 	private String nextMasterPage = null;
 	
-	private String headingTags = config.getHeadingTags();
 	private boolean pagination = config.pagination();
 	private boolean breakBeforeNextNode = false;
 	private boolean inTable = false;
@@ -126,7 +125,6 @@ public class TextParser extends Parser {
 	private boolean inFooter = false;
 	private String endnotesContext = null;
 	private String footnotesContext = null;
-	private boolean inlineCSS;
 	PageContainer pageContainer = null;
 
     public TextParser(OfficeReader ofr, XhtmlConfig config, Converter converter) {
@@ -139,7 +137,7 @@ public class TextParser extends Parser {
         footCv = new FootnoteParser(ofr, config, converter);
         endCv = new EndnoteParser(ofr, config, converter);
         nSplitAfter = 1000*config.splitAfter();
-        splitResultsLevel = config.getXhtmlSplitLevel();
+        splitHeadingLevel = config.getXhtmlSplitLevel();
         nRepeatLevels = converter.isOPS() ? 0 : config.getXhtmlRepeatLevels(); // never repeat headings in EPUB
         nFloatMode = ofr.isText() && config.xhtmlFloatObjects() ? 
             DrawParser.FLOATING : DrawParser.ABSOLUTE;
@@ -147,7 +145,6 @@ public class TextParser extends Parser {
         displayHiddenText = config.displayHiddenText();
         pageContainer = converter.pageContainer;
         docSep = new Separator(config, converter);
-        inlineCSS = config.inlineCSS();
     }
 	
     /** Converts an office node as a complete text document
@@ -157,7 +154,7 @@ public class TextParser extends Parser {
     public void convertDocumentContent(Element onode) {
         Element hnode = converter.nextOutFile();
         // Create form
-        if (splitResultsLevel==0) {
+        if (splitHeadingLevel==0) {
             Element form = getDrawCv().createForm();
             if (form!=null) {
                 hnode.appendChild(form);
@@ -181,7 +178,6 @@ public class TextParser extends Parser {
         // Add footnotes and endnotes
         insertFootnotes(hnode,true);
         
-        //hnode = exitPageContainer(hnode);
         addFooter(hnode);
         
         insertEndnotes(hnode, null);
@@ -191,11 +187,8 @@ public class TextParser extends Parser {
         tocParser.generate();
         bInToc = false;
 		
-        // Generate navigation links
-        generateHeaders();
-        generateFooters();
         bInToc = true;
-        tocParser.generatePanels(splitResultsLevel);
+        tocParser.generatePanels(splitHeadingLevel);
         bInToc = false;
     }
 
@@ -254,12 +247,6 @@ public class TextParser extends Parser {
     // NAVIGATION (fill header, footer and panel with navigation links)
     ////////////////////////////////////////////////////////////////////////
 
-    // The header is populated with prev/next navigation
-    private void generateHeaders() { }
-
-    // The footer is populated with prev/next navigation
-    private void generateFooters() { }
-
 	
     ////////////////////////////////////////////////////////////////////////
     // BLOCK TEXT (returns current html node at end of block)
@@ -575,7 +562,7 @@ public class TextParser extends Parser {
   			// Export the heading
   			if (!bTocOnly) {
   				// If split output, add headings of higher levels
-  				if (bAfterSplit && splitResultsLevel > 0) {
+  				if (bAfterSplit && splitHeadingLevel > 0) {
   					int nFirst = nLevel - nRepeatLevels;
   					if (nFirst < 0) {
   						nFirst = 0;
@@ -1066,42 +1053,34 @@ public class TextParser extends Parser {
     
     
     // Process the contents of a fake list item
-    private Node traverseFakeListItem (Node onode, Node hnode, int nLevel,
-        String sStyleName, boolean bUnNumbered, boolean bRestart, int nStartValue) { 
-        Node child = onode.getFirstChild();
-        while (child!=null) {
-            if (child.getNodeType() == Node.ELEMENT_NODE) {
-                String sNodeName = child.getNodeName();
-            
-                if (sNodeName.equals(TEXT_H)) {
-                    nDontSplitLevel++;
-                    int nOutlineLevel = getOutlineLevel((Element)onode);
-                    Node rememberNode = hnode;
-                    StyleWithProperties style = ofr.getParStyle(Misc.getAttribute(child, TEXT_STYLE_NAME));
-                    //hnode = maybeSplit(hnode,style,nOutlineLevel);
-                    handleHeading((Element)child, (Element)hnode, rememberNode!=hnode,
-                        ofr.getListStyle(sStyleName), nLevel,
-                        bUnNumbered, bRestart, nStartValue);
-                    nDontSplitLevel--;
-                    if (nDontSplitLevel==0) { bAfterHeading=true; }
-                }
-                else if (sNodeName.equals(TEXT_P)) {
-                     // Currently we only handle fakes lists containing headings
-                }
-                else if (sNodeName.equals(TEXT_LIST)) { // oasis
-                     return traverseFakeList(child, hnode, nLevel+1, sStyleName);
-                }
-                else if (sNodeName.equals(TEXT_ORDERED_LIST)) { // old
-                     return traverseFakeList(child, hnode, nLevel+1, sStyleName);
-                }
-                else if (sNodeName.equals(TEXT_UNORDERED_LIST)) { // old
-                     return traverseFakeList(child, hnode, nLevel+1, sStyleName);
-                }
-            }
-            child = child.getNextSibling();
-        }
-        return hnode;
-    }
+	private Node traverseFakeListItem(Node onode, Node hnode, int nLevel, String sStyleName, boolean bUnNumbered, boolean bRestart, int nStartValue) {
+		Node child = onode.getFirstChild();
+		while (child != null) {
+			if (child.getNodeType() == Node.ELEMENT_NODE) {
+				String sNodeName = child.getNodeName();
+
+				if (sNodeName.equals(TEXT_H)) {
+					nDontSplitLevel++;
+					Node rememberNode = hnode;
+					handleHeading((Element) child, (Element) hnode, rememberNode != hnode, ofr.getListStyle(sStyleName), nLevel, bUnNumbered, bRestart, nStartValue);
+					nDontSplitLevel--;
+					if (nDontSplitLevel == 0) {
+						bAfterHeading = true;
+					}
+				} else if (sNodeName.equals(TEXT_P)) {
+					// Currently we only handle fakes lists containing headings
+				} else if (sNodeName.equals(TEXT_LIST)) { // oasis
+					return traverseFakeList(child, hnode, nLevel + 1, sStyleName);
+				} else if (sNodeName.equals(TEXT_ORDERED_LIST)) { // old
+					return traverseFakeList(child, hnode, nLevel + 1, sStyleName);
+				} else if (sNodeName.equals(TEXT_UNORDERED_LIST)) { // old
+					return traverseFakeList(child, hnode, nLevel + 1, sStyleName);
+				}
+			}
+			child = child.getNextSibling();
+		}
+		return hnode;
+	}
 	
     ////////////////////////////////////////////////////////////////////////
     // INLINE TEXT
@@ -1146,148 +1125,121 @@ public class TextParser extends Parser {
     /*
      * Process inline text
      */
-    protected void traverseInlineText (Node onode,Node hnode) {        
-        //String styleName = Misc.getAttribute(onode, TEXT_STYLE_NAME);
-                              
-        if (onode.hasChildNodes()) {
-            NodeList nList = onode.getChildNodes();
-            int nLen = nList.getLength();
-                       
-            for (int i = 0; i < nLen; i++) {
-                
-                Node child = nList.item(i);
-                short nodeType = child.getNodeType();
-               
-                switch (nodeType) {
-                    case Node.TEXT_NODE:
-                        String s = child.getNodeValue();
-                        if (s.length() > 0) {
-                            hnode.appendChild( converter.createTextNode(s) );
-                        }
-                        break;
-                        
-                    case Node.ELEMENT_NODE:
-                        String sName = child.getNodeName();
-                        if (OfficeReader.isDrawElement(child)) {
-                            Element elm = getDrawCv().getRealDrawElement((Element)child);
-                            if (elm!=null) {
-                                String sAnchor = (elm.getAttribute(TEXT_ANCHOR_TYPE));
-                                if ("as-char".equals(sAnchor)) {
-                                    getDrawCv().handleDrawElement(elm,null,(Element)hnode,DrawParser.INLINE);
-                                }
-                            }
-                        }
-                        else if (child.getNodeName().equals(TEXT_S)) {
-                            if (config.ignoreDoubleSpaces()) {
-                                hnode.appendChild( converter.createTextNode(" ") );
-                            }
-                            else {
-                                int count= Misc.getPosInteger(Misc.getAttribute(child,TEXT_C),1);
-                                for ( ; count > 0; count--) {
-                                    hnode.appendChild( converter.createTextNode("\u00A0") );
-                                }
-                            }
-                        }
-                        else if (sName.equals(TEXT_TAB_STOP)) {
-                            handleTabStop(child,hnode);
-                        }
-                        else if (sName.equals(TEXT_TAB)) { // oasis
-                            handleTabStop(child,hnode);
-                        }
-                        else if (sName.equals(TEXT_LINE_BREAK)) {
-                            if (!config.ignoreHardLineBreaks()) {
-                                hnode.appendChild( converter.createElement("br") );
-                            }
-                        }
-                        else if (sName.equals(TEXT_SPAN)) {
-                            handleSpan(child,hnode);
-                        }
-                        else if (sName.equals(TEXT_A)) {
-                            handleAnchor(child,hnode);
-                        }
-                        else if (sName.equals(TEXT_FOOTNOTE)) {
-                            footCv.handleNote(child,hnode,footnotesContext);
-                        }
-                        else if (sName.equals(TEXT_ENDNOTE)) {
-                            endCv.handleNote(child,hnode,endnotesContext);
-                        }
-                        else if (sName.equals(TEXT_NOTE)) { // oasis
-                            if ("endnote".equals(Misc.getAttribute(child,TEXT_NOTE_CLASS))) {
-                                endCv.handleNote(child,hnode,endnotesContext);
-                            }
-                            else {
-                                footCv.handleNote(child,hnode,footnotesContext);
-                            }
-                        }
-                        else if (sName.equals(TEXT_SEQUENCE)) {
-	                        handleSequence(child,hnode);
-                        }
-                        else if (sName.equals(TEXT_PAGE_NUMBER)) {
-	                        handlePageNumber(child,hnode);
-                        }
-                        else if (sName.equals(TEXT_PAGE_COUNT)) {
-	                        handlePageCount(child,hnode);
-                        }
-                        else if (sName.equals(TEXT_SEQUENCE_REF)) {
-	                        handleSequenceRef(child,hnode);
-                        }
-                        else if (sName.equals(TEXT_FOOTNOTE_REF)) {
-	                        handleNoteRef(child,hnode);
-                        }
-                        else if (sName.equals(TEXT_ENDNOTE_REF)) {
-	                        handleNoteRef(child,hnode);
-                        }
-                        else if (sName.equals(TEXT_NOTE_REF)) { // oasis
-	                        handleNoteRef(child,hnode);
-                        }
-                        else if (sName.equals(TEXT_REFERENCE_MARK)) {
-	                        handleReferenceMark(child,hnode);
-                        }
-                        else if (sName.equals(TEXT_REFERENCE_MARK_START)) {
-	                        handleReferenceMark(child,hnode);
-                        }
-                        else if (sName.equals(TEXT_REFERENCE_REF)) {
-	                        handleReferenceRef(child,hnode);
-                        }
-                        else if (sName.equals(TEXT_BOOKMARK)) {
-	                        handleBookmark(child,hnode);
-                        }
-                        else if (sName.equals(TEXT_BOOKMARK_START)) {
-	                        handleBookmark(child,hnode);
-                        }
-                        else if (sName.equals(TEXT_BOOKMARK_REF)) {
-	                        handleBookmarkRef(child,hnode);
-                        }
-                        else if (sName.equals(TEXT_ALPHABETICAL_INDEX_MARK)) {
-	                        if (!bInToc) { indexCv.handleIndexMark(child,hnode); }
-                        }
-                        else if (sName.equals(TEXT_ALPHABETICAL_INDEX_MARK_START)) {
-                        	if (!bInToc) { indexCv.handleIndexMarkStart(child,hnode); }
-                        }
-                        else if (sName.equals(TEXT_TOC_MARK)) {
-	                        tocParser.handleTocMark(child,hnode);
-                        }
-                        else if (sName.equals(TEXT_TOC_MARK_START)) {
-	                        tocParser.handleTocMark(child,hnode);
-                        }
-                        else if (sName.equals(TEXT_BIBLIOGRAPHY_MARK)) {
-	                        handleBibliographyMark(child,hnode);
-                        }
-                        else if (sName.equals(OFFICE_ANNOTATION)) {
-                            converter.handleOfficeAnnotation(child,hnode);
-                        }
-						else if (sName.startsWith("text:")) {
-							 traverseInlineText(child,hnode);
-						}
-                        // other tags are ignored;
-                        break;
-                    default:
-                        // Do nothing
-                }
-            }
-        }
-    }
-	
+  	protected void traverseInlineText(Node onode, Node hnode) {
+  		// String styleName = Misc.getAttribute(onode, TEXT_STYLE_NAME);
+  
+  		if (onode.hasChildNodes()) {
+  			NodeList nList = onode.getChildNodes();
+  			int nLen = nList.getLength();
+  
+  			for (int i = 0; i < nLen; i++) {
+  
+  				Node child = nList.item(i);
+  				short nodeType = child.getNodeType();
+  
+  				switch (nodeType) {
+  				case Node.TEXT_NODE:
+  					String s = child.getNodeValue();
+  					if (s.length() > 0) {
+  						hnode.appendChild(converter.createTextNode(s));
+  					}
+  					break;
+  
+  				case Node.ELEMENT_NODE:
+  					String sName = child.getNodeName();
+  					if (OfficeReader.isDrawElement(child)) {
+  						Element elm = getDrawCv().getRealDrawElement((Element) child);
+  						if (elm != null) {
+  							String sAnchor = (elm.getAttribute(TEXT_ANCHOR_TYPE));
+  							if ("as-char".equals(sAnchor)) {
+  								getDrawCv().handleDrawElement(elm, null, (Element) hnode, DrawParser.INLINE);
+  							}
+  						}
+  					} else if (child.getNodeName().equals(TEXT_S)) {
+  						if (config.ignoreDoubleSpaces()) {
+  							hnode.appendChild(converter.createTextNode(" "));
+  						} else {
+  							int count = Misc.getPosInteger(Misc.getAttribute(child, TEXT_C), 1);
+  							for (; count > 0; count--) {
+  								hnode.appendChild(converter.createTextNode("\u00A0"));
+  							}
+  						}
+  					} else if (sName.equals(TEXT_TAB_STOP)) {
+  						handleTabStop(child, hnode);
+  					} else if (sName.equals(TEXT_TAB)) { // oasis
+  						handleTabStop(child, hnode);
+  					} else if (sName.equals(TEXT_LINE_BREAK)) {
+  						if (!config.ignoreHardLineBreaks()) {
+  							hnode.appendChild(converter.createElement("br"));
+  						}
+  					} else if (sName.equals(TEXT_SPAN)) {
+  						handleSpan(child, hnode);
+  					} else if (sName.equals(TEXT_A)) {
+  						handleAnchor(child, hnode);
+  					} else if (sName.equals(TEXT_FOOTNOTE)) {
+  						footCv.handleNote(child, hnode, footnotesContext);
+  					} else if (sName.equals(TEXT_ENDNOTE)) {
+  						endCv.handleNote(child, hnode, endnotesContext);
+  					} else if (sName.equals(TEXT_NOTE)) { // oasis
+  						if ("endnote".equals(Misc.getAttribute(child, TEXT_NOTE_CLASS))) {
+  							endCv.handleNote(child, hnode, endnotesContext);
+  						} else {
+  							footCv.handleNote(child, hnode, footnotesContext);
+  						}
+  					} else if (sName.equals(TEXT_SEQUENCE)) {
+  						handleSequence(child, hnode);
+  					} else if (sName.equals(TEXT_PAGE_NUMBER)) {
+  						handlePageNumber(child, hnode);
+  					} else if (sName.equals(TEXT_PAGE_COUNT)) {
+  						handlePageCount(child, hnode);
+  					} else if (sName.equals(TEXT_SEQUENCE_REF)) {
+  						handleSequenceRef(child, hnode);
+  					} else if (sName.equals(TEXT_FOOTNOTE_REF)) {
+  						handleNoteRef(child, hnode);
+  					} else if (sName.equals(TEXT_ENDNOTE_REF)) {
+  						handleNoteRef(child, hnode);
+  					} else if (sName.equals(TEXT_NOTE_REF)) { // oasis
+  						handleNoteRef(child, hnode);
+  					} else if (sName.equals(TEXT_REFERENCE_MARK)) {
+  						handleReferenceMark(child, hnode);
+  					} else if (sName.equals(TEXT_REFERENCE_MARK_START)) {
+  						handleReferenceMark(child, hnode);
+  					} else if (sName.equals(TEXT_REFERENCE_REF)) {
+  						handleReferenceRef(child, hnode);
+  					} else if (sName.equals(TEXT_BOOKMARK)) {
+  						handleBookmark(child, hnode);
+  					} else if (sName.equals(TEXT_BOOKMARK_START)) {
+  						handleBookmark(child, hnode);
+  					} else if (sName.equals(TEXT_BOOKMARK_REF)) {
+  						handleBookmarkRef(child, hnode);
+  					} else if (sName.equals(TEXT_ALPHABETICAL_INDEX_MARK)) {
+  						if (!bInToc) {
+  							indexCv.handleIndexMark(child, hnode);
+  						}
+  					} else if (sName.equals(TEXT_ALPHABETICAL_INDEX_MARK_START)) {
+  						if (!bInToc) {
+  							indexCv.handleIndexMarkStart(child, hnode);
+  						}
+  					} else if (sName.equals(TEXT_TOC_MARK)) {
+  						tocParser.handleTocMark(child, hnode);
+  					} else if (sName.equals(TEXT_TOC_MARK_START)) {
+  						tocParser.handleTocMark(child, hnode);
+  					} else if (sName.equals(TEXT_BIBLIOGRAPHY_MARK)) {
+  						handleBibliographyMark(child, hnode);
+  					} else if (sName.equals(OFFICE_ANNOTATION)) {
+  						converter.handleOfficeAnnotation(child, hnode);
+  					} else if (sName.startsWith("text:")) {
+  						traverseInlineText(child, hnode);
+  					}
+  					// other tags are ignored;
+  					break;
+  				default:
+  					// Do nothing
+  				}
+  			}
+  		}
+  	}
+
     private void handleTabStop(Node onode, Node hnode) {
         // xhtml does not have tab stops, but we export and ASCII TAB character, which the
         // user may choose to format