Skip to content

Commit

Permalink
(refs #8)Fix paragraph separation regular expression
Browse files Browse the repository at this point in the history
  • Loading branch information
takezoe committed Mar 3, 2016
1 parent 794b467 commit b9155be
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 4 deletions.
12 changes: 8 additions & 4 deletions src/main/java/io/github/gitbucket/markedj/Grammer.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 = "^ *\\[([^\\]]+)\\]: *<?([^\\s>]+)>?(?: +[\"(]([^\\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<String, Rule> BLOCK_RULES = new HashMap<>();
Expand All @@ -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)
Expand All @@ -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\\S]*?)['\"])?\\s*";

Expand Down
15 changes: 15 additions & 0 deletions src/test/java/io/github/gitbucket/markedj/MarkedTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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(
"<p>Message A</p>\n" +
"<ul>\n" +
"<li>List A</li>\n" +
"<li>List B</li>\n" +
"</ul>\n", result);
}

private String loadResourceAsString(String path) throws IOException {
InputStream in = Thread.currentThread().getContextClassLoader().getResourceAsStream(path);
try {
Expand Down

0 comments on commit b9155be

Please sign in to comment.