Skip to content

Commit

Permalink
Merge pull request #63 from damienbeaufils/supportLineBreaks
Browse files Browse the repository at this point in the history
Convert line separators in JSON to line breaks in HTML
  • Loading branch information
srenault authored Nov 16, 2017
2 parents 5389b63 + cea9689 commit 410d5d8
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 5 deletions.
2 changes: 1 addition & 1 deletion .mvn/wrapper/maven-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1 +1 @@
distributionUrl=https://repo1.maven.org/maven2/org/apache/maven/apache-maven/3.5.0/apache-maven-3.5.0-bin.zip
distributionUrl=https://repo1.maven.org/maven2/org/apache/maven/apache-maven/3.5.2/apache-maven-3.5.2-bin.zip
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<groupId>io.prismic</groupId>
<artifactId>java-kit</artifactId>
<packaging>jar</packaging>
<version>1.6.2</version>
<version>1.6.3-SNAPSHOT</version>
<name>java-kit</name>
<description>The developer kit to access Prismic.io repositories using the Java language.</description>
<url>http://maven.apache.org</url>
Expand Down
6 changes: 5 additions & 1 deletion src/main/java/io/prismic/Fragment.java
Original file line number Diff line number Diff line change
Expand Up @@ -1205,7 +1205,7 @@ else if(block instanceof Block.ListItem && ((Block.ListItem)block).isOrdered())
}
}
}
return html.toString();
return convertLineSeparatorsToHtmlLineBreaks(html.toString());
}

public String asHtml(Block block, LinkResolver linkResolver, HtmlSerializer htmlSerializer) {
Expand Down Expand Up @@ -1268,6 +1268,10 @@ public Tuple(X x, Y y) {
}
}

private String convertLineSeparatorsToHtmlLineBreaks(String html) {
return html.replaceAll("\n", "<br/>");
}

private static String serialize(Span span, String content, LinkResolver linkResolver, HtmlSerializer htmlSerializer) {
if (htmlSerializer != null) {
String customHtml = htmlSerializer.serialize(span, content);
Expand Down
14 changes: 12 additions & 2 deletions src/test/java/io/prismic/DocumentTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import junit.framework.Assert;

import org.joda.time.DateTime;
import org.joda.time.DateTimeZone;
import org.junit.Test;
Expand Down Expand Up @@ -246,7 +245,6 @@ public void labelSpans() throws Exception {
String jsonString = "{\"type\":\"StructuredText\",\"value\":[{\"type\":\"paragraph\",\"text\":\"To query your API, you will need to specify a form and a reference in addition to your query.\",\"spans\":[{\"start\":46,\"end\":50,\"type\":\"strong\"},{\"start\":57,\"end\":67,\"type\":\"strong\"},{\"start\":78,\"end\":92,\"type\":\"strong\"}]},{\"type\":\"list-item\",\"text\":\"The operator: this is the function you call to build the predicate, for example Predicate.at.\",\"spans\":[{\"start\":4,\"end\":12,\"type\":\"em\"},{\"start\":80,\"end\":92,\"type\":\"label\",\"data\":{\"label\":\"codespan\"}}]},{\"type\":\"list-item\",\"text\":\"The fragment: the first argument you pass, for example document.id.\",\"spans\":[{\"start\":4,\"end\":12,\"type\":\"em\"},{\"start\":55,\"end\":67,\"type\":\"label\",\"data\":{\"label\":\"codespan\"}}]},{\"type\":\"list-item\",\"text\":\"The values: the other arguments you pass, usually one but it can be more for some predicates. For example product.\",\"spans\":[{\"start\":4,\"end\":10,\"type\":\"em\"},{\"start\":106,\"end\":113,\"type\":\"label\",\"data\":{\"label\":\"codespan\"}}]}]}";
JsonNode json = mapper.readTree(jsonString);
Fragment.StructuredText text = Fragment.StructuredText.parse(json.path("value"));
System.out.println(text.asHtml(linkResolver));
Assert.assertEquals(
"<p>To query your API, you will need to specify a <strong>form</strong> and a <strong>reference </strong>in addition<strong> to your query</strong>.</p><ul><li>The <em>operator</em>: this is the function you call to build the predicate, for example <span class=\"codespan\">Predicate.at</span>.</li><li>The <em>fragment</em>: the first argument you pass, for example <span class=\"codespan\">document.id.</span></li><li>The <em>values</em>: the other arguments you pass, usually one but it can be more for some predicates. For example <span class=\"codespan\">product</span>.</li></ul>",
text.asHtml(linkResolver)
Expand All @@ -265,6 +263,18 @@ public void targetBlankLink() throws Exception {
);
}

@Test
public void lineBreaks() throws Exception {
ObjectMapper mapper = new ObjectMapper();
String jsonString = "{ \"type\": \"StructuredText\", \"value\": [ { \"type\": \"paragraph\", \"text\": \"Line\\nwith\\n\\nline breaks\", \"spans\": [] } ]}";
JsonNode json = mapper.readTree(jsonString);
Fragment.StructuredText text = Fragment.StructuredText.parse(json.path("value"));
Assert.assertEquals(
"<p>Line<br/>with<br/><br/>line breaks</p>",
text.asHtml(linkResolver)
);
}

/**
* Return JSON node from resource
* @param resource Json resource
Expand Down

0 comments on commit 410d5d8

Please sign in to comment.