Add support for before/after attributes in xhtml style maps

git-svn-id: svn://svn.code.sf.net/p/writer2latex/code/trunk@187 f0f2a975-2e09-46c8-9428-3b39399b9f3c
This commit is contained in:
henrikjust 2014-10-27 06:56:26 +00:00
parent 049bf052bc
commit de72a4f49e
16 changed files with 201 additions and 86 deletions

View file

@ -20,7 +20,7 @@
*
* All Rights Reserved.
*
* Version 1.4 (2014-09-16)
* Version 1.6 (2014-10-24)
*
*/package writer2latex.xhtml;
@ -82,9 +82,10 @@ public class HeadingStyleConverter extends StyleConverterHelper {
String sDisplayName = style.getDisplayName();
if (styleMap.contains(sDisplayName)) {
// Apply attributes as specified in style map from user
info.sTagName = styleMap.getBlockElement(sDisplayName);
if (!"(none)".equals(styleMap.getBlockCss(sDisplayName))) {
info.sClass = styleMap.getBlockCss(sDisplayName);
XhtmlStyleMapItem map = styleMap.get(sDisplayName);
info.sTagName = map.sBlockElement;
if (!"(none)".equals(map.sBlockCss)) {
info.sClass = map.sBlockCss;
}
}
else {
@ -113,9 +114,10 @@ public class HeadingStyleConverter extends StyleConverterHelper {
String sDisplayName = style.getDisplayName();
if (styleMap.contains(sDisplayName)) {
// Apply attributes as specified in style map from user
info.sTagName = styleMap.getElement(sDisplayName);
if (!"(none)".equals(styleMap.getCss(sDisplayName))) {
info.sClass = styleMap.getCss(sDisplayName);
XhtmlStyleMapItem map = styleMap.get(sDisplayName);
info.sTagName = map.sElement;
if (!"(none)".equals(map.sCss)) {
info.sClass = map.sCss;
}
}
}

View file

@ -20,7 +20,7 @@
*
* All Rights Reserved.
*
* Version 1.4 (2014-09-16)
* Version 1.6 (2014-10-24)
*
*/
@ -64,11 +64,12 @@ public class ListStyleConverter extends StyleConverterHelper {
else {
String sDisplayName = style.getDisplayName();
if (styleMap.contains(sDisplayName)) {
if (styleMap.getElement(sDisplayName).length()>0) {
info.sTagName = styleMap.getElement(sDisplayName);
XhtmlStyleMapItem map = styleMap.get(sDisplayName);
if (map.sElement.length()>0) {
info.sTagName = map.sElement;
}
if (!"(none)".equals(styleMap.getCss(sDisplayName))) {
info.sClass = styleMap.getCss(sDisplayName);
if (!"(none)".equals(map.sCss)) {
info.sClass = map.sCss;
}
}
else {

View file

@ -20,7 +20,7 @@
*
* All Rights Reserved.
*
* Version 1.4 (2014-09-16)
* Version 1.6 (2014-10-24)
*
*/
@ -73,11 +73,12 @@ public abstract class StyleWithPropertiesConverterHelper
String sDisplayName = style.getDisplayName();
if (styleMap.contains(sDisplayName)) {
// Apply attributes as specified in style map from user
if (styleMap.getElement(sDisplayName).length()>0) {
info.sTagName = styleMap.getElement(sDisplayName);
XhtmlStyleMapItem map = styleMap.get(sDisplayName);
if (map.sElement.length()>0) {
info.sTagName = map.sElement;
}
if (!"(none)".equals(styleMap.getCss(sDisplayName))) {
info.sClass = styleMap.getCss(sDisplayName);
if (!"(none)".equals(map.sCss)) {
info.sClass = map.sCss;
}
}
else {

View file

@ -20,7 +20,7 @@
*
* All Rights Reserved.
*
* Version 1.4 (2014-09-23)
* Version 1.6 (2014-10-26)
*
*/
@ -395,12 +395,13 @@ public class TextConverter extends ConverterHelper {
if (sDisplayName!=null && xpar.contains(sDisplayName)) {
Node curHnode = hnode;
String sBlockElement = xpar.getBlockElement(sDisplayName);
String sBlockCss = xpar.getBlockCss(sDisplayName);
if (xpar.getBlockElement(sDisplayName).length()>0) {
Element block = converter.createElement(xpar.getBlockElement(sDisplayName));
if (!"(none)".equals(xpar.getBlockCss(sDisplayName))) {
block.setAttribute("class",xpar.getBlockCss(sDisplayName));
XhtmlStyleMapItem map = xpar.get(sDisplayName);
String sBlockElement = map.sBlockElement;
String sBlockCss = map.sBlockCss;
if (map.sBlockElement.length()>0) {
Element block = converter.createElement(map.sBlockElement);
if (!"(none)".equals(map.sBlockCss)) {
block.setAttribute("class",map.sBlockCss);
}
hnode.appendChild(block);
curHnode = block;
@ -415,8 +416,9 @@ public class TextConverter extends ConverterHelper {
if (cnodeName.equals(XMLString.TEXT_P)) {
String sCurDisplayName = ofr.getParStyles().getDisplayName(Misc.getAttribute(child,XMLString.TEXT_STYLE_NAME));
if (sCurDisplayName!=null && xpar.contains(sCurDisplayName)) {
if (sBlockElement.equals(xpar.getBlockElement(sCurDisplayName)) &&
sBlockCss.equals(xpar.getBlockCss(sCurDisplayName))) {
XhtmlStyleMapItem newmap = xpar.get(sCurDisplayName);
if (sBlockElement.equals(newmap.sBlockElement) &&
sBlockCss.equals(newmap.sBlockCss)) {
bMoreParagraphs = true;
}
}
@ -701,7 +703,7 @@ public class TextConverter extends ConverterHelper {
if (!bUnNumbered) {
insertListLabel(listStyle,nListLevel,"SectionNumber",null,sLabel,heading);
}
// Add to toc
if (!bInToc) {
String sTarget = "toc"+(++nTocIndex);
@ -734,7 +736,11 @@ public class TextConverter extends ConverterHelper {
applyStyle(innerInfo, content);
}
traverseInlineText(onode,content);
// Keep track of current headings for split output
// Add before/after text if required
addBeforeAfter(heading,ofr.getParStyle(getParSc().getRealParStyleName(sStyleName)),config.getXHeadingStyleMap());
// Keep track of current headings for split output
currentHeading[nLevel] = heading;
for (int i=nLevel+1; i<=6; i++) {
currentHeading[i] = null;
@ -817,10 +823,14 @@ public class TextConverter extends ConverterHelper {
sCurrentListLabel = null;
}
// Finally, in EPUB export, if the exported paragraph turns out to be empty, remove it
if (converter.isOPS() && !par.hasChildNodes()) {
// Finally, in EPUB export, if the exported paragraph turns out to be empty, remove it
hnode.removeChild(par);
}
else {
// Otherwise, add before/after text if required
addBeforeAfter(par,ofr.getParStyle(getParSc().getRealParStyleName(sStyleName)),config.getXParStyleMap());
}
}
private void prependAsapNode(Node node) {
@ -1979,6 +1989,26 @@ public class TextConverter extends ConverterHelper {
// UTILITY METHODS
///////////////////////////////////////////////////////////////////////////
// Insert text before/after in an element
private void addBeforeAfter(Element elm, StyleWithProperties style, XhtmlStyleMap styleMap) {
if (style!=null && styleMap.contains(style.getDisplayName())) {
XhtmlStyleMapItem mapItem = styleMap.get(style.getDisplayName());
if (mapItem.sBefore!=null && mapItem.sBefore.length()>0) {
Node child = elm.getFirstChild();
if (child!=null) {
elm.insertBefore(converter.createTextNode(mapItem.sBefore),child);
}
else {
elm.appendChild(converter.createTextNode(mapItem.sBefore));
}
}
if (mapItem.sAfter!=null && mapItem.sAfter.length()>0) {
elm.appendChild(converter.createTextNode(mapItem.sAfter));
}
}
}
// Methods to query individual formatting properties (no inheritance)
// Does this style contain the bold attribute?
@ -2079,10 +2109,11 @@ public class TextConverter extends ConverterHelper {
private Element applyAttribute(Element node, String sAttr, boolean bApply) {
if (bApply) {
XhtmlStyleMap xattr = config.getXAttrStyleMap();
if (xattr.contains(sAttr) && xattr.getElement(sAttr).length()>0) {
Element attr = converter.createElement(xattr.getElement(sAttr));
if (!"(none)".equals(xattr.getCss(sAttr))) {
attr.setAttribute("class",xattr.getCss(sAttr));
if (xattr.contains(sAttr) && xattr.get(sAttr).sElement.length()>0) {
XhtmlStyleMapItem map = xattr.get(sAttr);
Element attr = converter.createElement(map.sElement);
if (!"(none)".equals(map.sCss)) {
attr.setAttribute("class",map.sCss);
}
node.appendChild(attr);
return attr;
@ -2157,6 +2188,4 @@ public class TextConverter extends ConverterHelper {
Misc.getPosInteger(node.getAttribute(XMLString.TEXT_LEVEL),1);
}
}

View file

@ -20,7 +20,7 @@
*
* All Rights Reserved.
*
* Version 1.4 (2014-09-16)
* Version 1.6 (2014-10-24)
*
*/
@ -107,8 +107,9 @@ public class TextStyleConverter extends StyleWithPropertiesConverterHelper {
// Look for a style map
String sDisplayName = ofr.getTextStyles().getDisplayName(sStyleName);
if (styleMap.contains(sDisplayName)) { // class name from config
if (!"(none)".equals(styleMap.getCss(sDisplayName))) {
info.sClass = styleMap.getCss(sDisplayName);
XhtmlStyleMapItem map = styleMap.get(sDisplayName);
if (!"(none)".equals(map.sCss)) {
info.sClass = map.sCss;
}
return;
}

View file

@ -20,7 +20,7 @@
*
* All Rights Reserved.
*
* Version 1.4 (2014-09-23)
* Version 1.6 (2014-10-24)
*
*/
@ -295,27 +295,38 @@ public class XhtmlConfig extends writer2latex.base.ConfigBase {
if (sFamily.length()==0) { // try old name
sFamily = elm.getAttribute("class");
}
Map<String,String> attr = new HashMap<String,String>();
String sElement = elm.getAttribute("element");
String sCss = elm.getAttribute("css");
if (sCss.length()==0) { sCss="(none)"; }
Map<String,String> attr = new HashMap<String,String>();
attr.put("element", sElement);
attr.put("css", sCss);
String sBlockElement = elm.getAttribute("block-element");
String sBlockCss = elm.getAttribute("block-css");
if (sBlockCss.length()==0) { sBlockCss="(none)"; }
String sBefore = elm.getAttribute("before");
String sAfter = elm.getAttribute("after");
if ("heading".equals(sFamily)) {
attr.put("block-element", sBlockElement);
attr.put("block-css", sBlockCss);
attr.put("before", sBefore);
attr.put("after", sAfter);
xheading.put(sName,attr);
}
if ("paragraph".equals(sFamily)) {
attr.put("block-element", sBlockElement);
attr.put("block-css", sBlockCss);
attr.put("before", sBefore);
attr.put("after", sAfter);
xpar.put(sName,attr);
}
else if ("text".equals(sFamily)) {
attr.put("before", sBefore);
attr.put("after", sAfter);
xtext.put(sName,attr);
}
else if ("frame".equals(sFamily)) {
@ -351,6 +362,8 @@ public class XhtmlConfig extends writer2latex.base.ConfigBase {
smNode.setAttribute("css",attr.get("css"));
if (attr.containsKey("block-element")) smNode.setAttribute("block-element",attr.get("block-element"));
if (attr.containsKey("block-css")) smNode.setAttribute("block-css",attr.get("block-css"));
if (attr.containsKey("before")) smNode.setAttribute("before",attr.get("before"));
if (attr.containsKey("after")) smNode.setAttribute("after",attr.get("after"));
dom.getDocumentElement().appendChild(smNode);
}
}
@ -428,7 +441,9 @@ public class XhtmlConfig extends writer2latex.base.ConfigBase {
String sCss = attr.containsKey("css") ? attr.get("css") : "";
String sBlockElement = attr.containsKey("block-element") ? attr.get("block-element") : "";
String sBlockCss = attr.containsKey("block-css") ? attr.get("block-css") : "";
map.put(sName, sBlockElement, sBlockCss, sElement, sCss);
String sBefore = attr.containsKey("before") ? attr.get("before") : "";
String sAfter = attr.containsKey("after") ? attr.get("after") : "";
map.put(sName, new XhtmlStyleMapItem(sBlockElement, sBlockCss, sElement, sCss, sBefore, sAfter));
}
return map;

View file

@ -16,54 +16,37 @@
* Foundation, Inc., 59 Temple Place, Suite 330, Boston,
* MA 02111-1307 USA
*
* Copyright: 2002-2003 by Henrik Just
* Copyright: 2002-2014 by Henrik Just
*
* All Rights Reserved.
*
* Version 0.3.2 (2003-11-25)
* Version 1.6 (2014-10-24)
*
*/
package writer2latex.xhtml;
import java.util.Hashtable;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
public class XhtmlStyleMap {
private Hashtable<String, String> blockElement = new Hashtable<String, String>();
private Hashtable<String, String> blockCss = new Hashtable<String, String>();
private Hashtable<String, String> element = new Hashtable<String, String>();
private Hashtable<String, String> css = new Hashtable<String, String>();
public void put(String sName, String sBlockElement, String sBlockCss, String sElement, String sCss) {
blockElement.put(sName,sBlockElement);
blockCss.put(sName,sBlockCss);
element.put(sName,sElement);
css.put(sName,sCss);
private Map<String,XhtmlStyleMapItem> items = new HashMap<String,XhtmlStyleMapItem>();
public boolean contains(String sName) {
return sName!=null && items.containsKey(sName);
}
public boolean contains(String sName) {
return sName!=null && element.containsKey(sName);
}
public String getBlockElement(String sName) {
return blockElement.get(sName);
public void put(String sName, XhtmlStyleMapItem item) {
items.put(sName, item);
}
public String getBlockCss(String sName) {
return blockCss.get(sName);
}
public String getElement(String sName) {
return element.get(sName);
public XhtmlStyleMapItem get(String sName) {
return items.get(sName);
}
public String getCss(String sName) {
return css.get(sName);
}
public Enumeration<String> getNames() {
return element.keys();
public Iterator<String> getNames() {
return items.keySet().iterator();
}
}

View file

@ -0,0 +1,47 @@
/************************************************************************
*
* XhtmlStyleMapItem.java
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License version 2.1, as published by the Free Software Foundation.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston,
* MA 02111-1307 USA
*
* Copyright: 2002-2014 by Henrik Just
*
* All Rights Reserved.
*
* Version 1.6 (2014-10-24)
*
*/
package writer2latex.xhtml;
/** This is a simple struct to hold data about a single style map
*/
public class XhtmlStyleMapItem {
public String sBlockElement=null;
public String sBlockCss=null;
public String sElement=null;
public String sCss=null;
public String sBefore=null;
public String sAfter=null;
public XhtmlStyleMapItem(String sBlockElement, String sBlockCss, String sElement, String sCss, String sBefore, String sAfter) {
this.sBlockElement=sBlockElement;
this.sBlockCss=sBlockCss;
this.sElement=sElement;
this.sCss=sCss;
this.sBefore=sBefore;
this.sAfter=sAfter;
}
}