From be88aec9771d18ac6750457fb846262b963f82e5 Mon Sep 17 00:00:00 2001
From: Georgy Litvinov <git@litvinovg.pro>
Date: Sun, 14 Mar 2021 00:55:58 +0100
Subject: [PATCH] Refactored separations

---
 .../java/w2phtml/xhtml/content/Separator.java | 140 ++++++++----------
 .../w2phtml/xhtml/content/TextParser.java     |   9 +-
 .../model/heading_with_sections.html          |   4 -
 3 files changed, 63 insertions(+), 90 deletions(-)

diff --git a/src/main/java/w2phtml/xhtml/content/Separator.java b/src/main/java/w2phtml/xhtml/content/Separator.java
index 9da1b32..0c66cf6 100644
--- a/src/main/java/w2phtml/xhtml/content/Separator.java
+++ b/src/main/java/w2phtml/xhtml/content/Separator.java
@@ -44,7 +44,7 @@ public class Separator {
 	private static final Logger logger  = LoggerFactory.getLogger(Separator.class);
 
 	public Separator(XhtmlConfig config, Converter converter) {
-		this.converter = converter;
+		Separator.converter = converter;
 		isGreenstoneCommentsNeeded = config.getGreenstoneSeparation();
 		pagination = config.pagination();
 		splitFilesOutlineLevel = config.getXhtmlSplitLevel();
@@ -55,88 +55,55 @@ public class Separator {
 		headingPath = new int[10];
 	}
 
-	//page break found. break outline
-	public Node breakPageWithOutline(Node currentNode, Node hnode, int pageNum) {
+	public Node breakOutline(Node currentNode, Node hnode, int pageNum) {
+		if (isGreenstoneCommentsNeeded) {
+			hnode = addGreenstoneOutlineBreaks(currentNode, hnode, pageNum);
+		} else if (rdfSeparation) {
+			hnode = addRDFOutlineBreaks(currentNode, hnode, pageNum);
+		} else if(isFileSplitNeed(currentNode, pageNum)) {
+			hnode = addFileOutlineBreaks(currentNode, hnode, pageNum);
+		} else {
+			logger.error("Shouldn't be here. No separation method found.");
+		}
+		return hnode;
+	}
+	
+	private Node addFileOutlineBreaks(Node currentNode, Node hnode, int pageNum) {
 		
+		prevPageContainer = hnode;	
+		hnode = closePage(hnode);
+		hnode = splitFiles(hnode);
+		lastSplitPageNum = pageNum;
+		hnode = openPage(hnode, pageNum);
+		return hnode;	
+	}
 
-		if (!isGreenstoneCommentsNeeded && 
-				!needPagination() && 
-				!needOutlineSplitOnThisPage(currentNode,pageNum)) {
-			return hnode;
+	private Node addGreenstoneOutlineBreaks(Node currentNode, Node hnode, int pageNum) {
+		
+		hnode = closePage(hnode);
+		Integer curLevel = getOutlineLevel(currentNode);
+		closeCommentHeadings(hnode, curLevel);
+		String title = getOutlineTitle(currentNode);
+		openCommentHeading(hnode, title);
+		addToCommentStack(curLevel.toString());
+		hnode = openPage(hnode, pageNum);
+		return hnode;	
 		}
+	
+	public Node addRDFOutlineBreaks(Node currentNode, Node hnode, int pageNum) {
 		 
-		if (needOutlineSplitOnThisPage(currentNode, pageNum)) {
-			prevPageContainer = hnode;
-		}
-
-		if (pageOpened) {
-			hnode = closePage(hnode);
-		}
+		prevPageContainer = hnode;	
+		hnode = closePage(hnode);
 		Integer curLevel = getOutlineLevel(currentNode);
-		if (isGreenstoneCommentsNeeded) {
-			closeCommentHeadings(hnode, curLevel);
-		}
 		updateHeadingPath(curLevel);
-		if (needOutlineSplitOnThisPage(currentNode, pageNum)) {
-			hnode = splitFiles(hnode);
-			if (rdfSeparation) {
-				setContentPath(hnode,curLevel);
-				String title = getOutlineTitle(currentNode);
-				setContentName(hnode,title);
-			}
-			lastSplitPageNum = pageNum;
-		}
-		if (isGreenstoneCommentsNeeded) {
-			String title = getOutlineTitle(currentNode);
-			openCommentHeading(hnode, title);
-			addToCommentStack(curLevel.toString());
-		}
-
-		if (needPagination()) {
-			hnode = openPage(hnode, pageNum);
-		}
-
+		hnode = splitFiles(hnode);
+		setContentPath(hnode,curLevel);
+		String title = getOutlineTitle(currentNode);
+		setContentName(hnode,title);
+		hnode = openPage(hnode, pageNum);
 		return hnode;
 	}
-	//page break not found. break outline
-	public Node breakOutlineOnly(Node currentNode, Node hnode, int pageNum) {
-
-		if (!isGreenstoneCommentsNeeded && 
-				!needPagination() && 
-				!needOutlineSplitOnThisPage(currentNode,pageNum)) {
-			return hnode;
-		}
-		if (needOutlineSplitOnThisPage(currentNode, pageNum)) {
-			prevPageContainer = hnode;
-		}
-		if (pageOpened) {
-			hnode = closePage(hnode);
-		}
-		Integer curLevel = getOutlineLevel(currentNode);
-		if (isGreenstoneCommentsNeeded) {
-			closeCommentHeadings(hnode, curLevel);
-		}
-		updateHeadingPath(curLevel);
-		if (needOutlineSplitOnThisPage(currentNode, pageNum)) {
-			hnode = splitFiles(hnode);
-			if (rdfSeparation) {
-				setContentPath(hnode,curLevel);		
-				String title = getOutlineTitle(currentNode);
-				setContentName(hnode,title);
-			}
-			lastSplitPageNum = pageNum;
-		}
-		if (isGreenstoneCommentsNeeded) {
-			String title = getOutlineTitle(currentNode);
-			openCommentHeading(hnode, title);
-			addToCommentStack(curLevel.toString());
-		}
-		if (needPagination()) {
-			hnode = openPage(hnode, pageNum);
-		}
-		return hnode;
-	}
-
+	
 	public static boolean isNotOutlineNode(Node currentNode) {
 		Integer curLevel = getOutlineLevel(currentNode);
 		if (curLevel < 1) {
@@ -268,17 +235,29 @@ public class Separator {
 		if (isNotOutlineNode(currentNode)) { 
 			return false;
 		}
+		if (isFilesSplitEnabled() && !isFileSplitNeed( currentNode, curPage)) {
+			return false;
+		}
+		return true;
+	}
+
+	private boolean isFilesSplitEnabled() {
+		return splitFilesOutlineLevel != 0;
+	}
+	
+	private boolean isFileSplitNeed(Node currentNode, int pageNum) {
+		boolean result = false;
 		Integer curLevel = getOutlineLevel(currentNode);
 		if ( splitFilesOutlineLevel >= curLevel ) {
 			if (alignSplitToPages) {
-				if (lastSplitPageNum != curPage) {
-					return true;
+				if (lastSplitPageNum != pageNum) {
+					result = true;
 				}	
 			} else {
-				return true;
+				result = true;
 			}
 		}
-		return false;
+		return result;
 	}
 	
 	public boolean isPageOpened() {
@@ -412,7 +391,6 @@ public class Separator {
 		Node content = outlineNode.cloneNode(true);
 		NodeList lineBreaks = ((Element) content).getElementsByTagName(XMLString.TEXT_LINE_BREAK);
 		NodeList textTabs = ((Element) content).getElementsByTagName(XMLString.TEXT_TAB);
-
 		replaceWithSpaces(lineBreaks);
 		replaceWithSpaces(textTabs);
 		deleteNotesIn(content);
@@ -483,7 +461,7 @@ public class Separator {
 	}
 
 	public Node closePage(Node hnode) {
-		if (pageOpened == false) {
+		if (pageOpened == false || !needPagination()) {
 			return hnode;
 		}
 		hnode = exitPageContainer(hnode);
diff --git a/src/main/java/w2phtml/xhtml/content/TextParser.java b/src/main/java/w2phtml/xhtml/content/TextParser.java
index e96940a..07d1cac 100644
--- a/src/main/java/w2phtml/xhtml/content/TextParser.java
+++ b/src/main/java/w2phtml/xhtml/content/TextParser.java
@@ -1229,8 +1229,8 @@ public class TextParser extends Parser {
 		
 		if (isPageBreakFound(style) && !isMasterPageNull) {
 			hnode = breakPage(currentNode, hnode, style);
-		} else if (!Separator.isNotOutlineNode(currentNode))  {
-			hnode = docSep.breakOutlineOnly(currentNode, hnode, pageNum);
+		} else if (isOutlineBreakFound(currentNode))  {
+			hnode = docSep.breakOutline(currentNode, hnode, pageNum);
 		}
 		setBreakPageBeforeNextFromStyle(style);
 		return hnode;
@@ -1281,12 +1281,11 @@ public class TextParser extends Parser {
 		setPageContainerStyle();
 		setNewPageNumber(style);
 		
-		if (Separator.isNotOutlineNode(currentNode)) {
+		if (!isOutlineBreakFound(currentNode)) {
 			hnode = docSep.turnPage(hnode, pageNum);
 		} else {
-			hnode = docSep.breakPageWithOutline(currentNode, hnode, pageNum);
+			hnode = docSep.breakOutline(currentNode, hnode, pageNum);
 		}		
-		
 		if (docSep.needPagination()) {
 			addHeader(hnode);
 		}
diff --git a/testdocuments/model/heading_with_sections.html b/testdocuments/model/heading_with_sections.html
index 27aa34e..18ab7eb 100644
--- a/testdocuments/model/heading_with_sections.html
+++ b/testdocuments/model/heading_with_sections.html
@@ -24,10 +24,6 @@
       <div class="pageContainer" style="column-count: 1;">
         <h1 id="toc0" style="margin-left:0;border:none;padding:0;font-weight:bold;clear:left;font-size:1.5166668rem;margin-top:0.9992126rem;font-family:&apos;IPH Astra Serif&apos;,serif;page-break-before:always;margin-bottom:0.5007874rem;margin-right:0;text-align:center;">Heading 1</h1>
         <p class="Textbody" style="margin-left:0;border:none;padding:0;text-indent:2.1259842rem;font-size:8.0rem;margin-top:0;font-family:&apos;IPH Astra Serif&apos;,serif;margin-bottom:0.58346456rem;margin-right:0;">Tex</p>
-      </div>
-    </div>
-    <div class="pageNum" page="2">
-      <div class="pageContainer" style="column-count: 1;">
         <h1 id="toc1" style="margin-left:0;border:none;padding:0;font-weight:bold;clear:left;font-size:1.5166668rem;margin-top:0.9992126rem;font-family:&apos;IPH Astra Serif&apos;,serif;margin-bottom:0.5007874rem;margin-right:0;text-align:center;">Heading 1</h1>
       </div>
     </div>