Experiment with support for multiple lines within a paragraph.
authorMathieu Baudier <mbaudier@argeo.org>
Thu, 21 Jan 2021 09:15:45 +0000 (10:15 +0100)
committerMathieu Baudier <mbaudier@argeo.org>
Thu, 21 Jan 2021 09:15:45 +0000 (10:15 +0100)
publishing/org.argeo.publishing.ui/src/org/argeo/cms/text/IdentityTextInterpreter.java
publishing/org.argeo.publishing.ui/src/org/argeo/cms/text/TextInterpreter.java
publishing/org.argeo.publishing.ui/src/org/argeo/cms/text/TextInterpreterImpl.java
publishing/org.argeo.publishing.ui/src/org/argeo/docbook/ui/AbstractDbkViewer.java
publishing/org.argeo.publishing.ui/src/org/argeo/docbook/ui/DbkTextInterpreter.java

index 5c019e59b6191298af58b8c499f7138b5f9d3356..ce59ed6630f2bb3c613ffeff53574c00c79d1c9f 100644 (file)
@@ -20,8 +20,7 @@ public class IdentityTextInterpreter implements TextInterpreter, CmsNames {
                                        validateBeforeStoring(raw);
                                        node.setProperty(CMS_CONTENT, raw);
                                } else {
                                        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;
                                }
                        } 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 {
        @Override
        public String read(Item item) {
                try {
@@ -58,8 +62,7 @@ public class IdentityTextInterpreter implements TextInterpreter, CmsNames {
 
                                        return node.getProperty(CMS_CONTENT).getString();
                                } else {
 
                                        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;
                                }
                        } 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. */
        }
 
        /** 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. */
                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;
        }
 }
                return content;
        }
 }
index f39a2b3291383b9c5c86c26030968fc5ed629728..5c94e669c06cdd5ad9fd948ec330baaca332cefa 100644 (file)
@@ -4,9 +4,11 @@ import javax.jcr.Item;
 
 /** Convert from/to data layer to/from presentation layer. */
 public interface TextInterpreter {
 
 /** 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);
 }
 }
index f9ee195a79960259916399f941f60556a82b694f..813fa0fcc9e9601fdf71b5f8ecd7c3d2bc52aad5 100644 (file)
@@ -8,8 +8,7 @@ import javax.jcr.RepositoryException;
  * specific formatting and integration.
  */
 class TextInterpreterImpl extends IdentityTextInterpreter {
  * specific formatting and integration.
  */
 class TextInterpreterImpl extends IdentityTextInterpreter {
-       private MarkupValidatorCopy markupValidator = MarkupValidatorCopy
-                       .getInstance();
+       private MarkupValidatorCopy markupValidator = MarkupValidatorCopy.getInstance();
 
        @Override
        protected void validateBeforeStoring(String raw) {
 
        @Override
        protected void validateBeforeStoring(String raw) {
@@ -17,14 +16,12 @@ class TextInterpreterImpl extends IdentityTextInterpreter {
        }
 
        @Override
        }
 
        @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
                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);
        }
 
                return super.convertFromStorage(item, content);
        }
 
index b21f502eb43c2efe3f76bf8fa9ad3a3aaa0f1429..45737180acd3fc15dab39152efcba9c3844ffaca 100644 (file)
@@ -238,9 +238,9 @@ public abstract class AbstractDbkViewer extends AbstractPageViewer implements Ke
                        if (part instanceof EditableText) {
                                EditableText paragraph = (EditableText) part;
                                if (paragraph == getEdited())
                        if (part instanceof EditableText) {
                                EditableText paragraph = (EditableText) part;
                                if (paragraph == getEdited())
-                                       paragraph.setText(textInterpreter.read(partNode));
-                               else
                                        paragraph.setText(textInterpreter.raw(partNode));
                                        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());
                        } 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();
 
                                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) {
                        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') {
 //                             cancelEdit();
                                saveEdit();
                        } else if (ke.character == '\r') {
-                               splitEdit();
+                               if (!shiftPressed)
+                                       splitEdit();
                        } else if (ke.character == 'z') {
                                if (ctrlPressed)
                                        cancelEdit();
                        } else if (ke.character == 'z') {
                                if (ctrlPressed)
                                        cancelEdit();
index 6ca50251835557481d1340b7277cad9a06562389..25056efb7bd5e03b65e43431565583ba56893f54 100644 (file)
@@ -1,10 +1,15 @@
 package org.argeo.docbook.ui;
 
 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 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;
 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;
                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;
                                        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;
                        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
                                        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);
                                        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 = "<br/>".length();
+
+       public String readSimpleHtml(Item item) {
+               String raw = raw(item);
+//             raw = "<span style='text-align:justify'>" + raw + "</span>";
+               if (raw.length() == 0)
+                       return raw;
+               try (StringReader reader = new StringReader(raw)) {
+                       List<String> 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("<br/>");
+                               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("<br/>");
+//                     sb.append(lines[i]);
+//             }
+//             return sb.toString();
+       }
+
        // EXTENSIBILITY
        /**
         * To be overridden, in order to make sure that only valid strings are being
        // EXTENSIBILITY
        /**
         * To be overridden, in order to make sure that only valid strings are being