Skip to content
Snippets Groups Projects
Commit 7d4c7f43 authored by Jan Zickermann's avatar Jan Zickermann
Browse files

OZG-4095 Simplify replacement

parent a8018ff4
Branches
Tags
2 merge requests!12Ozg 4095 abrufen aller nachrichten html,!11Ozg 4095 abrufen aller nachrichten
Pipeline #1604 failed
...@@ -24,39 +24,32 @@ public record Osi2HtmlDocument(String html) { ...@@ -24,39 +24,32 @@ public record Osi2HtmlDocument(String html) {
} }
static final List<SpanRendering> SPAN_RENDERINGS = List.of( static final List<SpanRendering> SPAN_RENDERINGS = List.of(
SpanRendering.of("a", anchor -> "[" + anchor.text() + "](" + anchor.attr("href") + ")"),
SpanRendering.of("img", image -> "![" + image.attr("alt") + "](" + image.attr("src") + ")"),
SpanRendering.of("i", italic -> "_" + italic.text() + "_"),
SpanRendering.of("em", italic -> "_" + italic.text() + "_"),
SpanRendering.of("b", bold -> "__" + bold.text() + "__"),
SpanRendering.of("strong", bold -> "__" + bold.text() + "__"),
SpanRendering.of("h1", heading -> "# " + heading.text()), SpanRendering.of("h1", heading -> "# " + heading.text()),
SpanRendering.of("h2", heading -> "## " + heading.text()), SpanRendering.of("h2", heading -> "## " + heading.text()),
SpanRendering.of("h3", heading -> "### " + heading.text()), SpanRendering.of("h3", heading -> "### " + heading.text()),
SpanRendering.of("i", italic -> "*" + italic.text() + "*"), SpanRendering.of("blockquote", bold -> "> " + bold.text())
SpanRendering.of("em", italic -> "*" + italic.text() + "*"),
SpanRendering.of("b", bold -> "**" + bold.text() + "**"),
SpanRendering.of("strong", bold -> "**" + bold.text() + "**"),
SpanRendering.of("img", image -> "![" + image.attr("alt") + "](" + image.attr("src") + ")"),
SpanRendering.of("a", anchor -> "[" + anchor.text() + "](" + anchor.attr("href") + ")"),
SpanRendering.of("blockquote", bold -> "> " + bold.wholeText())
); );
public String renderHTMLToPlainText() { public String renderToPlainText() {
var document = Jsoup.parse(html); var document = Jsoup.parse(html);
addNewlinesForSubsequentParagraphs(document); applySpanRenderings(document);
replaceElementsWithMarkdownSpansIn(document);
return document.wholeText(); return document.wholeText();
} }
private void addNewlinesForSubsequentParagraphs(Document document) { private void applySpanRenderings(Document document) {
document.body()
.select("p + p")
.prepend("\n");
}
private void replaceElementsWithMarkdownSpansIn(Document document) {
for (var entry : SPAN_RENDERINGS) { for (var entry : SPAN_RENDERINGS) {
entry.replaceElementsWithMarkdownSpansIn(document); entry.replaceElementsWithMarkdownSpansIn(document);
} }
} }
public static String renderPlainText(String html) { public static String renderToPlainText(String html) {
return new Osi2HtmlDocument(html).renderHTMLToPlainText(); return new Osi2HtmlDocument(html).renderToPlainText();
} }
} }
...@@ -49,6 +49,5 @@ public class PostfachApiFacadeService { ...@@ -49,6 +49,5 @@ public class PostfachApiFacadeService {
public void deleteMessage(final String messageId) { public void deleteMessage(final String messageId) {
messageExchangeApi.deleteMessage(UUID.fromString(messageId)); messageExchangeApi.deleteMessage(UUID.fromString(messageId));
} }
} }
...@@ -71,9 +71,9 @@ class Osi2HtmlDocumentTest { ...@@ -71,9 +71,9 @@ class Osi2HtmlDocumentTest {
@DisplayName("should render blockquote") @DisplayName("should render blockquote")
@Test @Test
void shouldRenderBlockquote() { void shouldRenderBlockquote() {
var plainText = renderPlainText("<blockquote><p>Example</p><p>Example2</p></blockquote>"); var plainText = renderPlainText("<blockquote><p>Example</p>\n<p>Example2</p></blockquote>");
assertThat(plainText).isEqualTo("> Example\nExample2"); assertThat(plainText).isEqualTo("> Example Example2");
} }
@DisplayName("should render html") @DisplayName("should render html")
...@@ -85,6 +85,6 @@ class Osi2HtmlDocumentTest { ...@@ -85,6 +85,6 @@ class Osi2HtmlDocumentTest {
} }
private String renderPlainText(String html) { private String renderPlainText(String html) {
return new Osi2HtmlDocument(html).renderHTMLToPlainText(); return new Osi2HtmlDocument(html).renderToPlainText();
} }
} }
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment