diff --git a/src/main/java/writer2latex/xhtml/Converter.java b/src/main/java/writer2latex/xhtml/Converter.java index d15c654..645de7f 100644 --- a/src/main/java/writer2latex/xhtml/Converter.java +++ b/src/main/java/writer2latex/xhtml/Converter.java @@ -104,7 +104,6 @@ public class Converter extends BasicConverter { private int outFileIndex; private XhtmlDocument htmlDoc; // current outfile private Document htmlDOM; // current DOM, usually within htmlDoc - private boolean bNeedHeaderFooter = false; public PageContainer pageContainer = null; //private int nTocFileIndex = -1; //private int nAlphabeticalIndex = -1; @@ -253,8 +252,6 @@ public class Converter extends BasicConverter { outFiles = new Vector(); outFileIndex = -1; - bNeedHeaderFooter = !bOPS && (ofr.isSpreadsheet() || ofr.isPresentation() || config.getXhtmlSplitLevel()>0 || config.getXhtmlUplink().length()>0); - l10n = new L10n(); if (isOPS()) { @@ -655,148 +652,135 @@ public class Converter extends BasicConverter { htmlDoc = new XhtmlDocument(getOutFileName(++outFileIndex,false),nType); htmlDoc.setConfig(config); if (template!=null) { htmlDoc.readFromTemplate(template); } - /*else if (bNeedHeaderFooter) { htmlDoc.createHeaderFooter(); }*/ outFiles.add(outFileIndex,htmlDoc); converterResult.addDocument(htmlDoc); - - // Create head + body - htmlDOM = htmlDoc.getContentDOM(); - Element rootElement = htmlDOM.getDocumentElement(); - styles.applyDefaultLanguage(rootElement); + Element rootElement = createHeadAndBody(); addEpubNs(rootElement); - rootElement.insertBefore(htmlDOM.createComment( - "This file was converted to xhtml by " - + (ofr.isText() ? "Writer" : (ofr.isSpreadsheet() ? "Calc" : "Impress")) - + "2xhtml ver. " + ConverterFactory.getVersion() + - ". See http://writer2latex.sourceforge.net for more info."), - rootElement.getFirstChild()); - - // Apply default writing direction - if (!ofr.isPresentation()) { - StyleInfo pageInfo = new StyleInfo(); - styles.getPageSc().applyDefaultWritingDirection(pageInfo); - styles.getPageSc().writeStyle(pageInfo,htmlDoc.getContentNode()); - } - - // Add title (required by xhtml) - Element title = htmlDoc.getTitleNode(); - if (title!=null) { - String sTitle = metaData.getTitle(); - if (sTitle==null) { // use filename as fallback - sTitle = htmlDoc.getFileName(); - } - title.appendChild( htmlDOM.createTextNode(sTitle) ); - } - - Element head = htmlDoc.getHeadNode(); - if (head!=null) { - // Declare charset (we need this for XHTML 1.0 strict and HTML5 because we have no ) - if (nType==XhtmlDocument.XHTML10) { - Element meta = htmlDOM.createElement("meta"); - meta.setAttribute("http-equiv","Content-Type"); - meta.setAttribute("content","text/html; charset="+htmlDoc.getEncoding().toLowerCase()); - head.appendChild(meta); - } - else if (nType==XhtmlDocument.HTML5) { - // The encoding should be UTF-8, but we still respect the user's setting - Element meta = htmlDOM.createElement("meta"); - meta.setAttribute("charset",htmlDoc.getEncoding().toUpperCase()); - head.appendChild(meta); - } - - // Add meta data (for EPUB the meta data belongs to the .opf file) - if (!bOPS) { - // "Traditional" meta data - //createMeta("generator","Writer2LaTeX "+Misc.VERSION); - createMeta(head,"description",metaData.getDescription()); - createMeta(head,"keywords",metaData.getKeywords()); - - // Dublin core meta data (optional) - // Format as recommended on dublincore.org (http://dublincore.org/documents/dc-html/) - // Declare meta data profile - if (config.xhtmlUseDublinCore()) { - head.setAttribute("profile","http://dublincore.org/documents/2008/08/04/dc-html/"); - // Add link to declare namespace - Element dclink = htmlDOM.createElement("link"); - dclink.setAttribute("rel","schema.DC"); - dclink.setAttribute("href","http://purl.org/dc/elements/1.1/"); - head.appendChild(dclink); - // Insert the actual meta data - createMeta(head,"DC.title",metaData.getTitle()); - // DC.subject actually contains subject+keywords, so we merge them - String sDCSubject = ""; - if (metaData.getSubject()!=null && metaData.getSubject().length()>0) { - sDCSubject = metaData.getSubject(); - } - if (metaData.getKeywords()!=null && metaData.getKeywords().length()>0) { - if (sDCSubject.length()>0) { sDCSubject+=", "; } - sDCSubject += metaData.getKeywords(); - } - createMeta(head,"DC.subject",sDCSubject); - createMeta(head,"DC.description",metaData.getDescription()); - createMeta(head,"DC.creator",metaData.getCreator()); - createMeta(head,"DC.date",metaData.getDate()); - createMeta(head,"DC.language",metaData.getLanguage()); - } - } - - // Add link to custom stylesheet, if producing normal XHTML - if (!bOPS && config.xhtmlCustomStylesheet().length()>0) { - Element htmlStyle = htmlDOM.createElement("link"); - htmlStyle.setAttribute("rel","stylesheet"); - htmlStyle.setAttribute("type","text/css"); - htmlStyle.setAttribute("media","all"); - htmlStyle.setAttribute("href",config.xhtmlCustomStylesheet()); - head.appendChild(htmlStyle); - } - - // Add link to generated stylesheet if producing normal XHTML and the user wants separate css - if (!bOPS && config.separateStylesheet() && config.xhtmlFormatting()>XhtmlConfig.IGNORE_STYLES) { - Element htmlStyle = htmlDOM.createElement("link"); - htmlStyle.setAttribute("rel","stylesheet"); - htmlStyle.setAttribute("type","text/css"); - htmlStyle.setAttribute("media","all"); - htmlStyle.setAttribute("href",sTargetFileName+"-styles.css"); - head.appendChild(htmlStyle); - } - - // Add link to included style sheet if producing OPS content - if (bOPS && styleSheet!=null) { - Element sty = htmlDOM.createElement("link"); - sty.setAttribute("rel", "stylesheet"); - sty.setAttribute("type", "text/css"); - sty.setAttribute("href", EPUB_CUSTOM_STYLESHEET); - head.appendChild(sty); - } - - // Add link to generated stylesheet if producing OPS content - if (isOPS() && config.xhtmlFormatting()>XhtmlConfig.IGNORE_STYLES) { - Element htmlStyle = htmlDOM.createElement("link"); - htmlStyle.setAttribute("rel","stylesheet"); - htmlStyle.setAttribute("type","text/css"); - htmlStyle.setAttribute("href",EPUB_STYLESHEET); - head.appendChild(htmlStyle); - } - - } - - // Recreate nested sections, if any - if (!textParser.sections.isEmpty()) { - Iterator iter = textParser.sections.iterator(); - while (iter.hasNext()) { - Element section = (Element) iter.next(); - String sStyleName = Misc.getAttribute(section,XMLString.TEXT_STYLE_NAME); - Element div = htmlDOM.createElement("div"); - htmlDoc.getContentNode().appendChild(div); - htmlDoc.setContentNode(div); - StyleInfo sectionInfo = new StyleInfo(); - styles.getSectionSc().readStyle(sStyleName,sectionInfo); - styles.getSectionSc().writeStyle(sectionInfo,div); - } - } - + applyWritingDirection(); + addTitle(); + addHeadData(); return htmlDoc.getContentNode(); } + + private void addTitle() { + Element title = htmlDoc.getTitleNode(); + if (title!=null) { + String sTitle = metaData.getTitle(); + if (sTitle==null) { // use filename as fallback + sTitle = htmlDoc.getFileName(); + } + title.appendChild( htmlDOM.createTextNode(sTitle) ); + } + } + + private Element createHeadAndBody() { + htmlDOM = htmlDoc.getContentDOM(); + Element rootElement = htmlDOM.getDocumentElement(); + styles.applyDefaultLanguage(rootElement); + return rootElement; + } + + private void applyWritingDirection() { + if (!ofr.isPresentation()) { + StyleInfo pageInfo = new StyleInfo(); + styles.getPageSc().applyDefaultWritingDirection(pageInfo); + styles.getPageSc().writeStyle(pageInfo,htmlDoc.getContentNode()); + } + } + + private void addHeadData() { + Element head = htmlDoc.getHeadNode(); + if (head!=null) { + // Declare charset (we need this for XHTML 1.0 strict and HTML5 because we have no ) + if (nType==XhtmlDocument.XHTML10) { + Element meta = htmlDOM.createElement("meta"); + meta.setAttribute("http-equiv","Content-Type"); + meta.setAttribute("content","text/html; charset="+htmlDoc.getEncoding().toLowerCase()); + head.appendChild(meta); + } + else if (nType==XhtmlDocument.HTML5) { + // The encoding should be UTF-8, but we still respect the user's setting + Element meta = htmlDOM.createElement("meta"); + meta.setAttribute("charset",htmlDoc.getEncoding().toUpperCase()); + head.appendChild(meta); + } + + // Add meta data (for EPUB the meta data belongs to the .opf file) + if (!bOPS) { + // "Traditional" meta data + //createMeta("generator","Writer2LaTeX "+Misc.VERSION); + createMeta(head,"description",metaData.getDescription()); + createMeta(head,"keywords",metaData.getKeywords()); + + // Dublin core meta data (optional) + // Format as recommended on dublincore.org (http://dublincore.org/documents/dc-html/) + // Declare meta data profile + if (config.xhtmlUseDublinCore()) { + head.setAttribute("profile","http://dublincore.org/documents/2008/08/04/dc-html/"); + // Add link to declare namespace + Element dclink = htmlDOM.createElement("link"); + dclink.setAttribute("rel","schema.DC"); + dclink.setAttribute("href","http://purl.org/dc/elements/1.1/"); + head.appendChild(dclink); + // Insert the actual meta data + createMeta(head,"DC.title",metaData.getTitle()); + // DC.subject actually contains subject+keywords, so we merge them + String sDCSubject = ""; + if (metaData.getSubject()!=null && metaData.getSubject().length()>0) { + sDCSubject = metaData.getSubject(); + } + if (metaData.getKeywords()!=null && metaData.getKeywords().length()>0) { + if (sDCSubject.length()>0) { sDCSubject+=", "; } + sDCSubject += metaData.getKeywords(); + } + createMeta(head,"DC.subject",sDCSubject); + createMeta(head,"DC.description",metaData.getDescription()); + createMeta(head,"DC.creator",metaData.getCreator()); + createMeta(head,"DC.date",metaData.getDate()); + createMeta(head,"DC.language",metaData.getLanguage()); + } + } + + // Add link to custom stylesheet, if producing normal XHTML + if (!bOPS && config.xhtmlCustomStylesheet().length()>0) { + Element htmlStyle = htmlDOM.createElement("link"); + htmlStyle.setAttribute("rel","stylesheet"); + htmlStyle.setAttribute("type","text/css"); + htmlStyle.setAttribute("media","all"); + htmlStyle.setAttribute("href",config.xhtmlCustomStylesheet()); + head.appendChild(htmlStyle); + } + + // Add link to generated stylesheet if producing normal XHTML and the user wants separate css + if (!bOPS && config.separateStylesheet() && config.xhtmlFormatting()>XhtmlConfig.IGNORE_STYLES) { + Element htmlStyle = htmlDOM.createElement("link"); + htmlStyle.setAttribute("rel","stylesheet"); + htmlStyle.setAttribute("type","text/css"); + htmlStyle.setAttribute("media","all"); + htmlStyle.setAttribute("href",sTargetFileName+"-styles.css"); + head.appendChild(htmlStyle); + } + + // Add link to included style sheet if producing OPS content + if (bOPS && styleSheet!=null) { + Element sty = htmlDOM.createElement("link"); + sty.setAttribute("rel", "stylesheet"); + sty.setAttribute("type", "text/css"); + sty.setAttribute("href", EPUB_CUSTOM_STYLESHEET); + head.appendChild(sty); + } + + // Add link to generated stylesheet if producing OPS content + if (isOPS() && config.xhtmlFormatting()>XhtmlConfig.IGNORE_STYLES) { + Element htmlStyle = htmlDOM.createElement("link"); + htmlStyle.setAttribute("rel","stylesheet"); + htmlStyle.setAttribute("type","text/css"); + htmlStyle.setAttribute("href",EPUB_STYLESHEET); + head.appendChild(htmlStyle); + } + + } + } // Add epub namespace for the purpose of semantic inflection in EPUB 3 public void addEpubNs(Element elm) { diff --git a/src/main/java/writer2latex/xhtml/content/TextParser.java b/src/main/java/writer2latex/xhtml/content/TextParser.java index 6885ba0..f2a03ff 100644 --- a/src/main/java/writer2latex/xhtml/content/TextParser.java +++ b/src/main/java/writer2latex/xhtml/content/TextParser.java @@ -71,7 +71,6 @@ public class TextParser extends Parser { private int nLastSplitLevel = 1; // The outline level at which the last split occurred private int nDontSplitLevel = 0; // if > 0 splitting is forbidden boolean bAfterHeading=false; // last element was a top level heading - public Stack sections = new Stack(); // stack of nested sections Element[] currentHeading = new Element[7]; // Last headings (repeated when splitting) private int nCharacterCount = 0; // The number of text characters in the current document