EscapeUtils { public static List<String> escapeLines(List<String> lines) { boolean needsEscape = true; for (int i = 0; i < lines.size(); i++) { String line = lines.get(i); if (line.startsWith("```")) { needsEscape = !needsEscape; } if (needsEscape && !line.startsWith(" ")) { line = escapeSingleLine(line); line = preserveRuleTagQuotes(line); } lines.set(i, line); } return lines; } private  EscapeUtils(); static String escapeMarkdown(String unescaped); static String escapeSingleLine(String line); static List<String> escapeLines(List<String> lines); static String preserveRuleTagQuotes(String text);  }
@Test public void testEscapeHtmlWithinMarkdownBlocks() { String text = "paragraph\n\n> quote <script>\n> quote line \"2\"\n>quote line `<script>` 3\n\n" + "next paragraph\n\n code <script> \"a < b\"\n code line 2\n\n" + "next paragraph\n\n```\ncode <script> \"a < b\"\ncode line 2\n```\n\n" + "next paragraph\n\n```java\nString = \"code <script> with syntax highlighting\";\ncode line 2\n```\n"; String expected = "paragraph\n\n> quote &lt;script&gt;\n> quote line &quot;2&quot;\n>quote line `<script>` 3\n\n" + "next paragraph\n\n code <script> \"a < b\"\n code line 2\n\n" + "next paragraph\n\n```\ncode <script> \"a < b\"\ncode line 2\n```\n\n" + "next paragraph\n\n```java\nString = \"code <script> with syntax highlighting\";\ncode line 2\n```\n"; List<String> escaped = EscapeUtils.escapeLines(Arrays.asList(text.split("\n"))); assertEquals(Arrays.asList(expected.split("\n")), escaped); }