X-Git-Url: https://git.argeo.org/?p=gpl%2Fargeo-suite.git;a=blobdiff_plain;f=publishing%2Forg.argeo.publishing.ui%2Fsrc%2Forg%2Fargeo%2Fdocbook%2Fui%2FDbkTextInterpreter.java;h=f5ab9975dcf2b59433f61eed1b6dd9350762756a;hp=51af55e46f02544b1f983515330a914dc57c3ac3;hb=4bd6cf0556f597ee73c8f13df45019ccf9e418a6;hpb=9dd16d64cc4ea274c33a4c1acaa289c46bf5251f diff --git a/publishing/org.argeo.publishing.ui/src/org/argeo/docbook/ui/DbkTextInterpreter.java b/publishing/org.argeo.publishing.ui/src/org/argeo/docbook/ui/DbkTextInterpreter.java index 51af55e..f5ab997 100644 --- a/publishing/org.argeo.publishing.ui/src/org/argeo/docbook/ui/DbkTextInterpreter.java +++ b/publishing/org.argeo.publishing.ui/src/org/argeo/docbook/ui/DbkTextInterpreter.java @@ -1,10 +1,19 @@ package org.argeo.docbook.ui; +import static org.argeo.docbook.DbkUtils.isDbk; +import static org.argeo.docbook.DocBookType.para; +import static org.argeo.docbook.DocBookType.title; + +import java.io.IOException; +import java.io.StringReader; +import java.util.List; + import javax.jcr.Item; import javax.jcr.Node; import javax.jcr.Property; import javax.jcr.RepositoryException; +import org.apache.commons.io.IOUtils; import org.argeo.cms.text.TextInterpreter; import org.argeo.jcr.Jcr; import org.argeo.jcr.JcrException; @@ -18,7 +27,7 @@ public class DbkTextInterpreter implements TextInterpreter { try { if (item instanceof Node) { Node node = (Node) item; - if (node.isNodeType(DocBookTypes.PARA)) { + if (isDbk(node, para) || isDbk(node, title)) { String raw = convertToStorage(node, content); validateBeforeStoring(raw); Node jcrText; @@ -56,11 +65,12 @@ public class DbkTextInterpreter implements TextInterpreter { item.getSession().refresh(true); if (item instanceof Node) { Node node = (Node) item; - if (node.isNodeType(DocBookTypes.PARA)) { + if (isDbk(node, para) || isDbk(node, title)) { Node jcrText = node.getNode(Jcr.JCR_XMLTEXT); String txt = jcrText.getProperty(Jcr.JCR_XMLCHARACTERS).getString(); // TODO make it more robust - txt = txt.replace("\n", "").replace("\t", ""); + // txt = txt.replace("\n", "").replace("\t", ""); + txt = txt.replace("\t", " "); return txt; } else { throw new IllegalArgumentException("Don't know how to interpret " + node); @@ -74,6 +84,39 @@ public class DbkTextInterpreter implements TextInterpreter { } } + final static int BR_LENGTH = "
".length(); + + public String readSimpleHtml(Item item) { + String raw = raw(item); +// raw = "" + raw + ""; + if (raw.length() == 0) + return raw; + try (StringReader reader = new StringReader(raw)) { + List lines = IOUtils.readLines(reader); + if (lines.size() == 1) + return lines.get(0); + StringBuilder sb = new StringBuilder(raw.length() + lines.size() * BR_LENGTH); + for (int i = 0; i < lines.size(); i++) { + if (i != 0) + sb.append("
"); + sb.append(lines.get(i)); + } + return sb.toString(); + } catch (IOException e) { + throw new RuntimeException(e); + } +// String[] lines = raw.split("[\r\n]+"); +// if (lines.length == 1) +// return lines[0]; +// StringBuilder sb = new StringBuilder(raw.length() + lines.length * BR_LENGTH); +// for (int i = 0; i < lines.length; i++) { +// if (i != 0) +// sb.append("
"); +// sb.append(lines[i]); +// } +// return sb.toString(); + } + // EXTENSIBILITY /** * To be overridden, in order to make sure that only valid strings are being