From 7b6b34f956c231ec4dc7aab0acf85c6e7b592e39 Mon Sep 17 00:00:00 2001 From: Mathieu Baudier Date: Thu, 21 Jan 2021 10:15:45 +0100 Subject: [PATCH] Experiment with support for multiple lines within a paragraph. --- .../cms/text/IdentityTextInterpreter.java | 17 +++---- .../org/argeo/cms/text/TextInterpreter.java | 8 ++-- .../argeo/cms/text/TextInterpreterImpl.java | 9 ++-- .../argeo/docbook/ui/AbstractDbkViewer.java | 10 +++-- .../argeo/docbook/ui/DbkTextInterpreter.java | 45 +++++++++++++++++-- 5 files changed, 65 insertions(+), 24 deletions(-) diff --git a/publishing/org.argeo.publishing.ui/src/org/argeo/cms/text/IdentityTextInterpreter.java b/publishing/org.argeo.publishing.ui/src/org/argeo/cms/text/IdentityTextInterpreter.java index 5c019e5..ce59ed6 100644 --- a/publishing/org.argeo.publishing.ui/src/org/argeo/cms/text/IdentityTextInterpreter.java +++ b/publishing/org.argeo.publishing.ui/src/org/argeo/cms/text/IdentityTextInterpreter.java @@ -20,8 +20,7 @@ public class IdentityTextInterpreter implements TextInterpreter, CmsNames { validateBeforeStoring(raw); node.setProperty(CMS_CONTENT, raw); } else { - throw new CmsException("Don't know how to interpret " - + node); + throw new CmsException("Don't know how to interpret " + node); } } else {// property Property property = (Property) item; @@ -33,6 +32,11 @@ public class IdentityTextInterpreter implements TextInterpreter, CmsNames { } } + @Override + public String readSimpleHtml(Item item) { + return raw(item); + } + @Override public String read(Item item) { try { @@ -58,8 +62,7 @@ public class IdentityTextInterpreter implements TextInterpreter, CmsNames { return node.getProperty(CMS_CONTENT).getString(); } else { - throw new CmsException("Don't know how to interpret " - + node); + throw new CmsException("Don't know how to interpret " + node); } } else {// property Property property = (Property) item; @@ -79,15 +82,13 @@ public class IdentityTextInterpreter implements TextInterpreter, CmsNames { } /** To be overridden, in order to support additional formatting. */ - protected String convertToStorage(Item item, String content) - throws RepositoryException { + protected String convertToStorage(Item item, String content) throws RepositoryException { return content; } /** To be overridden, in order to support additional formatting. */ - protected String convertFromStorage(Item item, String content) - throws RepositoryException { + protected String convertFromStorage(Item item, String content) throws RepositoryException { return content; } } diff --git a/publishing/org.argeo.publishing.ui/src/org/argeo/cms/text/TextInterpreter.java b/publishing/org.argeo.publishing.ui/src/org/argeo/cms/text/TextInterpreter.java index f39a2b3..5c94e66 100644 --- a/publishing/org.argeo.publishing.ui/src/org/argeo/cms/text/TextInterpreter.java +++ b/publishing/org.argeo.publishing.ui/src/org/argeo/cms/text/TextInterpreter.java @@ -4,9 +4,11 @@ import javax.jcr.Item; /** Convert from/to data layer to/from presentation layer. */ public interface TextInterpreter { - public String raw(Item item); + String raw(Item item); - public String read(Item item); + String read(Item item); - public void write(Item item, String content); + String readSimpleHtml(Item item); + + void write(Item item, String content); } diff --git a/publishing/org.argeo.publishing.ui/src/org/argeo/cms/text/TextInterpreterImpl.java b/publishing/org.argeo.publishing.ui/src/org/argeo/cms/text/TextInterpreterImpl.java index f9ee195..813fa0f 100644 --- a/publishing/org.argeo.publishing.ui/src/org/argeo/cms/text/TextInterpreterImpl.java +++ b/publishing/org.argeo.publishing.ui/src/org/argeo/cms/text/TextInterpreterImpl.java @@ -8,8 +8,7 @@ import javax.jcr.RepositoryException; * specific formatting and integration. */ class TextInterpreterImpl extends IdentityTextInterpreter { - private MarkupValidatorCopy markupValidator = MarkupValidatorCopy - .getInstance(); + private MarkupValidatorCopy markupValidator = MarkupValidatorCopy.getInstance(); @Override protected void validateBeforeStoring(String raw) { @@ -17,14 +16,12 @@ class TextInterpreterImpl extends IdentityTextInterpreter { } @Override - protected String convertToStorage(Item item, String content) - throws RepositoryException { + protected String convertToStorage(Item item, String content) throws RepositoryException { return super.convertToStorage(item, content); } @Override - protected String convertFromStorage(Item item, String content) - throws RepositoryException { + protected String convertFromStorage(Item item, String content) throws RepositoryException { return super.convertFromStorage(item, content); } diff --git a/publishing/org.argeo.publishing.ui/src/org/argeo/docbook/ui/AbstractDbkViewer.java b/publishing/org.argeo.publishing.ui/src/org/argeo/docbook/ui/AbstractDbkViewer.java index b21f502..4573718 100644 --- a/publishing/org.argeo.publishing.ui/src/org/argeo/docbook/ui/AbstractDbkViewer.java +++ b/publishing/org.argeo.publishing.ui/src/org/argeo/docbook/ui/AbstractDbkViewer.java @@ -238,9 +238,9 @@ public abstract class AbstractDbkViewer extends AbstractPageViewer implements Ke if (part instanceof EditableText) { EditableText paragraph = (EditableText) part; if (paragraph == getEdited()) - paragraph.setText(textInterpreter.read(partNode)); - else paragraph.setText(textInterpreter.raw(partNode)); + else + paragraph.setText(textInterpreter.readSimpleHtml(partNode)); } else if (part instanceof EditableImage) { EditableImage editableImage = (EditableImage) part; imageManager.load(partNode, part.getControl(), editableImage.getPreferredImageSize()); @@ -265,7 +265,8 @@ public abstract class AbstractDbkViewer extends AbstractPageViewer implements Ke return; String text = ((Text) et.getControl()).getText(); - String[] lines = text.split("[\r\n]+"); + // String[] lines = text.split("[\r\n]+"); + String[] lines = { text }; assert lines.length != 0; saveLine(part, lines[0]); if (lines.length > 1) { @@ -777,7 +778,8 @@ public abstract class AbstractDbkViewer extends AbstractPageViewer implements Ke // cancelEdit(); saveEdit(); } else if (ke.character == '\r') { - splitEdit(); + if (!shiftPressed) + splitEdit(); } else if (ke.character == 'z') { if (ctrlPressed) cancelEdit(); 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 6ca5025..25056ef 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,15 @@ package org.argeo.docbook.ui; +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 +23,7 @@ public class DbkTextInterpreter implements TextInterpreter { try { if (item instanceof Node) { Node node = (Node) item; - if (node.isNodeType(DocBookTypes.PARA)||node.isNodeType(DocBookTypes.TITLE)) { + if (node.isNodeType(DocBookTypes.PARA) || node.isNodeType(DocBookTypes.TITLE)) { String raw = convertToStorage(node, content); validateBeforeStoring(raw); Node jcrText; @@ -56,11 +61,12 @@ public class DbkTextInterpreter implements TextInterpreter { item.getSession().refresh(true); if (item instanceof Node) { Node node = (Node) item; - if (node.isNodeType(DocBookTypes.PARA)||node.isNodeType(DocBookTypes.TITLE)) { + if (node.isNodeType(DocBookTypes.PARA) || node.isNodeType(DocBookTypes.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 +80,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 -- 2.30.2