From b9155beb99fe549b730af27e309723dc34a24e59 Mon Sep 17 00:00:00 2001 From: Naoki Takezoe Date: Fri, 4 Mar 2016 02:15:51 +0900 Subject: [PATCH] (refs #8)Fix paragraph separation regular expression --- .../java/io/github/gitbucket/markedj/Grammer.java | 12 ++++++++---- .../io/github/gitbucket/markedj/MarkedTest.java | 15 +++++++++++++++ 2 files changed, 23 insertions(+), 4 deletions(-) diff --git a/src/main/java/io/github/gitbucket/markedj/Grammer.java b/src/main/java/io/github/gitbucket/markedj/Grammer.java index 924e25d..75a0166 100644 --- a/src/main/java/io/github/gitbucket/markedj/Grammer.java +++ b/src/main/java/io/github/gitbucket/markedj/Grammer.java @@ -20,10 +20,10 @@ public class Grammer { public static String BLOCK_HR = "^( *[-*_]){3,} *(?:\\n+|$)"; public static String BLOCK_HEADING = "^ *(#{1,6}) *([^\\n]+?) *#* *(?:\\n+|$)"; public static String BLOCK_LHEADING = "^([^\\n]+)\\n *(=|-){2,} *(?:\\n+|$)"; - public static String BLOCK_BLOCKQUOTE = "^( *>[^\\n]+(\\n(?!" + DEF + ")[^\\n]+)*\\n*)+"; - public static String BLOCK_LIST = "^( *)(" + BULLET + ") [\\s\\S]+?(?:" + HR + "|\\n+(?=" + DEF + ")|\\n{2,}(?! )(?!\\1" + BULLET + " )\\n*|\\s*$)"; + public static String BLOCK_BLOCKQUOTE = "^( *>[^\\n]+(\\n(?!" + removeLineStart(DEF) + ")[^\\n]+)*\\n*)+"; + public static String BLOCK_LIST = "^( *)(" + BULLET + ") [\\s\\S]+?(?:" + HR + "|\\n+(?=" + removeLineStart(DEF) + ")|\\n{2,}(?! )(?!\\1" + BULLET + " )\\n*|\\s*$)"; public static String BLOCK_DEF = "^ *\\[([^\\]]+)\\]: *]+)>?(?: +[\"(]([^\\n]+)[\")])? *(?:\\n+|$)"; - public static String BLOCK_PARAGRAPH = "^((?:[^\\n]+\\n?(?!" + BLOCK_HR + "|" + BLOCK_HEADING + "|" + BLOCK_LHEADING + "|" + BLOCK_BLOCKQUOTE + "|<" +TAG + "|" + BLOCK_DEF + "))+)\\n*"; + public static String BLOCK_PARAGRAPH = "^((?:[^\\n]+\\n?(?!" + removeLineStart(BLOCK_HR) + "|" + removeLineStart(BLOCK_HEADING) + "|" + removeLineStart(BLOCK_LHEADING) + "|" + removeLineStart(BLOCK_BLOCKQUOTE) + "|<" + TAG + "|" + removeLineStart(BLOCK_DEF) + "))+)\\n*"; public static String BLOCK_GFM_FENCES = "^ *(`{3,}|~{3,})[ \\.]*(\\S+)? *\\n([\\s\\S]*?)\\s*\\1 *(?:\\n+|$)"; public static Map BLOCK_RULES = new HashMap<>(); @@ -49,7 +49,7 @@ public class Grammer { BLOCK_GFM_RULES.putAll(BLOCK_RULES); BLOCK_GFM_RULES.put("fences", new FindFirstRule(BLOCK_GFM_FENCES)); - BLOCK_GFM_RULES.put("paragraph", new FindFirstRule(BLOCK_PARAGRAPH.replace("(?!", "(?!" + BLOCK_GFM_FENCES.replace("\\1", "\\2") + "|" + BLOCK_LIST.replace("\\1", "\\3") + "|"))); + BLOCK_GFM_RULES.put("paragraph", new FindFirstRule(BLOCK_PARAGRAPH.replace("(?!", "(?!" + removeLineStart(BLOCK_GFM_FENCES).replace("\\1", "\\2") + "|" + removeLineStart(BLOCK_LIST).replace("\\1", "\\3") + "|"))); BLOCK_GFM_RULES.put("heading", new FindFirstRule("^ *(#{1,6}) +([^\\n]+?) *#* *(?:\\n+|$)")); // TODO // block.gfm.paragraph = replace(block.paragraph) @@ -63,6 +63,10 @@ public class Grammer { BLOCK_TABLE_RULES.put("table", new FindFirstRule("^ *\\|(.+)\\n *\\|( *[-:]+[-| :]*)\\n((?: *\\|.*(?:\\n|$))*)\\n*")); } + private static String removeLineStart(String regex){ + return regex.replaceAll("(^|[^\\[])\\^", "$1"); + } + public static String INSIDE = "(?:\\[[^\\]]*\\]|[^\\[\\]]|\\](?=[^\\[]*\\]))*"; public static String HREF = "\\s*?(?:\\s+['\"]([\\s\\S]*?)['\"])?\\s*"; diff --git a/src/test/java/io/github/gitbucket/markedj/MarkedTest.java b/src/test/java/io/github/gitbucket/markedj/MarkedTest.java index 499f11b..1cff0c8 100644 --- a/src/test/java/io/github/gitbucket/markedj/MarkedTest.java +++ b/src/test/java/io/github/gitbucket/markedj/MarkedTest.java @@ -122,6 +122,21 @@ public void testEmptyItemOfList() throws Exception { assertEquals(loadResourceAsString("empty_item_of_list.html"), result); } + @Test + public void testParagraphSeparation() throws Exception { + String result = Marked.marked( + "Message A\n" + + "- List A\n" + + "- List B", new Options()); + + assertEquals( + "

Message A

\n" + + "\n", result); + } + private String loadResourceAsString(String path) throws IOException { InputStream in = Thread.currentThread().getContextClassLoader().getResourceAsStream(path); try {