From 88fb27d55d5d8fcd4ee660e20e1c3b8dabade9db Mon Sep 17 00:00:00 2001 From: Georgy Litvinov Date: Sun, 25 Sep 2016 16:30:52 +0300 Subject: [PATCH] refactoring and rewriting code --- .../writer2latex/xhtml/GreenstoneTags.java | 97 ++++++++++--------- .../writer2latex/xhtml/TextConverter.java | 80 ++++++++------- .../java/writer2latex/xhtml/XhtmlConfig.java | 2 +- 3 files changed, 99 insertions(+), 80 deletions(-) diff --git a/source/java/writer2latex/xhtml/GreenstoneTags.java b/source/java/writer2latex/xhtml/GreenstoneTags.java index c3a5eae..df8afd9 100644 --- a/source/java/writer2latex/xhtml/GreenstoneTags.java +++ b/source/java/writer2latex/xhtml/GreenstoneTags.java @@ -30,60 +30,46 @@ public class GreenstoneTags { protected static Node processHeading(Node currentNode, Node hnode, int pageNum) { - - if (headingTags.equals(NONE)){ + //Get outline level + String sLevel = Misc.getAttribute(currentNode, XMLString.TEXT_OUTLINE_LEVEL); + if (sLevel == null || sLevel.isEmpty()) { + System.out.println("Error occurred. Expected Outline level here!"); return hnode; } + int nLevel = Integer.parseInt(sLevel); - String sLevel = Misc.getAttribute(currentNode, XMLString.TEXT_OUTLINE_LEVEL); - //If this heading contain outline-level - if (sLevel != null && !sLevel.isEmpty()) { - int nLevel = Integer.parseInt(sLevel); - if (pageOpened) { - hnode = closePage(hnode); - } - + if (pageOpened) { + hnode = closePage(hnode); + } + + if (headingTags.equals(SECTIONS)){ closeHeadingSections(hnode, nLevel); - Node nextNode = currentNode.getNextSibling(); - //Useless to open section if we are at end of the document - if (nextNode != null){ - String title = getTitle(currentNode); - //Open Heading section - openSection(hnode, title); - headerStack.offerFirst(Integer.parseInt(sLevel)); - //If next node is not a heading element with outline number and tags type is headings-pages - //Then open paged section - if (!( - nextNode.getNodeType() == Node.ELEMENT_NODE - && nextNode.getNodeName().equals(XMLString.TEXT_H) - && Misc.getAttribute(nextNode, XMLString.TEXT_OUTLINE_LEVEL) != null - && !Misc.getAttribute(nextNode, XMLString.TEXT_OUTLINE_LEVEL).isEmpty() - )) { - hnode = openPage(hnode, pageNum); - } - } - - } + String title = getTitle(currentNode); + openHeadingSection(hnode, title); + headerStack.offerFirst(Integer.parseInt(sLevel)); + } + if (!pageTags.equals(NONE)){ + hnode = openPage(hnode, pageNum); + } + return hnode; } + protected static Node processPageBreak(Node currentNode, Node hnode, Integer pageNum){ if (pageTags.equals(NONE)){ return hnode; } - - if ( !( currentNode.getNodeType() == Node.ELEMENT_NODE - && Misc.getAttribute(currentNode, XMLString.TEXT_OUTLINE_LEVEL) != null - && !Misc.getAttribute(currentNode, XMLString.TEXT_OUTLINE_LEVEL).isEmpty() - ) - ) { - if (pageOpened) { - hnode = closePage(hnode); - } - hnode = openPage(hnode, pageNum); - } + if (pageOpened) { + hnode = closePage(hnode); + } + hnode = openPage(hnode, pageNum); + return hnode; } - //Method to open main document tag + + /** + * Opens main document section heading tag + */ protected static Node StartDocument(Node hnode, String title, String heading, String pages, int pageNum){ headingTags = heading; pageTags = pages; @@ -93,7 +79,7 @@ public class GreenstoneTags { } if(headingTags.equals(SECTIONS)){ //Create global section - openSection(hnode, title); + openHeadingSection(hnode, title); } hnode = openPage(hnode, pageNum); @@ -128,7 +114,23 @@ public class GreenstoneTags { } private static Node exitPageDiv(Node hnode){ + + while ( hnode.getParentNode() != null + && + !( + hnode.getNodeType() == Node.ELEMENT_NODE + && + ((Element) hnode).getTagName().equals("div") + //&& + //((Element) hnode).getAttribute("class").equals("pageNum") + ) + ){ + System.out.println("TAG " + ((Element) hnode).getTagName()); + System.out.println("class " + ((Element) hnode).getAttribute("class")); + hnode = hnode.getParentNode(); + } hnode = hnode.getParentNode(); + System.out.println(((Element) hnode).getTagName()); return hnode; } @@ -166,13 +168,14 @@ public class GreenstoneTags { // insert open section comment before header node hnode.appendChild(openSection); } - private static void openSection(Node hnode, String title){ + private static void openHeadingSection(Node hnode, String title){ Document doc = hnode.getOwnerDocument(); String commentText = "
\n\n" + title + "\n"; Node openSection = doc.createComment(commentText); // insert open section comment before header node hnode.appendChild(openSection); + } private static void closeSection(Node hnode){ @@ -209,11 +212,13 @@ public class GreenstoneTags { private static Node openPage(Node hnode, Integer pageNum){ if (pageTags.equals(SECTIONS)){ openPageSection(hnode, pageNum); + pageOpened = true; } else if (pageTags.equals(DIV)){ hnode = openPageDiv(hnode, pageNum); - } - pageOpened = true; + pageOpened = true; + } + return hnode; } diff --git a/source/java/writer2latex/xhtml/TextConverter.java b/source/java/writer2latex/xhtml/TextConverter.java index b05792b..fc53e39 100644 --- a/source/java/writer2latex/xhtml/TextConverter.java +++ b/source/java/writer2latex/xhtml/TextConverter.java @@ -265,7 +265,7 @@ public class TextConverter extends ConverterHelper { int nOutlineLevel = getOutlineLevel((Element)child); Node rememberNode = hnode; hnode = processPageBreaks(child, hnode, style); - hnode = GreenstoneTags.processHeading(child, hnode, pageNum); + //hnode = GreenstoneTags.processHeading(child, hnode, pageNum); hnode = maybeSplit(hnode,style,nOutlineLevel); nCharacterCount+=OfficeReader.getCharacterCount(child); handleHeading((Element)child,(Element)hnode,rememberNode!=hnode); @@ -1565,37 +1565,16 @@ public class TextConverter extends ConverterHelper { } } - // Set first master page name - // If Master Page style has been already set } if (currentMasterPage == null && style != null) { - // Looks like first page. - if (checkMasterStylePageBreak(style)) { - updateMasterPage(style); - } else { - //Set standard MP - currentMasterPage = "Standard"; - } - if (newPageNumber != null) { - pageNum = newPageNumber; - } else { - fitPageNumberToMasterPageStyle(); - } - - //Start tagging - String sTitle = converter.getTitle(); - hnode = GreenstoneTags.StartDocument(hnode, sTitle,headingTags,pageTags, pageNum); - - //Print header - addHeader(hnode); - + hnode = StartMasterPage(hnode, style, newPageNumber); } else { //If old master page was defined //If new master page definition found //Or if fo:break-before found //Or if soft-page-break or fo:break-after appeared before this node - if (checkMasterStylePageBreak(style) || checkHardBreakBefore(style) || breakBeforeNextNode) { + if (checkMasterPageBreak(style) || checkHardBreakBefore(style) || breakBeforeNextNode) { //Insert footnotes footCv.insertFootnotes(hnode, true); //Add previous MP footer @@ -1609,8 +1588,16 @@ public class TextConverter extends ConverterHelper { pageNum++; fitPageNumberToMasterPageStyle(); } - //if - hnode = GreenstoneTags.processPageBreak(currentNode, hnode, pageNum); + + if ( currentNode.getNodeType() == Node.ELEMENT_NODE + && Misc.getAttribute(currentNode, XMLString.TEXT_OUTLINE_LEVEL) != null + && !Misc.getAttribute(currentNode, XMLString.TEXT_OUTLINE_LEVEL).isEmpty() + ){ + hnode = GreenstoneTags.processHeading(currentNode, hnode, pageNum); + } else { + hnode = GreenstoneTags.processPageBreak(currentNode, hnode, pageNum); + } + //Print new header addHeader(hnode); breakBeforeNextNode = false; @@ -1619,13 +1606,40 @@ public class TextConverter extends ConverterHelper { } } + //Create method to process Section -> Something -> Foo-break-before + + /*currentNode.getNextSibling() + if (currentNode.getNextSibling()) + */ + if (checkHardBreakAfter(style)) { - breakBeforeNextNode = true; - return hnode; - - } - breakBeforeNextNode = false; + } else { + breakBeforeNextNode = false; + } + + return hnode; + } + + private Node StartMasterPage(Node hnode, StyleWithProperties style, Integer newPageNumber) { + + if (checkMasterPageBreak(style)) { + updateMasterPage(style); + } else { + currentMasterPage = "Standard"; + } + if (newPageNumber != null) { + pageNum = newPageNumber; + } else { + fitPageNumberToMasterPageStyle(); + } + + //Start tagging + String sTitle = converter.getTitle(); + hnode = GreenstoneTags.StartDocument(hnode, sTitle,headingTags,pageTags, pageNum); + + //Print header + addHeader(hnode); return hnode; } @@ -1658,7 +1672,7 @@ public class TextConverter extends ConverterHelper { } } - private boolean checkMasterStylePageBreak(StyleWithProperties style) { + private boolean checkMasterPageBreak(StyleWithProperties style) { // Page break was found before if (style != null) { @@ -1673,7 +1687,7 @@ public class TextConverter extends ConverterHelper { } private void updateMasterPage(StyleWithProperties style) { - if (style != null && checkMasterStylePageBreak(style)) { + if (style != null && checkMasterPageBreak(style)) { String sMasterPage = style.getMasterPageName(); if (sMasterPage != null && sMasterPage.length() > 0) { currentMasterPage = sMasterPage; diff --git a/source/java/writer2latex/xhtml/XhtmlConfig.java b/source/java/writer2latex/xhtml/XhtmlConfig.java index e91855f..52f0585 100644 --- a/source/java/writer2latex/xhtml/XhtmlConfig.java +++ b/source/java/writer2latex/xhtml/XhtmlConfig.java @@ -293,7 +293,7 @@ public class XhtmlConfig extends writer2latex.base.ConfigBase { options[UPLINK] = new Option("uplink",""); options[DIRECTORY_ICON] = new Option("directory_icon",""); options[DOCUMENT_ICON] = new Option("document_icon",""); - options[HEADING_TAGS] = new Option("heading_tags","sections"); + options[HEADING_TAGS] = new Option("heading_tags","none"); options[PAGE_TAGS] = new Option("page_tags","div"); }