Skip to content
Permalink
Browse files
404: Add a blank line after quoted blocks in TextToMarkdown
Reviewed-by: jvernee
  • Loading branch information
rwestberg committed Jun 1, 2020
1 parent 417adeb commit f4d9b6e87c48c93bf0fcdcf2ac9723a0ae598a0d
@@ -22,11 +22,13 @@
*/
package org.openjdk.skara.bots.mlbridge;

import java.util.ArrayList;
import java.util.regex.*;

public class TextToMarkdown {
private static final Pattern punctuationPattern = Pattern.compile("([!\"#$%&'()*+,\\-./:;<=?@\\[\\]^_`{|}~])", Pattern.MULTILINE);
private static final Pattern indentedPattern = Pattern.compile("^ {4}", Pattern.MULTILINE);
private static final Pattern quoteBlockPattern = Pattern.compile("^(>(>|\\s)*\\s.*$)", Pattern.MULTILINE);

private static String escapeBackslashes(String text) {
return text.replace("\\", "\\\\");
@@ -42,7 +44,24 @@ private static String escapeIndention(String text) {
return indentedMatcher.replaceAll("&#32; ");
}

private static String separateQuoteBlocks(String text) {
var ret = new ArrayList<String>();
var lastLineQuoted = false;
for (var line : text.split("\\R")) {
if ((line.length() > 0) && (line.charAt(0) == '>')) {
lastLineQuoted = true;
} else {
if (lastLineQuoted && !line.isBlank()) {
ret.add("");
}
lastLineQuoted = false;
}
ret.add(line);
}
return String.join("\n", ret);
}

static String escapeFormatting(String text) {
return escapeIndention(escapePunctuation(escapeBackslashes(text)));
return escapeIndention(escapePunctuation(escapeBackslashes(separateQuoteBlocks(text))));
}
}
@@ -48,4 +48,12 @@ void preserveQuoting() {
void escapedPattern() {
assertEquals("1\\$2", TextToMarkdown.escapeFormatting("1$2"));
}

@Test
void separateQuoteBlocks() {
assertEquals("> 1\n\n2", TextToMarkdown.escapeFormatting("> 1\n2"));
assertEquals("> 1\n\n2", TextToMarkdown.escapeFormatting("> 1\n\n2"));
assertEquals("> 1\n> 2\n\n3", TextToMarkdown.escapeFormatting("> 1\n> 2\n3"));
assertEquals("> 1\n> 2\n\n3", TextToMarkdown.escapeFormatting("> 1\n> 2\n\n3"));
}
}

0 comments on commit f4d9b6e

Please sign in to comment.